IoT Fundamentals · Study deck

Packet Framing

Picture a meter stream where one damaged byte makes every later reading slide into the wrong field.

Physics Phoebe is your guide for this deck.

packetframing
Physics Phoebe, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Explain: Walkthrough: From Stream to Reliable Boundaries Pick the boundary method: Fixed-length for uniform records, length-prefix for variable binary messages, or a delimiter for text and streams where resynchronization matters.
  • Explain: If delimiter, define escaping: Choose the marker, then specify how payload bytes equal to the marker, and to the escape byte itself, are escaped on send and restored on receive.
  • Explain: The practical job is to choose a boundary method, handle the cases where data could imitate a boundary, and define how large messages are split and rebuilt.
  • describe fragmentation, reassembly, and framing failure modes
iotclass.org

Major section

In 60 Seconds · Start With the Story

The receiver needs a clear way to find the next valid message.

  • JavaScript Object Notation is a text format for named values; it is shortened to JSON.
  • A payload means the part of a message that carries the application data.
  • Framing is how a receiver finds message boundaries in a continuous byte stream.
iotclass.org

Major section

Finding Message Boundaries in a Stream

Framing adds those spaces and full stops to a byte stream.

  • A communication link often delivers a continuous run of bytes with no built-in marks between messages.
  • Framing is the agreement that lets a receiver split that stream back into the discrete packets the sender intended.
  • Both work, and each carries different trade-offs.

Why it matters

A TCP application protocol may length-prefix each binary record because TCP is a stream, not a message service.

iotclass.org

Major section

Finding Message Boundaries in a Stream (continued)

Without framing, the receiver has data but no idea where one message stops and the next begins.

  • The letters are all there, but you can only read it once you agree where words begin and end.
  • In practice, the boundary rule is visible in the trace.
  • A text log protocol may use newline delimiters.
iotclass.org

Major section

Finding Message Boundaries in a Stream (continued)

You can tell the receiver how long each message is with a length field, or you can place a special delimiter marker between messages.

  • A UART sensor protocol may start with a sync byte and a length byte.
  • A BLE notification, LoRaWAN payload, or low-power serial link may choose a compact binary layout to avoid wasting bytes on repeated markers.
  • The receiver can only parse safely when that rule is explicit.
iotclass.org

Major section

Apply It: Choose and Implement a Framing Scheme

The practical job is to choose a boundary method, handle the cases where data could imitate a boundary, and define how large messages are split and rebuilt.

  • A clear, written framing rule lets two independent implementations interoperate.
  • Bound the maximum so a corrupt or hostile length cannot request an unreasonable read or buffer.

Key terms

Every frame
Every frame is the same known size.

Why it matters

Intermediate For a length-prefixed binary protocol, write the rule that prevents a corrupt length from allocating an unreasonable buffer.

Boundary strategies: count bytes with a length field, scan for a delimiter, or protect delimiter bytes with escaping.
Boundary strategies: count bytes with a length field, scan for a delimiter, or protect delimiter bytes with escaping.
iotclass.org

Major section

Apply It: Choose and Implement a Framing Scheme (continued)

That last column is the exact transformation performed in the worked example above.

  • Walkthrough: From Stream to Reliable BoundariesPick the boundary method: Fixed-length for uniform records, length-prefix for variable binary messages, or a delimiter for text and streams where resynchronization matters.
  • The result is that the flag value only ever appears as a real boundary on the wire, never inside payload data.
  • Every frame is the same known size.
iotclass.org

Major section

Apply It: Choose and Implement a Framing Scheme (continued)

If delimiter, define escaping: Choose the marker, then specify how payload bytes equal to the marker, and to the escape byte itself, are escaped on send and restored on receive.

  • A length field makes the header count authoritative but can lose synchronization when that count is corrupt.
  • Delimiters make frame edges visible and help the receiver resynchronize.
  • Wastes space for short messages and cannot carry variable data.
iotclass.org

Major section

Apply It: Choose and Implement a Framing Scheme (continued)

Byte stuffing preserves those visible edges when the payload itself contains 0x7E, at the cost of variable overhead.

  • A corrupted length desynchronizes the stream until recovery logic acts.
  • Escaping adds bytes and processing to every frame.
  • If your job is to define and implement a framing scheme, you can stop here.
