Home / Learn Chip Programming / Designing a Serial Protocol: Framing, CRC and Parsing

Designing a Serial Protocol: Framing, CRC and Parsing

Create a robust binary or text protocol between devices or with a PC.

In shortCreate a robust binary or text protocol between devices or with a PC.

Theory in plain words

Frame0xAA startLENCMDpayload (LEN bytes)CRC16
Frame

What it is

A good protocol has a start marker, length, command ID, payload, CRC and optional sequence number. Text protocols (AT commands, NMEA, JSON lines) are easier to debug; binary protocols are compact.

Why we use it

Every product talks to something: a PC tool, a phone or another board. Bad framing leads to lost bytes and crashes.

How it works

Use a state machine parser: wait for start byte, read length, collect payload, verify CRC, dispatch. Resynchronise on errors, add timeouts, document with a table.

Where it is used

  • PC configuration tool
  • Module to MCU link
  • Sensor network frames

Frame parser state machine

Feed bytes into the parserTry it Yourself

Garbage before the start byte is skipped. A corrupted frame is dropped and the parser resynchronises on the next start byte.

Quick quiz

1. Why a start marker?

2. Never trust...