Class XBeePacketParser
This class reads and parses XBee packets from the input stream returning an XBeePacket which can be casted later to the corresponding high level specific API packet.
Inheritance
Inherited Members
Namespace: XBeeLibrary.Core.Packet
Assembly: XBeeLibrary.Core.dll
Syntax
public class XBeePacketParser
Remarks
All the API and API2 logic is already included so all packet reads are independent of the XBee operating mode.
Two API modes are supported and both can be enabled using the AP
(API Enable) command:
- API1 - API Without EscapesThe data frame structure is defined as follows:
Start Delimiter Length Frame Data Checksum (Byte 1) (Bytes 2-3) (Bytes 4-n) (Byte n + 1) +----------------+ +-------------------+ +--------------------------- + +----------------+ | 0x7E | | MSB | LSB | | API-specific Structure | | 1 Byte | +----------------+ +-------------------+ +----------------------------+ +----------------+ MSB = Most Significant Byte, LSB = Least Significant Byte
- API2 - API With Escapes
The data frame structure is defined as follows:
Start Delimiter Length Frame Data Checksum (Byte 1) (Bytes 2-3) (Bytes 4-n) (Byte n + 1) +----------------+ +-------------------+ +--------------------------- + +----------------+ | 0x7E | | MSB | LSB | | API-specific Structure | | 1 Byte | +----------------+ +-------------------+ +----------------------------+ +----------------+ \____________________________________________________________________/ \/ Characters Escaped If Needed MSB = Most Significant Byte, LSB = Least Significant Byte
When sending or receiving an API2 frame, specific data values must be escaped (flagged) so they do not interfere with the data frame sequencing.
To escape an interfering data byte, the byte
0x7D
is inserted before the byte to be escaped XOR'd with0x20
.The data bytes that need to be escaped:
0x7E
- Frame Delimiter (HEADER_BYTE)0x7D
- Escape (ESCAPE_BYTE)0x11
- XON (XON_BYTE)0x13
- XOFF (XOFF_BYTE)
The Length field has a two-byte value that specifies the number of bytes that will be contained in the frame data field. It does not include the checksum field.
The frame data forms an API-specific structure as follows:
Start Delimiter Length Frame Data Checksum
(Byte 1) (Bytes 2-3) (Bytes 4-n) (Byte n + 1)
+----------------+ +-------------------+ +---------------------------+ +----------------+
| 0x7E | | MSB | LSB | | API - specific Structure | | 1 Byte |
+----------------+ +-------------------+ +---------------------------+ +----------------+
/
/ API Identifier Identifier specific data
+------------------+ +------------------------------+
| cmdID | | cmdData |
+------------------+ +------------------------------+
The cmdID
frame (API-identifier) indicates which API messages will be contained in
the cmdData
frame (Identifier-specific data).
To test data integrity, a checksum is calculated and verified on non-escaped data.
Methods
ParsePacket(Byte[], OperatingMode)
Parses the bytes from the given array depending on the provided operating mode and returns the API packet.
Declaration
public XBeePacket ParsePacket(byte[] packetByteArray, OperatingMode mode)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | packetByteArray | Byte array with the complete frame, starting from the header and ending in the checksum. |
OperatingMode | mode | XBee device operating mode. |
Returns
Type | Description |
---|---|
XBeePacket | Parsed packet from the given byte array. |
Remarks
The operating mode must be API or API_ESCAPE
Exceptions
Type | Condition |
---|---|
InvalidPacketException | If there is not enough data in the array or if there is an error verifying the checksum or if the payload is invalid for the specified frame type. |
System.ArgumentException | If |
System.ArgumentNullException | If |
See Also
ParsePacket(Stream, OperatingMode)
Parses the bytes from the given inputStream
depending on the provided
mode
and returns the API packet.
Declaration
public XBeePacket ParsePacket(Stream inputStream, OperatingMode mode)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | inputStream | Input stream to read bytes from. |
OperatingMode | mode | XBee device operating mode. |
Returns
Type | Description |
---|---|
XBeePacket | Parsed packet from the input stream. |
Remarks
The operating mode must be API or API_ESCAPE.
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If |
System.ArgumentNullException | If |
InvalidPacketException | If there is not enough data in the stream or if there is an error verifying the checksum or if the payload is invalid for the specified frame type. |