E
ESCduxf
Unregistered / Unconfirmed
GUEST, unregistred user!
本人最近新学delphi,用spcomm控件,发送很好!但无法接收,高手指导指导!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Comm1: TComm;
procedure Button1Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
comm1:tcomm;
viewstring:string;
sbuf:array[0..20] of byte;
rbuf:array[0..20] of byte;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Comm1.StartComm;
sleep(200);
sbuf[0]:=byte(strtoint('$'+'68')); //帧头
sbuf[1]:=byte(strtoint('$'+'03'));
sbuf[2]:=byte(strtoint('$'+'00'));
sbuf[3]:=byte(strtoint('$'+'00'));
sbuf[4]:=byte(strtoint('$'+'00'));
sbuf[5]:=byte(strtoint('$'+'00'));
sbuf[6]:=byte(strtoint('$'+'00'));
sbuf[7]:=byte(strtoint('$'+'68'));
sbuf[8]:=byte(strtoint('$'+'01'));
sbuf[9]:=byte(strtoint('$'+'02'));
sbuf[10]:=byte(strtoint('$'+'52'));
sbuf[11]:=byte(strtoint('$'+'C3'));
sbuf[12]:=byte(strtoint('$'+'EB'));
sbuf[13]:=byte(strtoint('$'+'16'));
Comm1.WriteCommData(@sbuf,14);
//Comm1.StopComm;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:='';
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
begin
viewstring:=viewstring+ inttohex(rbuf,2)+'' ;
end;
viewstring:='接收'+ viewstring;
//memo1.lines.add(viewstring);
//memo1.lines.add('');
end;
procedure TForm1.Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
begin
showmessage('错误');
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Comm1: TComm;
procedure Button1Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
comm1:tcomm;
viewstring:string;
sbuf:array[0..20] of byte;
rbuf:array[0..20] of byte;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Comm1.StartComm;
sleep(200);
sbuf[0]:=byte(strtoint('$'+'68')); //帧头
sbuf[1]:=byte(strtoint('$'+'03'));
sbuf[2]:=byte(strtoint('$'+'00'));
sbuf[3]:=byte(strtoint('$'+'00'));
sbuf[4]:=byte(strtoint('$'+'00'));
sbuf[5]:=byte(strtoint('$'+'00'));
sbuf[6]:=byte(strtoint('$'+'00'));
sbuf[7]:=byte(strtoint('$'+'68'));
sbuf[8]:=byte(strtoint('$'+'01'));
sbuf[9]:=byte(strtoint('$'+'02'));
sbuf[10]:=byte(strtoint('$'+'52'));
sbuf[11]:=byte(strtoint('$'+'C3'));
sbuf[12]:=byte(strtoint('$'+'EB'));
sbuf[13]:=byte(strtoint('$'+'16'));
Comm1.WriteCommData(@sbuf,14);
//Comm1.StopComm;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:='';
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
begin
viewstring:=viewstring+ inttohex(rbuf,2)+'' ;
end;
viewstring:='接收'+ viewstring;
//memo1.lines.add(viewstring);
//memo1.lines.add('');
end;
procedure TForm1.Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
begin
showmessage('错误');
end;
end.