iotclass.org

Major section

Under the Hood: Synchronization, Stuffing, and Reassembly · Summary

The deeper layer explains how a receiver first locks onto the bits, how stuffing keeps a marker unique, and how a corrupted boundary recovers.

  • Bit Synchronization Comes FirstBefore bytes can even be read, the receiver must lock onto the bit timing.
  • The receiver removes the stuffed bit.
  • Fragments may arrive out of order.

Key terms

Byte stuffing
Byte stuffing is the byte-oriented version of the same idea, escaping any payload byte that matches the flag or escape value.

Why it matters

The receiver uses offsets to restore byte order, but a missing range prevents delivery of the whole datagram.

Reassembly needs state: fragments share a datagram tag and size, later fragments add an offset, and the receiver buffers and orders them, delivering only if all arrive before the 60-second timeout.
Reassembly needs state: fragments share a datagram tag and size, later fragments add an offset, and the receiver buffers and orders them, delivering only if all arrive before the 60-second timeout.
iotclass.org

Deck summary

Key takeaways

The receiver needs a clear way to find the next valid message.

  • Framing adds those spaces and full stops to a byte stream.
  • Without framing, the receiver has data but no idea where one message stops and the next begins.
  • You can tell the receiver how long each message is with a length field, or you can place a special delimiter marker between messages.
  • The practical job is to choose a boundary method, handle the cases where data could imitate a boundary, and define how large messages are split and rebuilt.
iotclass.org

Retrieval practice

Recall check 1 of 4

Physics Phoebe says: answer from memory, then check your reasoning.

Q1What core problem does framing solve?

AIt hides each message payload from readers who do not hold the key
BIt marks where each message starts and ends in a continuous byte stream
CIt reduces each message payload by replacing repeated values with shorter codes
DIt verifies each message payload with a checksum before the parser reads it
Show answer

Answer: B Framing supplies the boundaries that turn an undifferentiated byte stream back into discrete messages.

iotclass.org

Retrieval practice

Recall check 2 of 4

Physics Phoebe says: answer from memory, then check your reasoning.

Q2A receiver has fragments from one message, but a middle range never arrives. Its reassembly timer expires. What should it do?

ADeliver the fragments as if the message were complete.
BKeep the buffer forever while waiting for the range.
CFill the gap with bytes from a different message.
DDiscard the incomplete message and release its buffer.
Show answer

Answer: D Without the missing range, the full message is not available; the timeout bounds memory use.

iotclass.org

Retrieval practice

Recall check 3 of 4

Physics Phoebe says: answer from memory, then check your reasoning.

Q3A delimiter-framed protocol uses a flag byte to mark boundaries, but payloads sometimes contain that exact byte value. What must the design do?

ARaise the radio transmit power so the flag byte is less likely to flip
BEscape every payload flag byte before sending, then unescape it after the frame is found
CAdd a checksum after the payload and treat the flag byte as data
DReserve that byte value for control fields and reject payloads containing it
Show answer

Answer: B Byte stuffing keeps the flag value unique to real boundaries while still allowing arbitrary payload data.

iotclass.org

Retrieval practice

Recall check 4 of 4

Physics Phoebe says: answer from memory, then check your reasoning.

Q4In a length-prefix scheme with no other protection, a single bit flips inside one frame's length field. What is the most likely consequence?

AThe receiver corrupts only one payload byte and starts the next packet normally
BThe receiver scans for a delimiter and resumes at the next marker
CThe receiver uses the checksum to repair the length before parsing payload bytes
DThe receiver reads the wrong byte count and misframes later packets until recovery realigns it
Show answer

Answer: D A wrong length shifts the next boundary, which cascades into every subsequent frame until resynchronization.

iotclass.org

Print reference

Answers

Answer key.

  1. B · Framing supplies the boundaries that turn an undifferentiated byte stream back into discrete messages.
  2. D · Without the missing range, the full message is not available; the timeout bounds memory use.
  3. B · Byte stuffing keeps the flag value unique to real boundaries while still allowing arbitrary payload data.
  4. D · A wrong length shifts the next boundary, which cascades into every subsequent frame until resynchronization.
iotclass.org