H
hrc378
Unregistered / Unconfirmed
GUEST, unregistred user!
我编一个串口的发送和接收问题,下面是我编写的一个程序,但是它不能发送跟接收,
你们帮我看一下错在哪!
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
comm1.StartComm;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
//从端口发送数据
procedure TForm1.senddataClick(Sender: TObject);
var
strchar;
count:integer;
begin
str:=pchar(memo1.Text );
count:=length(str);
if comm1.writecommData(str,count)then
Memo1.Lines.Add('已发送'+inttostr(count)+'个字节')
else
raise exception.create('发送错误');
end;
//从端口接收数据
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
strReceiv:string;
begin
setlength (strReceiv,Bufferlength);
move(Buffer^,Pchar(strReceiv)^,Bufferlength);
memo1.Lines.Add('已接收'+inttostr(Bufferlength)+'字节数据');
memo1.Lines.Add(strReceiv);
memo1.Invalidate;
end;
你们帮我看一下错在哪!
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
comm1.StartComm;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
//从端口发送数据
procedure TForm1.senddataClick(Sender: TObject);
var
strchar;
count:integer;
begin
str:=pchar(memo1.Text );
count:=length(str);
if comm1.writecommData(str,count)then
Memo1.Lines.Add('已发送'+inttostr(count)+'个字节')
else
raise exception.create('发送错误');
end;
//从端口接收数据
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
strReceiv:string;
begin
setlength (strReceiv,Bufferlength);
move(Buffer^,Pchar(strReceiv)^,Bufferlength);
memo1.Lines.Add('已接收'+inttostr(Bufferlength)+'字节数据');
memo1.Lines.Add(strReceiv);
memo1.Invalidate;
end;