UART Packet Parser State Machine
A sensor sends packets over UART in this format:
| Field | Size | Value |
|---|---|---|
| Start | 1 | 0xAA |
| Length | 1 | payload length, 1–16 |
| Payload | Length | data |
| Checksum | 1 | XOR of Length and every payload byte |
Write int parser_feed(uint8_t byte), called once per received byte. It returns the payload length when a valid packet completes (copying the payload into the global rx_payload), and 0 otherwise. Bad lengths and checksum errors must be dropped silently, and the parser must resynchronise on the next 0xAA.
Write your code on the right and press Run (or Ctrl + Enter). Hidden tests check your functions and print the result below the editor.
Use an enum of states: waiting for start, waiting for length, receiving payload, waiting for checksum. Keep the running XOR and the index as static variables.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
uint8_t rx_payload[16];
typedef enum { ST_START, ST_LEN, ST_DATA, ST_CSUM } pstate_t;
int parser_feed(uint8_t byte)
{
static pstate_t st = ST_START;
static uint8_t len, idx, csum;
switch (st) {
case ST_START:
if (byte == 0xAA) st = ST_LEN;
break;
case ST_LEN:
if (byte >= 1 && byte <= sizeof rx_payload) {
len = byte; idx = 0; csum = byte;
st = ST_DATA;
} else {
st = (byte == 0xAA) ? ST_LEN : ST_START; /* resync */
}
break;
case ST_DATA:
rx_payload[idx++] = byte;
csum ^= byte;
if (idx == len) st = ST_CSUM;
break;
case ST_CSUM:
st = ST_START;
if (byte == csum) return len;
break;
}
return 0;
}
A byte-at-a-time state machine never blocks, works directly from a UART ISR or ring buffer, and handles packets split across reads. Validating the length before storing anything prevents a buffer overflow from a corrupted length byte, which is a classic security bug.
Improvements for production: copy the payload to the application only after the checksum passes (here a failing packet partially overwrites rx_payload), add an inter-byte timeout to reset the state, and use a CRC instead of XOR. Because 0xAA can appear in the payload, real protocols often use byte stuffing (SLIP, COBS, HDLC) so the start marker is unique.
Show the hidden tests
/* ---- tests ---- */
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0)
static int feed_all(const uint8_t *b, size_t n)
{
int last = 0;
for (size_t i = 0; i < n; i++) { int r = parser_feed(b[i]); if (r) last = r; }
return last;
}
int main(void)
{
const uint8_t ok[] = { 0xAA, 3, 0x10, 0x20, 0x30, 3 ^ 0x10 ^ 0x20 ^ 0x30 };
CHECK(feed_all(ok, sizeof ok) == 3 && memcmp(rx_payload, ok + 2, 3) == 0);
const uint8_t bad[] = { 0xAA, 2, 0x01, 0x02, 0x00 }; /* wrong checksum */
CHECK(feed_all(bad, sizeof bad) == 0);
const uint8_t junk[] = { 0x00, 0x55, 0xAA, 0x40, 0xAA, 1, 0x7E, 1 ^ 0x7E }; /* bad len, then resync */
CHECK(feed_all(junk, sizeof junk) == 1 && rx_payload[0] == 0x7E);
const uint8_t aa_in_payload[] = { 0xAA, 2, 0xAA, 0xAA, 2 };
CHECK(feed_all(aa_in_payload, sizeof aa_in_payload) == 2);
puts(fails ? "Some tests failed" : "All tests passed");
return fails != 0;
}