hello,
Reading data in packets
About packets
When application is connected to some sort of serial device like data loggers which constantly send
data to PC, data is usually sent in packets. Packet is a string of characters, usually of constant size,
with some start and/or stop characters. Application can parse incoming data inside OnRxChar
event handler, but its much easier to use TComDataPacket component, which does parsing process
automatically.
One TCustomComPort can have more than one TComDataPacket components linked, so
application can easily recieve packets of different sizes and forms.
When packet is formed OnPacket event is triggered with packet string as parameter. Any data that
is discarded during the process of packet forming goes through OnDiscard event.
If TComDataPacket component is linked to TCustomComPort component, OnRxChar event of
TCustomComPort is not triggered, so application has to use OnRxBuf event handler if it also needs
unparsed incoming data.
Start and stop conditions
Application sets up packet by setting start and stop conditions. This is accomplished by setting
StartString, StopString and Size properties of TComDataPacket component.
If StartString property is not empty, packet starts when StartString string has arrived in input buffer.
If StartString is empty, packet starts immidiately when any charachter arrives in input buffer.
Stop condition is defined by StopString and Size property. If Size is 0, size stop condition is
ignored. If Size is larger than 0, packet is ended when packet size reaches Size charachters. If
StopString is not empty, packet stops when StopString string arrives in input buffer. If Size is larger
than 0 and StopString is not empty, packet is ended when size of packet reaches Size charachters or
StopString string arrives in input buffer. If Size is 0 and StopString is 0 then packet is not defined
and OnPacket has the same effect as OnRxBuf event handler.
Example
Some PBX device sends data to PC in packets, starting with STX (start-of-text) charachter and
ending with ETX (end-of-text) charachter. Before opening serial port, application should set up start
and stop condition like this:
begin
// set start and stop condition for packet
ComDataPacket1.StartString := #2;
ComDataPacket1.StopString := #3;
// now open the port
ComPort1.Open;
end;
Note
ComDataPacket1 has to be linked to ComPort1 component.
Custom packets
Some packets have specific form and sometimes application can't use standard start and stop
conditions. To form a custom packet, application has write OnCustomStart and/or OnCustomStop
event handler of TComDataPacket component. If OnCustomStart event handler is assigned,
StartString start condition is ignored and it is defined inside event handler. Simillary, if
OnCustomStop event handler is assigned, StopString and Size stop conditions are ignored and
defined in OnCustomStop event handler.
Example
Data from serial device is sent to PC in the following format: #packet_size#packet_data
where packet_size is size of packet including header and packet_data is raw data.
var
CurrentPacketSize: Integer;
procedure ComDataPacket1CustomStart(Sender: TObject; const Str: string; var Pos: Integer);
var
P1, P2: Integer;
begin
Pos := 0; // no start position yet
P1 := System.Pos('#', Str);
if P1 = 0 then
Exit; // no start charachter found
P2 := System.Pos('#', Copy(Str, P1 + 1, Length(Str) – P1)); // second # charachter
if P2 = 0 then
Exit; // no second start charachter found
P2 := P2 + P1;
Pos := P1; // start position of packet defined at first # charachter
try
// determine packet size
CurrentPacketSize := StrToInt(Copy(Str, P1 + 1, P2 – P1 - 1));
except
CurrentPacketSize := P2 – P1 + 1; // packet size is corrupted, cut only #…# part
end;
end;
procedure ComDataPacket1CustomStop(Sender: TObject; const Str: string; var Pos: Integer);
begin
if CurrentPacketSize <= Length(Str) then
Pos := CurrentPacketSize; // set stop position
end;
Friendly J.P