使用Serial comport 411f delphi 7

时间:2016-11-23 07:07:06

标签: delphi delphi-7 tcomport

我有一个Delphi 7代码,可以从DSP TMS32F28069接收传感器值。 Delphi收到的值是Hex文件数据。例如,我发送数据:

  

F1; 01; 01; 07; 00; 00; 0A; 00; 00; 00; 00; F7

来自DSP的

我使用Comport 411f,实际上当我使用Windows 10 64位英文版时,一切都很好。但是当我使用Windows 64位时,有时接收到的数据有时会发生变化。我尝试使用Windows 7 64位中文版的几款笔记本,它也有同样的问题。 Windows 7 64位中文版收到的文件显示:

  

F1; 01; 01; 01; 00; 00; 00; F7; 00; 00; F7; 00.或F1; 01; 07; 01; 00; 0A; 00; 00; F7; F7; 00; 00

并且总是改变。这是我在Delphi 7中编写的代码:

procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var
p:integer;
r:array[1..12]of integer;
h:array[1..12]of String;
 begin
   comport1.Open;
  for p:=1 to 12 do
   begin
     comport1.Read(r[p],1);
     h[p]:= IntToHex((r[p]),2);
     sMemo3.Text:=   h[1]+';'+h[2]+';'+h[3]+';'+h[4]+';'+h[5]+';'+h[6]+';'+h[7]+';'+h[8]+';'+h[9]+';'+h[10]+';'+h[11]+';'+h[12];//Show data Receive on Memo4//
    end;
end;

请给我任何建议为什么在Windows 7 64位中文版上发生这种情况?因为当我使用Windows 7 64位英文版时,它也可以正常工作。

谢谢

1 个答案:

答案 0 :(得分:1)

  1. 删除comport1.Open - 如果发生RxChar事件,无疑会打开

  2. 本地整数数组中填充了一些废话。 comport1.Read(r[p],1);只填充一个字节。所以使用字节数组

  3. 在每个字节后输出完整的数据 - 这是一种奇怪的方法。

  4. 当事件触发时,端口缓冲区包含Count个字节 - 因此读取实际字节数。更好的方法 - 在全局数组(或ansistring)中累积接收的信息,并在收到12个字节时对其进行处理。

  5. Buffer: AnsiString;
    ...
    
    procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
    var
      sa: AnsiString;
      ByteBuf: array[1..12] of Byte;
    begin
       SetLength(sa, Count);
       comport1.Read(sa[1], Count);
       Buffer := Buffer + sa;
       while Length(Buffer) >= 12 do begin
          Move(Buffer[1], ByteBuf, 12); 
          TreatData(ByteBuf);
          Delete(Buffer, 1, 12);
       end;
     end;
    
     procedure TreatData(bb: array of Byte);
     //treat and output here 
    
相关问题