unit sptest;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
SPComm, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Comm1: TComm;
Timer1: TTimer;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormHide(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
viewstring:string;
i:integer;rbuf,sbuf:array[1..4] of byte;
rbuf1:array[1..8] of byte;
procedure senddata;
procedure outsenddata;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
sbuf[1]:=byte($cc);
sbuf[2]:=byte($00);
sbuf[3]:=byte($02);
sbuf[4]:=byte($0c);
senddata;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
comm1.StopComm ;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var i:integer;
begin
timer1.Enabled :=false;
viewstring:='';
for i:=1 to bufferlength do
viewstring:=viewstring+inttohex(rbuf,2)+'';
viewstring:='接收'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
sbuf1[1]:=byte($cc);
sbuf1[2]:=byte($06);
sbuf1[3]:=byte($05);
sbuf1[4]:=byte($0b);
sbuf1[5]:=byte($00);
sbuf1[6]:=byte($00);
sbuf1[7]:=byte($00);
sbuf1[8]:=byte($0c);
outsenddata;
timer1.Enabled :=true;
end;
procedure senddata;
var i,z:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
z:=0;
for i:=1 to 4 do
begin
z:=z+1;
if z=3 then begin sleep(30);z:=0;end ;
if not form1.Comm1.WriteCommData(@sbuf,1) then
begin
commflg:=false;
break;
end;
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.FormShow(Sender: TObject);
begin
comm1.StartComm;
end;
procedure TForm1.FormHide(Sender: TObject);
begin
comm1.StopComm ;
end;
procedure outsenddata;
var i,z:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
z:=0;
for i:=1 to 8 do
begin
z:=z+1;
if z=3 then begin sleep(30);z:=0;end ;
if not form1.Comm1.WriteCommData(@sbuf1,1) then
begin
commflg:=false;
break;
end;
viewstring:=viewstring+inttohex(sbuf1,2)+'';
end;
viewstring:='发送'+viewstring;
form1.memo1.lines.add(viewstring);
form1.memo1.lines.add('');
if not commflg then messagedlg('发送失败',mterror,[mbyes],0);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
sbuf[1]:=byte($cc);
sbuf[2]:=byte($00);
sbuf[3]:=byte($02);
sbuf[4]:=byte($0c);
senddata;
end;
end.