D
danliyou
Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi编的串口通讯程序接收插在PCI插槽板上的数据出错。
我板上发送的数据是55H,而接收的是‘fffef8’而且每次接收的都不一样。请问是什么原因?
我用本机的com2口接收。
delphi程序如下:
unit Comm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, SPComm;
type
TFCOMM = class(TForm)
Memo1: TMemo;
Btn_send: TButton;
Button1: TButton;
Comm2: TComm;
Memo2: TMemo;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Btn_sendClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Comm2ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FCOMM: TFCOMM;
Viewstring:string;
i:integer;
rbuf,sbuf:array[1..5] of byte;
//定义发送和接收数组
implementation
{$R *.DFM}
procedure TFCOMM.FormShow(Sender: TObject);
begin
//comm1.StartComm;
//打开串口
comm2.StartComm;
end;
procedure TFCOMM.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
// comm1.StopComm;
//关闭串口
comm2.StopComm;
end;
procedure senddata;
//自定义的发送数据过程
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=1 to length(sbuf) do
begin
if not fcomm.comm2.writecommdata(@sbuf,1) then
begin
commflg:=false;
break;
end;
sleep(2);
//发送时字节间的延时
viewstring:=viewstring+inttohex(sbuf,2)+'';
end;
viewstring:='发送:'+viewstring;
fcomm.memo1.lines.add(viewstring);
//fcomm.memo1.lines.add('');
if not commflg then
messagedlg('发送失败!',mterror,[mbyes],0);
end;
procedure TFCOMM.Btn_sendClick(Sender: TObject);
begin
sbuf[1]:=byte($55);
sbuf[2]:=byte($00);
sbuf[3]:=byte($00);
sbuf[4]:=byte($00);
sbuf[5]:=byte($55);
senddata;{调用发送函数}
end;
procedure TFCOMM.Button1Click(Sender: TObject);
begin
close();
end;
procedure TFCOMM.Comm2ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
//COM2接收过程
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;
memo2.lines.add(viewstring);
//memo2.lines.add('');
end;
end.
我板上发送的数据是55H,而接收的是‘fffef8’而且每次接收的都不一样。请问是什么原因?
我用本机的com2口接收。
delphi程序如下:
unit Comm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, SPComm;
type
TFCOMM = class(TForm)
Memo1: TMemo;
Btn_send: TButton;
Button1: TButton;
Comm2: TComm;
Memo2: TMemo;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Btn_sendClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Comm2ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FCOMM: TFCOMM;
Viewstring:string;
i:integer;
rbuf,sbuf:array[1..5] of byte;
//定义发送和接收数组
implementation
{$R *.DFM}
procedure TFCOMM.FormShow(Sender: TObject);
begin
//comm1.StartComm;
//打开串口
comm2.StartComm;
end;
procedure TFCOMM.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
// comm1.StopComm;
//关闭串口
comm2.StopComm;
end;
procedure senddata;
//自定义的发送数据过程
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=1 to length(sbuf) do
begin
if not fcomm.comm2.writecommdata(@sbuf,1) then
begin
commflg:=false;
break;
end;
sleep(2);
//发送时字节间的延时
viewstring:=viewstring+inttohex(sbuf,2)+'';
end;
viewstring:='发送:'+viewstring;
fcomm.memo1.lines.add(viewstring);
//fcomm.memo1.lines.add('');
if not commflg then
messagedlg('发送失败!',mterror,[mbyes],0);
end;
procedure TFCOMM.Btn_sendClick(Sender: TObject);
begin
sbuf[1]:=byte($55);
sbuf[2]:=byte($00);
sbuf[3]:=byte($00);
sbuf[4]:=byte($00);
sbuf[5]:=byte($55);
senddata;{调用发送函数}
end;
procedure TFCOMM.Button1Click(Sender: TObject);
begin
close();
end;
procedure TFCOMM.Comm2ReceiveData(Sender: TObject;
Buffer: Pointer;
BufferLength: Word);
//COM2接收过程
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;
memo2.lines.add(viewstring);
//memo2.lines.add('');
end;
end.