Serial communication over UART delivers a raw byte stream, but most protocols need discrete packets. Framing those packets correctly is harder than it looks: plaintext encodings waste bandwidth, binary protocols with CRC and terminator bytes can still suffer false resynchronization when the delimiter byte appears inside payload data. COBS (Consistent Overhead Byte Stuffing) solves this by reserving 0x00 exclusively as a packet delimiter and encoding all zero bytes in the payload as block-length prefixes. The result is guaranteed resynchronization after any error, with worst-case overhead of just 1+ceil(N/254) bytes. The post walks through plaintext encoding options, escape schemes, binary header-data-trailer formats, framing error scenarios, and finally the COBS algorithm with a step-by-step encoder description.

26m read timeFrom embeddedrelated.com
Post cover image
Table of contents
Quick Diversion, Part I: Which of the following is the toughest area of electrical engineering?Quick Diversion, Part II: Communications can't be that hard, can it?A warning and a disclaimerFramed!Packets and streamsQuick, escape!42 69 6E 61 72 79 20 50 72 6F 74 6F 63 6F 6C 73 (Binary Protocols)Framing errorsPreventing framing errorsCOBS, at last!What's the big deal?