有关SPCOMM使用的问题(100分)

  • 主题发起人 主题发起人 wzwz
  • 开始时间 开始时间
W

wzwz

Unregistered / Unconfirmed
GUEST, unregistred user!
请各位能给我一个较详细的SPCOMM的使用例子,给一○○分
 
http://www.delphibbs.com/delphibbs/DispQ.asp?LID=678999
 
使用SPCOMM可以不需要例子,但你需要熟悉串口通信及应用时所需要的设置。
如果你附150分可以给你做一个串口收发的例子。
toyzn@sohu.com
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, SPComm;
type
TForm1 = class(TForm)
Comm1: TComm;
Memo1: TMemo;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
var
s: string;
begin
SetLength(S, BufferLength); //接收RS232的数据并显示Memo1上。
Move(Buffer^, PChar(S)^, BufferLength);
Memo1.Lines.Add(S);
Memo1.Invalidate;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Comm1.startcomm;//创建窗体时,将comm1控件打开。
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;//关闭窗体时,将comm1控件关闭。
end;
end.

 
我的分数不多,请谅解。
 
我前天做的,调试的时候,请用com1口,当然你自己也可以重新设置
单机调试,请把串口的2、3两脚用导线连接。有问题可发email给我,给你全部程序。
w.j.deng@263.net


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;

type
TForm1 = class(TForm)
Comm1: TComm;
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Viewstring:string;
i:integer;
rbuf,sbuf:array[1..6] of byte;



implementation

{$R *.dfm}
procedure senddata;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=1 to 6 do
begin
if not form1.comm1.writecommdata(@sbuf,1) then
begin
commflg:=false;
break;
end;
sleep(2); {发送时字节间的延时}
viewstring:=viewstring+inttohex(sbuf,2)+'';
end;

viewstring:='发送'+viewstring;
form1.memo1.lines.add(viewstring);
form1.memo1.lines.add('');
if not commflg then messagedlg('发送失败!',mterror,[mbyes],0);
end;


procedure TForm1.Button2Click(Sender: TObject);
begin

comm1.StartComm; button2.Enabled:=false;
showmessage('ok!');

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);

var

i:integer;

begin

viewstring:='';
move(buffer^,rbuf,bufferlength);

for i:=1 to bufferlength do

viewstring:=viewstring+inttohex(rbuf,2)+' ';

viewstring:='接受'+viewstring;

memo2.lines.add(viewstring);

memo2.lines.add('');

end;




procedure TForm1.Button1Click(Sender: TObject);
begin
sbuf[1]:=byte($f0); {帧头}

sbuf[2]:=byte($01); {命令号}

sbuf[3]:=byte($ff);

sbuf[4]:=byte($ff);

sbuf[5]:=byte($01);

sbuf[6]:=byte($0f); {帧尾}

senddata;{调用发送函数}

end;

end.
 
上面仅仅是传一个数据进行测试。
 
后退
顶部