串口通信:发送的数据可以接收到,可是和发送的不一样啊(10分)

  • 主题发起人 主题发起人 lcl_003
  • 开始时间 开始时间
L

lcl_003

Unregistered / Unconfirmed
GUEST, unregistred user!
我在发送端定时发送数据,接收端接收到的数据和我发送的不一样啊,我发送端的代码
如下:
procedure TForm1.FormCreate(Sender: TObject);
begin
try
Comm1.startcomm;//创建窗体时,将comm1控件打开。
SendBuffer[0]:=$0;
SendBuffer[1]:=$0;
SendBuffer[2]:=$0;
SendBuffer[3]:=$0;
SendBuffer[4]:=$0;
SendBufferLength:=5;
Sendtimer.Enabled :=true;
Except
on E:Exception do
showmessage(E.Message );
//comm1.StopComm ;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;//关闭窗体时,将comm1控件关闭。
end;
procedure TForm1.sendtimerTimer(Sender: TObject);
var
viewstring:string;
i:integer;
begin
SendBuffer[0]:=SendBuffer[0]+1;
if SendBuffer[0]>9 then
begin
SendBuffer[0]:=0;
SendBuffer[1]:=SendBuffer[1]+1;
end;
if SendBuffer[1]>9 then
begin
SendBuffer[1]:=0;
SendBuffer[2]:=SendBuffer[2]+1;
end;
if SendBuffer[2]>9 then
begin
SendBuffer[3]:=0;
SendBuffer[3]:=SendBuffer[3]+1;
end;
if SendBuffer[3]>9 then SendBuffer[3]:=0;
comm1.WritecommData(pchar(@SendBuffer),SendBufferLength);
for i:=0 to SendBufferLength-1 do
viewstring:=viewstring+inttohex(SendBuffer,2)+' ';
viewstring:='发送'+viewstring;
Memo1.Lines.Add(viewstring);
memo1.Lines.Add ('');
end;



接收端代码如下:
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
viewstring:string;
i:integer;
ReceiveBuffer:array[0..5] of integer;
begin
//SetLength(10, BufferLength); //接收RS232的数据并显示Memo1上。
Move(Buffer^,pchar((@ReceiveBuffer)^), BufferLength);
for i:=0 to bufferlength-1 do
viewstring:=viewstring+inttohex(ReceiveBuffer,2)+' ';
viewstring:='接收'+viewstring;
Memo1.Lines.Add(viewstring);
Memo1.Lines.Add('');
Memo1.Invalidate;


end;

procedure TForm1.FormCreate(Sender: TObject);
begin
comm1.StartComm;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;

发送端发送的数据应该是下面格式的数据
发送01 00 00 00 00
发送02 00 00 00 00
发送03 00 00 00 00
发送04 00 00 00 00

可是接收端接收的格式却是:
接收01 16873F08 04 00 CD0FC8
接收02 16873F08 04 00 CD0FC8
接收03 2460008 8C880000 3EF80003 165F0000

 
接收端不对吧:Move(Buffer^,pchar((@ReceiveBuffer)^), BufferLength);
改成:
Move(Buffer^,ReceiveBuffer, BufferLength);
 
等我研究一下再说!:)
 
pipi 老大,我试了试还是不行接收端变成下面的格式了(83是我发的固定值)


接收83 16870004 04 00 CD0FC8 10E01AB

接收83 2460005 8C880000 3EF80002 165F0000 273728B6

接收83 2460006 8C880000 3EF80002 165F0000 273728B6

接收83 2460007 8C880000 3EF80002 165F0000 273728B6

接收83 2460008 8C880000 3EF80002 165F0000 273728B6

 
你看看SPcomm的例子
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部