Serial Port in Delphi 7 with ComPort do not send the data fully

3.1k views Asked by At

I have a problem with accessing my serial port in Delphi when using the TComPort component. It can not send the data fully, but some data is lost randomly, like this example:

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

The right data should contain 10 values, like below:

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

Please help me solve this problem. I'm a beginner in Delphi.

This is my code:

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

There are 1 answers

1
MBo On

I don't see how packet mode helps you. Try an approach without packets, make OnRXChar event handler. I assume that your data contains end marker byte $A, no start marker.

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;