Using Data Packets in Async Professional 2.5
When it comes right down to it, serial communications is about data
collection. Many times, the data you’re trying to collect during a
communications session is in the form of a data packet. A data packet
can be thought of as a specific sequence of bytes which proceed or
follow the actual data, combined with the data itself. Data packets come
in many different forms, and Async Professional's TApdDataPacket
component is designed to make data collection easy. If your application
is trying to monitor, filter, or process incoming data, that data will
likely be in one of the following forms.
- A specific character or string. For example, a login prompt will
often be proceeded by the characters, "login:". This is the most
common usage of the string data packet (if you have used previous
versions of APro then this form of data packet can be thought of as an
enhanced DataTrigger). To implement this type of data packet, set
TApdDataPacket's StartCond property to scString and its StartString
property to "login:" (without quotes). When this string is detected,
the OnPacket and OnStringPacket events are generated, and you can then
take whatever action your application requires.
- A bracketed (or "framed") packet. This is when the data characters
are bracketed between two known characters (e.g., <STX>‘XXXX’<ETX>).
In this case, the packet starts with the STX character (ASCII 2) and
ends with the ETX character (ASCII 3). ‘XXXX’ represents an unknown
number of characters. To detect this type of packet, set the StartCond
to scString, StartString to "#2", EndCond to [ecString] and EndString
to "#3" (without the quotes). When StartString is detected, all
following characters are saved in the TApdDataPacket component’s
buffer. When EndString is detected, the OnPacket and OnStringPacket
events are generated. The OnPacket event passes a pointer to the
bracketed characters while the OnStringPacket passes a string
containing the bracketed characters.
- A known start character or string followed by data of known
length. An example might be <STX>‘XXXX’. Here, the packet starts with
the STX character (#2) and contains a sequence of characters of four
characters (represented by ‘X’). To detect this packet, set the
StartCond to scString, StartString to "#2" (without the quotes),
EndCond to [ecPacketSize], and PacketSize to the length of the data
packet (4 in this case). When StartString is detected, the following
PacketSize characters are saved in the TApdDataPacket’s buffer. Once
the specified number of characters is received, the OnPacket and
OnStringPacket events are generated.
- A known number of characters followed by a terminating character,
e.g., ‘XXXX’<ETX>. This packet can start at any time in the
communications session, and is defined by the terminating character or
string. To detect this type of packet, set StartCond to [scAnyData],
EndCond to [ecString] and EndString to "#3". When the EndString
character or string is detected, the OnPacket and OnStringPacket
events fire. Your code must then extract the expected number of
characters from the buffer. Since all characters preceding the
EndString are placed in the TApdDataPacket component’s buffer, this
type of packet can be error-prone, especially if the packet for which
you are looking follows other data or information. In this case, you
must enable the TApdDataPacket component by setting Enabled property
to True just prior to when the data is expected.
The TApdDataPacket component provides a great way of retrieving data
from serial ports. For code examples of TApdDataPacket, see the EXWPACKT
and QRYMDM examples that come with Async Professional 2.5.