我用spcomm做串口通信,同样的程序,两台机子只是波特率不同,怎么2400的可以发送指令帧和接收返回帧,1200的可以发出指令帧却接收不到返回帧?我用串口大师测试时,是都可以接受到返回帧的。怀疑是我的接受段程序有什么问题,麻烦各位高手指教!//***********接收**********//
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
iLoop,iPos: integer;
i:integer;
begin
viewstring:='';
for i:=1 to 200 do begin
rbuf[i]:=0;
end;
move(buffer^,pchar(@rbuf)^,bufferlength);
bSuccess:=true;
giReceLen:=bufferlength-giSendLen; for i := 1 to giReceLen do
begin
rbuf[i] := rbuf[giSendLen + i];
end;
for iLoop:=1 to giReceLen do
viewstring:=viewstring+inttohex(rbuf[iloop],2)+' ';
iPos:=pos('68',viewstring);
if showdetails = 1 then
begin
viewstring:='接收:'+viewstring;
memo1.Lines.Add(viewstring);
memo1.Lines.Add('');
end;
if iPos=0 then
exit; iPos:=(iPos div 3)+1; //68H在rbuf数组的序号 68H是数据段起止标识 begin
if (rbuf[iPos+8] = $81) then
begin
edit1.Text := Format('%.2x%.2x%.2x%.2x%.2x',
[ (rbuf[19]-$33),(rbuf[18]-$33),(rbuf[17]-$33),
(rbuf[16]-$33),(rbuf[15]-$33)]);
addbuf[1] := rbuf[4];
addbuf[2] := rbuf[5];
addbuf[3] := rbuf[6];
addbuf[4] := rbuf[7];
addbuf[5] := rbuf[8];
addbuf[6] := rbuf[9];
addbuf[7] := rbuf[10];
if not bSuccess then
Exit;
end;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
iLoop,iPos: integer;
i:integer;
begin
viewstring:='';
for i:=1 to 200 do begin
rbuf[i]:=0;
end;
move(buffer^,pchar(@rbuf)^,bufferlength);
bSuccess:=true;
giReceLen:=bufferlength-giSendLen; for i := 1 to giReceLen do
begin
rbuf[i] := rbuf[giSendLen + i];
end;
for iLoop:=1 to giReceLen do
viewstring:=viewstring+inttohex(rbuf[iloop],2)+' ';
iPos:=pos('68',viewstring);
if showdetails = 1 then
begin
viewstring:='接收:'+viewstring;
memo1.Lines.Add(viewstring);
memo1.Lines.Add('');
end;
if iPos=0 then
exit; iPos:=(iPos div 3)+1; //68H在rbuf数组的序号 68H是数据段起止标识 begin
if (rbuf[iPos+8] = $81) then
begin
edit1.Text := Format('%.2x%.2x%.2x%.2x%.2x',
[ (rbuf[19]-$33),(rbuf[18]-$33),(rbuf[17]-$33),
(rbuf[16]-$33),(rbuf[15]-$33)]);
addbuf[1] := rbuf[4];
addbuf[2] := rbuf[5];
addbuf[3] := rbuf[6];
addbuf[4] := rbuf[7];
addbuf[5] := rbuf[8];
addbuf[6] := rbuf[9];
addbuf[7] := rbuf[10];
if not bSuccess then
Exit;
end;
end;
你可以注意一下流控制(Flowcontrol)的设置