Skip to content

Framing Details

This page covers the technical details of the framing system.

Frame Components

Frames consist of two parts:

  1. Header: Determines sync bytes and addressing
  2. Payload: Determines length encoding and checksum

Header Types

Basic Header (2 bytes)

[0x90] [0x70 + PayloadType]

Two-byte sync for reliable synchronization. Recommended for most applications.

Tiny Header (1 byte)

[0x70 + PayloadType]

Single-byte sync for bandwidth-limited scenarios.

None Header (0 bytes)

(no header)

For trusted links where synchronization isn’t needed.

Payload Types

Minimal

[MSG_ID] [PAYLOAD...]

1 byte overhead. Requires message length lookup. No checksum or length field.

Default

[LENGTH] [MSG_ID] [PAYLOAD...] [CRC_HI] [CRC_LO]

4 bytes overhead. Includes length (1 byte, max 255) and Fletcher-16 checksum.

Extended

[LEN_LO] [LEN_HI] [PKG_ID] [MSG_ID] [PAYLOAD...] [CRC1] [CRC2]

6 bytes overhead. 16-bit little-endian length (max 65535 bytes) and a package-ID byte, so the full message ID is (PKG_ID << 8) | MSG_ID.

ExtendedMultiSystemStream

[SEQ] [SYS_ID] [COMP_ID] [LEN_LO] [LEN_HI] [PKG_ID] [MSG_ID] [PAYLOAD...] [CRC1] [CRC2]

9 bytes overhead. Adds a sequence number and system/component IDs for multi-node networks.

The Default payload is [LEN] [MSG_ID] [PAYLOAD...] [CRC1] [CRC2]; its CRC bytes are sum1 then sum2 of the checksum below.

Frame Profiles

Profiles combine header + payload types:

ProfileHeaderPayloadTotal Overhead
StandardBasicDefault6 bytes
SensorTinyMinimal2 bytes
IPCNoneMinimal1 byte
BulkBasicExtended8 bytes
NetworkBasicExtendedMultiSystemStream11 bytes

Checksum (Fletcher-16)

The Default and Extended payload types use Fletcher-16 checksum.

Magic Numbers:

  • Basic frame start: 0x90 followed by 0x70 + PayloadType
  • Tiny frame start: 0x70 + PayloadType
  • Payload type base value: 0x70

The checksum covers everything after the start bytes and before the CRC (for the Default payload: LENGTH, MSG_ID, PAYLOAD), followed by two per-message magic bytes (MAGIC1, MAGIC2) derived from the message layout and available in the generated code. The magic bytes are not transmitted, so a receiver needs the message definition to verify a frame:

for each byte in [frame bytes..., MAGIC1, MAGIC2]:
sum1 = (sum1 + byte) mod 256
sum2 = (sum2 + sum1) mod 256
CRC1 = sum1, CRC2 = sum2

Parser State Machine

The parser implements a state machine:

IDLE → wait for start byte(s)
HEADER → read header bytes
PAYLOAD → read length, msg_id, data
CHECKSUM → verify CRC
COMPLETE → return message

For Minimal payloads, the parser requires a message length callback to determine payload size.

Usage in C++

#include "frame_profiles.hpp"
using namespace structframe;
// Using profiles. CRC profiles need the message-info callback on the reader.
uint8_t buffer[1024];
ProfileStandardWriter writer(buffer, sizeof(buffer));
ProfileStandardAccumulatingReader reader(get_message_info);
// Encode
writer.write(msg);
send_data(writer.data(), writer.size());
// Decode (streaming)
while (receiving) {
if (auto result = reader.push_byte(read_byte())) {
handle_message(result.msg_id, result.msg_data, result.msg_len);
}
}
// Decode (buffer). try_next also returns corrupt frames (valid == false), so a
// damaged frame does not stop the loop.
reader.add_data(buffer, buffer_size);
FrameMsgInfo result;
while (reader.try_next(result)) {
if (result.valid) {
handle_message(result.msg_id, result.msg_data, result.msg_len);
}
}

Usage in Python

from structframe.frame_profiles import BufferWriter, AccumulatingReader, PROFILE_STANDARD_CONFIG
from structframe.messages import MyMessage, get_message_info
# Encode
msg = MyMessage(value=42)
writer = BufferWriter(PROFILE_STANDARD_CONFIG, 1024)
writer.write(msg)
frame = writer.data()
# Decode (byte-by-byte streaming)
reader = AccumulatingReader(PROFILE_STANDARD_CONFIG, get_message_info=get_message_info)
for byte in frame:
result = reader.push_byte(byte)
if result.valid:
handle_message(result)
# Decode (buffer mode)
reader2 = AccumulatingReader(PROFILE_STANDARD_CONFIG, get_message_info=get_message_info)
reader2.add_data(frame)
while (result := reader2.try_next()) is not None:
if result.valid:
handle_message(result)

Custom Profiles

Create custom frame formats by combining header and payload types:

using namespace structframe;
// Custom: Tiny header with Extended payload
using CustomConfig = ProfileConfig<headers::HEADER_TINY_CONFIG, payloads::PAYLOAD_EXTENDED_CONFIG>;
uint8_t buffer[1024];
BufferWriter<CustomConfig> writer(buffer, sizeof(buffer));
ProfileReader<CustomConfig> reader(mypkg::get_message_info);
from structframe.frame_profiles import ProfileConfig, HEADER_TINY_CONFIG, PAYLOAD_EXTENDED_CONFIG
from structframe.frame_profiles import BufferWriter, AccumulatingReader
custom_config = ProfileConfig(HEADER_TINY_CONFIG, PAYLOAD_EXTENDED_CONFIG, name="TinyExtended")
writer = BufferWriter(custom_config, 1024)
reader = AccumulatingReader(custom_config, get_message_info=get_message_info)