I downloaded and installed the comport library. I have a simulator sending data through a serial RS232 to a Selphi program. This is the next piece of code.
procedure TMainForm.ComPortRxChar(Sender: TObject; Count: Integer);
begin
ComPort.ReadStr(CPort.Str, Count);
Memo.Text := Memo.Text + CPort.Str;
end;
As for the part of the CPort library, I added:
var
Str: String;
here is the problem.
The example data that passes is roughly similar to
$HEHDT,288.45,T*1D
$HEHDT,288.46,T*18
$HEHDT,288.47,T*1A
etc. Each line is sent per second. Thus, using the above code, all this data will be displayed in a note.
However, if I changed the code to:
procedure TMainForm.ComPortRxChar(Sender: TObject; Count: Integer);
begin
ComPort.ReadStr(CPort.Str, Count);
Memo.Text := Memo.Text + CPort.Str + 'haha';
end;
This is what appears in the note:
$HEHDT,2haha88.45,T*haha1Dhaha
$HEHDT,2haha88.46,T*haha18haha
$HEHDT,2haha88.47,T*haha1Ahaha
Haha appears after 8 ASCII characters. so this means in the CPort.pas library, in the asynchronous / synchronous part, only 8ASCII characters max is assigned to the variable Str?
, , ,
Str 8 .
** , CPort . - , ? , . !
// split buffer in packets
procedure TComDataPacket.HandleBuffer;
procedure DiscardPacketToPos(Pos: Integer);
var
Str: string;
begin
FInPacket := True;
if Pos > 1 then
begin
Str := Copy(Buffer, 1, Pos - 1); // some discarded data
Buffer := Copy(Buffer, Pos, Length(Buffer) - Pos + 1);
DoDiscard(Str);
end;
end;
procedure FormPacket(CutSize: Integer);
var
Str: string;
begin
Str := Copy(Buffer, 1, CutSize);
Buffer := Copy(Buffer, CutSize + 1, Length(Buffer) - CutSize);
CheckIncludeStrings(Str);
DoPacket(Str);
end;
procedure StartPacket;
var
Found: Integer;
begin
// check for custom start condition
Found := -1;
DoCustomStart(Buffer, Found);
if Found > 0 then
DiscardPacketToPos(Found);
if Found = -1 then
begin
if Length(FStartString) > 0 then // start string valid
begin
Found := Pos(Upper(FStartString), Upper(Buffer));
if Found > 0 then
DiscardPacketToPos(Found);
end
else
FInPacket := True;
end;
end;
procedure EndPacket;
var
Found, CutSize, Len: Integer;
begin
// check for custom stop condition
Found := -1;
DoCustomStop(Buffer, Found);
if Found > 0 then
begin
// custom stop condition detected
CutSize := Found;
FInPacket := False;
end
else
if Found = -1 then
begin
Len := Length(Buffer);
if (FSize > 0) and (Len >= FSize) then
begin
// size stop condition detected
FInPacket := False;
CutSize := FSize;
end
else
begin
Len := Length(FStartString);
Found := Pos(Upper(FStopString),
Upper(Copy(Buffer, Len + 1, Length(Buffer) - Len)));
if Found > 0 then
begin
// stop string stop condition detected
CutSize := Found + Length(FStopString) + Len - 1;
FInPacket := False;
end;
end;
end;
if not FInPacket then
FormPacket(CutSize); // create packet
end;