Structured Frame Codec
Decodes and builds binary frames described by a JSON schema instead of fixed form fields. A schema lists the
fields in order, each with a kind (const, u8, u16le/u16be and wider integers, bytes, string, …) and an
optional role: sync marks the constant that frames start with, payload the bytes surfaced as the message
payload, checksum a checksum with its algo and scope. A field can take its size from an earlier length
field (size_from, size_adjust), which is then filled in automatically when sending.
A schema may declare several variants for devices that send more than one frame format on the same line; the
decoder picks the one whose sync bytes match first. Decoded fields become values, so they can be charted without
Lua. Start from a preset in the form, or see cycbox/examples/structured_frame_codec_pms9013m.lua.
Configuration reference
| Config ID | structured_frame_codec |
| Selected by | app_codec = "structured_frame_codec" |
| Platforms | Linux, Windows, Android |
| License | Free |
Declarative JSON-configured frame codec for diverse IoT/embedded protocols.
Select a preset or edit the JSON schema directly. Each field declares kind, role (sync/length/checksum/data), and optional references (size_from).
| Setting | Key | Type | Default | Notes |
|---|---|---|---|---|
| Schema (JSON) | structured_frame_codec_schema | Code | "" | Required |
Setting details
Schema (JSON)
Frame schema — ordered list of fields with their kinds, roles, and references.
Allowed values:
| Value | Label |
|---|---|
"{\n \"schema_version\": 1,\n \"variants\": [\n {\n \"name\": \"default\",\n \"description\": \"Generic 0x55 / CRLF framed protocol\",\n \"fields\": [\n { \"name\": \"sync\", \"kind\": \"const\", \"bytes\": \"55\", \"role\": \"sync\", \"description\": \"Frame start marker\" },\n { \"name\": \"seq\", \"kind\": \"u8\", \"description\": \"Sequence number\" },\n { \"name\": \"cmd\", \"kind\": \"u8\", \"description\": \"Command code\" },\n { \"name\": \"length\", \"kind\": \"u16le\", \"description\": \"Bytes in data + CRC\" },\n { \"name\": \"data\", \"kind\": \"bytes\", \"role\": \"payload\",\n \"size_from\": \"length\", \"size_adjust\": -2,\n \"description\": \"Payload (length includes the trailing CRC)\" },\n { \"name\": \"crc\", \"kind\": \"u16le\", \"role\": \"checksum\",\n \"algo\": \"crc16_modbus\",\n \"scope\": [\"seq\", \"cmd\", \"length\", \"data\"],\n \"description\": \"CRC-16 over seq+cmd+length+data\" },\n { \"name\": \"eof\", \"kind\": \"const\", \"bytes\": \"0d 0a\", \"description\": \"CRLF terminator\" }\n ]\n }\n ]\n}\n" | Generic 55 Crlf |
"{\n \"schema_version\": 1,\n \"variants\": [\n {\n \"name\": \"measurement\",\n \"direction\": \"rx\",\n \"description\": \"PMS9103M / PM2S-3 32-byte measurement data frame (device → host)\",\n \"fields\": [\n { \"name\": \"prefix\", \"kind\": \"const\", \"bytes\": \"42 4d\", \"role\": \"sync\",\n \"description\": \"Start marker 'BM'\" },\n { \"name\": \"length\", \"kind\": \"u16be\",\n \"description\": \"Frame length = remaining bytes including checksum (fixed 0x001C = 28)\" },\n { \"name\": \"data\", \"kind\": \"bytes\", \"role\": \"payload\",\n \"size_from\": \"length\", \"size_adjust\": -2,\n \"description\": \"Measurement payload — 26 bytes: 6×PM concentrations, 6×particle counts, version, error code\" },\n { \"name\": \"checksum\", \"kind\": \"u16be\", \"role\": \"checksum\",\n \"algo\": \"sum16_be\",\n \"scope\": [\"prefix\", \"length\", \"data\"],\n \"description\": \"Sum16 (BE) over prefix + length + data\" }\n ]\n },\n {\n \"name\": \"command\",\n \"direction\": \"tx\",\n \"description\": \"Host → device 7-byte command frame (Read Data, Set Mode, Standby, TSI Curve)\",\n \"fields\": [\n { \"name\": \"prefix\", \"kind\": \"const\", \"bytes\": \"42 4d\", \"role\": \"sync\",\n \"description\": \"Start marker 'BM'\" },\n { \"name\": \"cmd\", \"kind\": \"u8\", \"default\": \"0xe2\",\n \"description\": \"Command opcode: 0xE2 Read Data, 0xE1 Set Mode, 0xE4 Standby, 0x52 TSI Curve\" },\n { \"name\": \"data_high\", \"kind\": \"u8\", \"default\": \"0x00\",\n \"description\": \"Parameter high byte (typically 0x00)\" },\n { \"name\": \"data_low\", \"kind\": \"u8\", \"default\": \"0x00\",\n \"description\": \"Parameter low byte (mode/standby flag or TSI curve selector)\" },\n { \"name\": \"checksum\", \"kind\": \"u16be\", \"role\": \"checksum\",\n \"algo\": \"sum16_be\",\n \"scope\": [\"prefix\", \"cmd\", \"data_high\", \"data_low\"],\n \"description\": \"Sum16 (BE) over prefix + cmd + data_high + data_low\" }\n ]\n },\n {\n \"name\": \"ack\",\n \"direction\": \"rx\",\n \"description\": \"Device → host 8-byte command acknowledgment frame\",\n \"fields\": [\n { \"name\": \"prefix\", \"kind\": \"const\", \"bytes\": \"42 4d\", \"role\": \"sync\",\n \"description\": \"Start marker 'BM'\" },\n { \"name\": \"length\", \"kind\": \"u16be\",\n \"description\": \"Frame length including checksum (fixed 0x0004 = 4)\" },\n { \"name\": \"data\", \"kind\": \"bytes\", \"role\": \"payload\",\n \"size_from\": \"length\", \"size_adjust\": -2,\n \"description\": \"Ack payload — 2 bytes: command echo + status\" },\n { \"name\": \"checksum\", \"kind\": \"u16be\", \"role\": \"checksum\",\n \"algo\": \"sum16_be\",\n \"scope\": [\"prefix\", \"length\", \"data\"],\n \"description\": \"Sum16 (BE) over prefix + length + data\" }\n ]\n }\n ]\n}\n" | Pms9103m |
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": "structured_frame_codec"
},
"structured_frame_codec": {
"structured_frame_codec_schema": ""
}
}