How does a microcontroller serial port receive data of variable length?
A UART/serial port only gives you a stream of bytes—no built-in “message length.” To receive variable-length data you add a framing rule in software (and sometimes use hardware features to help). Here are the proven patterns and how to implement them on MCUs. The Four Common Framing Strategies Delimiter-terminated (e.g., newline \n ) Sender ends each message with a unique byte/sequence ( \n , \r\n , 0x00 , etc.). Receiver collects bytes until it sees the delimiter. Pros: simple; works with text protocols. Cons: delimiter cannot appear in payload unless you escape it. Length-prefixed (binary packets) Message starts with a header that includes LEN , then exactly LEN bytes of payload (often with a checksum/CRC). Pros: fast, no searching for delimiters, great for binary. Cons: you must trust the header (add CRC + bounds checks). Start/End markers with escaping (SLIP/COBS-style) Use a start (and/or end) byte (e.g., 0x7E ), and escape th...