带有ComPort的Delphi 7中的串行端口不会完全发送数据

时间:2017-01-06 11:37:34

标签: delphi tcomport

使用TComPort组件时,我在Delphi中访问串口时遇到问题。它无法完全发送数据,但有些数据会随机丢失,如下例所示:

  

061:1268:-108:16596:-462:239:-75:0.000000:0.000000
  061:
  061:1240:-84:16616:-425:235:-54:0.000000:0
。   061:1220:-96:16620:-418:224:-69:0.000000:0.000000:0.0055
  061:1220:-72:16676:-420:246:-72:0.0
  061:1100:-176:16688:-460:224:-45:0.000000:0.000000:0.0056
  061:1164:-144:16676:-442:23
  061:1272:-136:16456:-413:219:-84:0.000000:0.000000:0.0056
  061:1096:-152:16616
  061:1096:-140:16656:-427:235:-88:0.000000:0.000000:0.0056
  061:1200:-1
  061:1192:-84:16664:-447:228:-60:0.000000:0.000000:0.0055
  061
  061:1208:-116:16576:-421:233:-56:0.000000:0.000000
  061:1208:-128:16584:-427:226:-94:0.000000:0.000000:0.0056
  061:1172:-264:16560:-436:257:-72:0.000000:
  061:1196:-200:16724:-428:234:-78:0.000000:0.000000:0.0056

正确的数据应包含10个值,如下所示:

  

061:1196:-200:16724:-428:234:-78:0.000000:0.000000:0.0056

请帮我解决这个问题。我是Delphi的初学者。

这是我的代码:

procedure TForm1.ComDataPacket1Packet(Sender: TObject; const Str: String);
begin
  allData := allData + Str;
  if Pos (#$A, allData) > 0 then
  begin
    awal := 1;
    batas := Pos(':', allData);
    Delete(allData, awal, batas);

    //Data Gyroscope
    awal := 1;
    batas := Pos(':', allData);
    gyroX := Copy(allData, awal, batas-1);
    Delete(allData, awal, batas);
    Label2.Caption := gyroX;

    awal := 1;
    batas := Pos(':', allData);
    gyroY := Copy(allData, awal, batas-1);
    Delete(allData, awal, batas);
    Label3.Caption := gyroY;

    awal := 1;
    batas := Pos(':', allData);
    gyroZ := Copy(allData, awal, batas-1);
    Delete(allData, awal, batas);
    Label4.Caption := gyroZ;

    //Data Accelero
    awal := 1;
    batas := Pos(':', allData);
    accX := Copy(allData, awal, batas-1);
    Delete(allData, awal, batas);
    Label6.Caption := accX;

    awal := 1;
    batas := Pos(':', allData);
    accY := Copy(allData, awal, batas-1);
    Delete(allData, awal, batas);
    Label7.Caption := accY;

    awal := 1;
    batas := Pos(':', allData);
    accZ := Copy(allData, awal, batas-1);
    Delete(allData, 1, batas);
    Label8.Caption := accZ;

    //Data GPS
    awal := 1;
    batas := Pos(':', allData);
    Lat := Copy(allData, awal, batas-1);
    Delete(allData, awal, batas);
    Label10.Caption := Lat;

    awal := 1;
    batas := Pos(':', allData);
    Long := Copy(allData, awal, batas-1);
    Delete(allData, awal, batas);
    Label11.Caption := Long;

    awal := 1;
    palingAkhir := Pos(#$A, allData);
    Alt := Copy(allData, awal, palingAkhir-1);
    Delete(allData, awal, palingAkhir);
    Label12.Caption := Alt;

    //Showing to Memo
    Memo1.Text := Memo1.Text + Str + #$D;
    SendMessage(Memo1.Handle, EM_LINESCROLL, 0, Memo1.Lines.Count);
  end

1 个答案:

答案 0 :(得分:0)

我不知道数据包模式如何帮助您。尝试一种没有数据包的方法,制作OnRXChar事件处理程序。我假设您的数据包含结束标记字节$ A,没有开始标记。

var
  Accumulator: AnsiString;
  List: TStringList;
...
//OnCreate
List:= TStringList.Create;
List.Delimiter := ':';

//OnDestroy
 List.Free;


procedure XXX.RXChar(Sender: TObject; Count: Integer);
var
  s, DataStr: AnsiString;
  APos: Integer;
begin
  ComPort.ReadStr(s, Count);
  Accumulator := Accumulator + s;
  APos := Pos (#$A, Accumulator);
  if APos > 0 then begin
    DataStr := Copy(Accumulator, 1, APos - 1);
    Delete(Accumulator, 1, APos);
    Memo1.Lines.Add(DataStr);
    List.DelimitedText := DataStr;
    if List.Count = 10 then
      TreatListItems; //extract data from List[]
  end;
end;