Frame Codec
Decodes binary frames with a fixed layout: prefix + header + length + payload + checksum + tailer + suffix,
where every part except the payload is optional. It finds frames by their prefix, uses the length field (or a
fixed payload size) to know where they end, validates the checksum and labels each part in the message list.
When sending, it builds the frame around the payload and fills in the length and checksum.
cycbox/examples/serial_port_frame_codec_pms9103m.lua configures it for the PMS9103M particle sensor (42 4D
prefix, 16-bit length, Sum16 checksum). For formats this layout cannot express (several frame types on one line,
length fields inside a header, checksums over selected fields), use Structured Frame.
Configuration reference
| Config ID | frame_codec |
| Selected by | app_codec = "frame_codec" |
| Platforms | Linux, Windows, Android |
| License | Free |
Configurable frame-based protocol: [prefix]+[header?]+[length?]+[payload]+[checksum?]+[tailer?]+[suffix?]
Designed for binary protocols with fixed structure, supporting flexible frame format definition.
Frame Structure
A typical frame structure looks like:
[Prefix] [Header?] [Length?] [Payload] [Checksum?] [Tailer?] [Suffix?]
Configuration Options
Prefix
- Frame start identifier in hexadecimal format, space-separated
- Example:
42 4drepresents 0x42 0x4D ("BM") - Required field, used for frame synchronization
Header Size
- Number of fixed header bytes after prefix, before length field
- Does not include prefix and length field itself
- Used for protocol version, command type, etc.
Length Mode
- Fixed Size: Fixed-length frames, requires specifying fixed payload size
- U8: 1-byte length field (0-255)
- U16 Little/Big-Endian: 2-byte length field (0-65535)
- U32 Little/Big-Endian: 4-byte length field (0-4294967295)
Length Meaning
- Payload Only: Length field represents payload size only
- Payload + Checksum: Length field includes payload and checksum
- Payload + Checksum + Suffix: Length field includes payload, checksum, and suffix
Checksum Algorithm
Supports various checksum and cyclic redundancy check (CRC) algorithms:
Simple Checksums
- None: No checksum
- XOR8: 8-bit XOR checksum
- Sum8: 8-bit sum checksum (lower 8 bits)
- LRC: Longitudinal Redundancy Check
- Sum16 BE/LE: 16-bit sum checksum (big/little-endian)
CRC Checksums
- CRC-5 USB: CRC-5 for USB token packets
- CRC-7 MMC: CRC-7 for MultiMediaCard
- CRC-8 AUTOSAR: CRC-8 for AUTOSAR standard
- CRC-8 MAXIM/DOW: CRC-8 for Maxim/Dallas 1-Wire
- CRC-8 SMBUS: CRC-8 for SMBus/I²C
- CRC-15 CAN: CRC-15 for CAN bus
- CRC-16 CCITT: CRC-16 CCITT (X.25, HDLC)
- CRC-16 IBM SDLC: CRC-16 IBM SDLC/HDLC
- CRC-16 MODBUS: CRC-16 Modbus RTU
- CRC-16 PROFIBUS: CRC-16 PROFIBUS (IEC 61158-2)
- CRC-16 USB: CRC-16 for USB data packets
- CRC-16 XMODEM: CRC-16 XMODEM protocol
- CRC-17 CAN FD: CRC-17 for CAN FD
- CRC-24 BLE: CRC-24 for Bluetooth Low Energy
- CRC-32 AUTOSAR: CRC-32 for AUTOSAR standard
- CRC-32 ISO HDLC: CRC-32 ISO HDLC (Ethernet, IEEE 802.3)
- CRC-32 MPEG-2: CRC-32 for MPEG-2 transport streams
Checksum Scope
- Payload Only: Calculate checksum over payload only
- Header + Length + Payload: Calculate over fixed header, length field, and payload
- Prefix + Header + Length + Payload: Calculate over all fields including prefix
Tailer Length
- Number of fixed tailer bytes after checksum
- Used for frame end markers or other fixed data
Suffix
- Frame end identifier in hexadecimal format, space-separated
- Example:
0d 0arepresents CRLF - Optional field, located at frame end
Configuration Examples
Example 1: Simple Fixed-Length Frame
Prefix: AA 55
Header Size: 0
Length Mode: Fixed Size (32)
Checksum: None
Frame structure: AA 55 + 32 bytes payload
Example 2: Frame with Length Field and CRC16
Prefix: 42 4d
Header Size: 2
Length Mode: U16 Little-Endian
Length Meaning: Payload Only
Checksum: CRC16-IBM
Checksum Scope: Header + Length + Payload
Suffix: 0d 0a
Frame structure: 42 4d + 2-byte header + 2-byte length + N-byte payload + 2-byte CRC16 + 0d 0a
Example 3: Modbus-Style Frame
Prefix: 01
Header Size: 1
Length Mode: U8 Big-Endian
Checksum: CRC16-IBM
Checksum Scope: Prefix + Header + Length + Payload
| Setting | Key | Type | Default | Notes |
|---|---|---|---|---|
| Prefix (Hex) | frame_codec_prefix | Text | "42 4d" | Required |
| Header Size | frame_codec_header_size | Integer | 0 | Required |
| Tailer Length | frame_codec_tailer_length | Integer | 0 | Required |
| Suffix (Hex) | frame_codec_suffix | Text | "" | |
| Length Field Mode | frame_codec_length_mode | Text choice | "fixed" | Required |
| Fixed Payload Size | frame_codec_fixed_payload_size | Integer | 32 | Required; Shown when Length Field Mode (frame_codec_length_mode) = "fixed" |
| Length Field Meaning | frame_codec_length_meaning | Text choice | "payload_only" | Required |
| Checksum Algorithm | frame_codec_checksum_algo | Text choice | "none" | Required |
| Checksum Scope | frame_codec_checksum_scope | Text choice | "payload" | Required |
Setting details
Length Field Mode
Allowed values:
| Value | Label |
|---|---|
"fixed" | Fixed Size |
"u8" | U8 |
"u16_le" | U16 Little-Endian |
"u16_be" | U16 Big-Endian |
"u32_le" | U32 Little-Endian |
"u32_be" | U32 Big-Endian |
Length Field Meaning
Allowed values:
| Value | Label |
|---|---|
"payload_only" | Payload Only |
"payload_checksum" | Payload + Checksum |
"payload_tailer" | Payload + Tailer |
"payload_suffix" | Payload + Suffix |
"payload_checksum_tailer" | Payload + Checksum + Tailer |
"payload_tailer_suffix" | Payload + Tailer + Suffix |
"payload_checksum_suffix" | Payload + Checksum + Suffix |
"payload_checksum_tailer_suffix" | Payload + Checksum + Tailer + Suffix |
"header_payload" | Header + Payload |
"header_payload_checksum" | Header + Payload + Checksum |
"header_payload_checksum_tailer" | Header + Payload + Checksum + Tailer |
"header_payload_checksum_tailer_suffix" | Header + Payload + Checksum + Tailer + Suffix |
Checksum Algorithm
Allowed values:
| Value | Label |
|---|---|
"none" | None |
"xor8" | XOR-8 (XOR all bytes) |
"sum8" | Sum-8 (8-bit checksum) |
"lrc" | LRC (Longitudinal Redundancy Check) |
"sum16_be" | Sum-16 (Big-Endian) |
"sum16_le" | Sum-16 (Little-Endian) |
"crc5_usb" | CRC-5 USB |
"crc7_mmc" | CRC-7 MMC |
"crc8_autosar" | CRC-8 AUTOSAR |
"crc8_maxim_dow" | CRC-8 MAXIM/DOW |
"crc8_smbus" | CRC-8 SMBUS |
"crc15_can" | CRC-15 CAN |
"crc16_ccitt" | CRC-16 CCITT |
"crc16_ibm_sdlc" | CRC-16 IBM SDLC |
"crc16_modbus" | CRC-16 MODBUS |
"crc16_profibus" | CRC-16 PROFIBUS |
"crc16_usb" | CRC-16 USB |
"crc16_xmodem" | CRC-16 XMODEM |
"crc17_can_fd" | CRC-17 CAN FD |
"crc24_ble" | CRC-24 BLE |
"crc32_autosar" | CRC-32 AUTOSAR |
"crc32_iso_hdlc" | CRC-32 ISO HDLC |
"crc32_mpeg2" | CRC-32 MPEG-2 |
Checksum Scope
Allowed values:
| Value | Label |
|---|---|
"payload" | Payload Only |
"header_length_payload" | Header + Length + Payload |
"prefix_header_length_payload" | Prefix + Header + Length + Payload |
Example config
Default values as they appear in a saved configuration (for example the JSON block at the end of a cycbox/examples/*.lua file). Settings detected at runtime are omitted.
{
"app": {
"app_codec": "frame_codec"
},
"frame_codec": {
"frame_codec_prefix": "42 4d",
"frame_codec_header_size": 0,
"frame_codec_tailer_length": 0,
"frame_codec_suffix": "",
"frame_codec_length_mode": "fixed",
"frame_codec_fixed_payload_size": 32,
"frame_codec_length_meaning": "payload_only",
"frame_codec_checksum_algo": "none",
"frame_codec_checksum_scope": "payload"
}
}