Z
zts566
Unregistered / Unconfirmed
GUEST, unregistred user!
unit comm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;
type
TFComm = class(TForm)
Memo1: TMemo;
BtnSend: TButton;
Comm1: TComm;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure BtnSendClick(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
procedure SendData;
public
{ Public declarations }
end;
var
FComm: TFComm;
ViewString:string;
i:integer;
sbuf:array[1..6] of byte;
implementation
{$R *.dfm}
procedure TFComm.FormShow(Sender: TObject);
begin
self.Comm1.StartComm;
end;
procedure TFComm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
self.Comm1.StopComm;
end;
procedure TFComm.SendData;
var
i:integer;
commflg:boolean;
str:string;
count:integer;
begin
viewString:='';
commflg:=true;
for i:=1 to 8do
begin
if not self.Comm1.WriteCommData(@sbuf,1) then
begin
commflg:=false;
break;
end;
sleep(2);
{发送时字节间的延时}
ViewString:=ViewString+inttohex(sbuf,2)+' ';
//inttohex():10-16进制的转换
end;
ViewString:='发送'+ViewString;
self.Memo1.Lines.Add(ViewString);
self.Memo1.Lines.Add('');
if not commflg then
MessageDlg('发送失败!', mtError, [], 0);
end;
procedure TFComm.BtnSendClick(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;
procedure TFComm.Comm1ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
var
i:integer;
recivedata:array of byte;
rbuf:array of byte;
begin
showmessage('dd');
ViewString:='';
move(buffer^,recivedata,bufferlength);
for i:=1 to bufferlengthdo
begin
ViewString:=ViewString+inttohex(recivedata,2)+' ';
end;
ViewString:='接受'+ViewString;
self.Memo1.Lines.Add(ViewString);
self.Memo1.Lines.Add('');
end;
end.
这是在网上找到的一个例子,为什么就是没有触发ReceiveData()事件呢?
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;
type
TFComm = class(TForm)
Memo1: TMemo;
BtnSend: TButton;
Comm1: TComm;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure BtnSendClick(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
procedure SendData;
public
{ Public declarations }
end;
var
FComm: TFComm;
ViewString:string;
i:integer;
sbuf:array[1..6] of byte;
implementation
{$R *.dfm}
procedure TFComm.FormShow(Sender: TObject);
begin
self.Comm1.StartComm;
end;
procedure TFComm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
self.Comm1.StopComm;
end;
procedure TFComm.SendData;
var
i:integer;
commflg:boolean;
str:string;
count:integer;
begin
viewString:='';
commflg:=true;
for i:=1 to 8do
begin
if not self.Comm1.WriteCommData(@sbuf,1) then
begin
commflg:=false;
break;
end;
sleep(2);
{发送时字节间的延时}
ViewString:=ViewString+inttohex(sbuf,2)+' ';
//inttohex():10-16进制的转换
end;
ViewString:='发送'+ViewString;
self.Memo1.Lines.Add(ViewString);
self.Memo1.Lines.Add('');
if not commflg then
MessageDlg('发送失败!', mtError, [], 0);
end;
procedure TFComm.BtnSendClick(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;
procedure TFComm.Comm1ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
var
i:integer;
recivedata:array of byte;
rbuf:array of byte;
begin
showmessage('dd');
ViewString:='';
move(buffer^,recivedata,bufferlength);
for i:=1 to bufferlengthdo
begin
ViewString:=ViewString+inttohex(recivedata,2)+' ';
end;
ViewString:='接受'+ViewString;
self.Memo1.Lines.Add(ViewString);
self.Memo1.Lines.Add('');
end;
end.
这是在网上找到的一个例子,为什么就是没有触发ReceiveData()事件呢?