D122 of 14
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

D12 · Lesson 2 · Frames

Why a receiver needs structure around the data, the UART's start and stop bits around every byte, packet frames with a preamble for the receiver to lock onto, a sync word chosen to stand out from noise, a length field, and finding the frame in a stream of bits

A receiver switched on in the middle of a transmission hears a stream of bits with no idea where anything begins. Which bit is the first of a byte? Is this noise or a message? Every link answers with framing. A UART (C12, D10) wraps each byte in a start bit and a stop bit. Packet radios do it once per message: a preamble of alternating bits that lets the receiver settle its gain and clock, a sync word whose pattern is unlikely in noise and marks exactly where the data begins, and a length so the receiver knows when to stop.

  • The bits of one frame carrying a single byte B: 32 preamble bits (1, 0, 1, 0, ...), the 16-bit sync word 0xD391, a length byte (here 1) and the payload byte.
  • The preamble's regular pattern is easy to lock onto but carries no information; the sync word is chosen to look nothing like the preamble or shifted copies of itself.
  • Change B: only the last 8 bits change.

Predict

A frame has 32 preamble bits, a 16-bit sync word, a length byte and a 2-byte payload. What fraction of its bits carry the payload?

Name it

Byte framing: the UART

Each byte travels as a start bit (0), eight data bits least significant first, and a stop bit (1). The falling edge of the start bit resynchronizes the receiver's clock for every byte (D10).

Packet framing

  • Preamble: alternating 1s and 0s for the receiver's gain control and clock recovery (lesson 5) to settle.
  • Sync word: a fixed pattern, here 0xD391, chosen so that it differs from the preamble and from its own shifted copies in many bit positions. The receiver slides it along the incoming bits and declares a match when at most a bit or two disagree, which tolerates noise without matching random data too often.
  • Length and payload, then a check such as a CRC (lesson 12).

False matches

A random 16-bit window matches a given sync word exactly with probability 2−16, and within one bit error with probability 17/216, about once per 3900 bit positions. Longer sync words, or requiring the CRC to pass as well, cut false frames further.

Worked example

What's the UART frame for the byte 0x4B, and how long is a frame for a 2-byte payload here?

Show the solution
  1. The byte in binary. 0x4B=01001011
  2. The UART frame. The start bit 0 comes first, then the data bits least significant first, then the stop bit 1: 0,1,1,0,1,0,0,1,0,1
  3. The packet frame. Preamble, sync word, length byte and 2 payload bytes: 32+16+8+16=72bits
  4. Its airtime at 1200 baud. t=721200s=60ms

Answer the UART sends 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, and the 2-byte packet frame is 72 bits, 60 ms at 1200 baud.

Worked example

A 16-bit sync word is searched for in random bits arriving at 1200 bit/s. How often does random data match it by chance?

Show the solution
  1. Chance at each position. Each position matches with probability 2−16.
  2. False matches per second. r=1200×2−16=0.018per second
  3. Time between them. 1r=2161200s≈55s
  4. Why the frame carries a CRC. That's why the frame also carries a length and a CRC: a false sync is caught when the CRC fails.

Answer about 0.018 false matches a second, one every 55 s or so.

Worked example

What fraction of the frame is payload for 2 bytes and for 64 bytes, with 32 bits of preamble, 16 of sync and 16 of CRC?

Show the solution
  1. Overhead bits. Preamble, sync word and CRC: 32+16+16=64bits
  2. Two bytes. 16 payload bits in a frame of 80: 1616+64=1680=20%
  3. Sixty-four bytes. 512 payload bits in a frame of 576: 512512+64=512576=89%
  4. What it means. Short messages pay mostly for overhead, which is why links bundle small messages into larger frames when they can.

Answer the payload is 20% of the frame for 2 bytes and 89% for 64 bytes.

Check

Build a frame, find its sync word in noise, and read back the payload.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A frame has 32 preamble bits, a 16-bit sync word, a length byte and a 2-byte payload. What percentage of its bits are payload?

Check

A receiver accepts a 16-bit sync word with at most one bit wrong. What's the chance that a random 16-bit window is accepted?

Check

A 16-bit sync word is searched for in random bits at 1200 bit/s.

(a)

How many times a second does random data match it?

(b)

On average, how long between false matches?

Write units the way you normally would: 1.5 s and 1500 ms are the same answer.

Check

A frame carries 16 payload bytes and 64 bits of preamble, sync, length and CRC.

(a)

What fraction of the frame is payload?

(b)

How long does it take at 1200 baud?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

A frame has a 32-bit preamble, a 16-bit sync word, an 8-bit length, 128 payload bytes and a 16-bit CRC.

(a)

What percentage of the bits are payload?

(b)

Scanning 10 million bits of random noise, about how many false matches of the sync word should the receiver expect?

Explain why

Why does a packet start with a preamble before the sync word?

Practice

Keep practicing

7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.