M-Bus / Meter-Bus (EN 13757) frame and telegram decoder for Gengo.
gengo-mbus accepts raw M-Bus frames as hex strings and returns structured
decode results. It is transport-agnostic: serial, TCP, radio gateway, and file
input are all outside the decoder itself.
The implementation is currently a single Gengo module in mbus.gengo, with a small CLI and a test corpus.
Current capabilities:
- frame decoding for ACK, short, control, and long frames
- checksum and CRC-16 validation
- variable data telegram decoding
- fixed data telegram decoding
- compact data and format data identification with partial body export
- JSON and flat-map export helpers
- CLI for decoding a single hex payload from stdin
Current limitations:
- compact data telegrams are identified, but not expanded without a matching format template
- format data telegrams expose descriptor parsing, not full compact-template application
- encrypted / manufacturer-specific payload handling is limited
Decode a frame from a Gengo script:
mbus := import("./mbus")
std := import("std")
hex_str := "683D3D68080B7221000000B05C021B120000000C784904006402750A0001FD711E2F2F0A6620020AFB1A310502FD971D00002F2F2F2F2F2F2F2F2F2F2F2F2F2F2FDD16"
result := mbus.decode(hex_str)
std.io.println("ok:", result.ok)
if result.telegram != null {
std.io.println(mbus.telegram_to_json(result.telegram))
}
Run the CLI:
echo "683D3D68080B7221000000B05C021B120000000C784904006402750A0001FD711E2F2F0A6620020AFB1A310502FD971D00002F2F2F2F2F2F2F2F2F2F2F2F2F2F2FDD16" | ../gengo/zig-out/bin/gengo cli.gengoMain entrypoints in mbus.gengo:
decode(hex_str string) DecodeResultdecode_one(hex_str string) ?Telegramdecode_frame(hex_str string) Framedecode_telegram(frame Frame) Telegram
Export helpers:
telegram_to_json(t Telegram) stringframe_to_json(f Frame) stringdecode_result_to_json(r DecodeResult) stringtelegram_to_map(t Telegram) map[string]string
Utilities:
crc16_en13757(hex_str string) ?intdecode_status_flags(status_byte int) StatusFlags
Run all test suites:
./run_tests.shrun_tests.sh uses GENGO_BIN if set. Otherwise it falls back to a local
../gengo/zig-out/bin/gengo binary when present, then to gengo on PATH.
Important test files:
- test_all.gengo
- test_decode.gengo
- test_fixed_data.gengo
- test_crc16.gengo
- test_edge_cases.gengo
- test_export.gengo
- test_corpus2.gengo
- USAGE.md: walkthrough and examples
- ARCHITECTURE.md: internal structure and design notes
gengo-mbus/
mbus.gengo
cli.gengo
test_all.gengo
test_decode.gengo
test_fixed_data.gengo
test_crc16.gengo
test_edge_cases.gengo
test_export.gengo
test_corpus2.gengo
run_tests.sh
README.md
USAGE.md
ARCHITECTURE.md