This protocol defines a structured way to send and receive data over UART. Each packet contains a start byte, length field, identifier, payload, checksum, and end byte.
Each packet follows this format:
[ Start Byte | ID | Length | Payload | Checksum | End Byte ]
| Field | Size (bytes) | Description |
|---|---|---|
| Start Byte | 1 | Marks the beginning of a packet (0x7E) |
| ID | 1 | Identifies the type of packet |
| Length | 1 | Number of bytes in payload |
| Payload | Variable | Data being transmitted |
| Checksum | 1 | XOR of all bytes from ID to Payload (error detection) |
| End Byte | 1 | Marks the end of the packet (0x7F) |
- Start with
0x7Eas the Start Byte. - Add an ID field for identifying packet type.
- Compute the Length field (total bytes of payload).
- Insert the Payload.
- Compute the Checksum as an XOR of all bytes from
LengthtoPayload. - Byte stuff everything except the Start Byte and End Byte.
- Append the End Byte.
- Wait for a Start Byte.
- When reading bytes, make sure to unstuff them.
- Read the Length field to determine expected bytes.
- Extract ID, Payload, and Checksum.
- Verify the Checksum (if incorrect, discard packet).
- Check for the End Byte.
If the start byte (0x7E) or end byte (0x7F) appears in the payload, replace it with an escape byte (0x7D) followed by a modified version of the byte.
The modified byte is obtained by XORing the original byte with 0x20.
If an escape byte (0x7D) is detected, take the next byte and XOR it with 0x20 to recover the original value.
- Checksum Mismatch: Packet is discarded.
- Invalid Length Field: Ignore the packet.
- Missing End Byte (if expected): Timeout or resync.
To compile the tests, execute the following command from the root of the project:
mkdir build_local
cd build_local
cmake .. -DENABLE_TESTS=ON
makeTo run the tests, execute the following command from the build directory of the project:
./com_client/tests/test_com_client