sml-kit 0.1.2

Read German smart meters in Rust: SML (Smart Message Language) decoder and encoder, sans-I/O, no_std, no allocator, no dependencies
Documentation
  • Coverage
  • 100%
    389 out of 389 items documented11 out of 112 items with examples
  • Size
  • Source code size: 225.5 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.6 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • hupe1980/sml-kit
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • hupe1980

sml-kit

Read German smart meters in Rust. sml-kit decodes SML (Smart Message Language) from a meter's optical interface or the meter bus behind a smart-meter gateway into exact readings: sans-I/O, no_std, no allocator, no dependencies.

crates.io docs.rs CI MSRV 1.85 License

Documentation · What is SML? · API reference

use sml_kit::{Decoder, Obis, readout};

// Call with every chunk of bytes from the reading head. Keep one decoder per meter
// (`Decoder::new([0; 1024])`): a frame often arrives across several reads.
fn on_bytes(decoder: &mut Decoder<[u8; 1024]>, bytes: &[u8]) {
    decoder.push_slice(bytes, |frame| {
        let Ok(list) = frame.and_then(readout) else { return };
        if let Some(a_plus) = list.quantity(Obis::ENERGY_IMPORT) {
            println!("{}: A+ = {a_plus}", list.server_id); // "1 EMH00 04321349: A+ = 15338555.7 Wh"
        }
    });
}

Install

cargo add sml-kit                                # desktop, Raspberry Pi, server
cargo add sml-kit --no-default-features          # microcontroller: no std, no alloc

To read a real meter you need an infrared reading head on its optical interface (9600 baud, 8N1) and, for power and full-precision energy, the PIN from your meter operator. What is SML? explains both.

Tested against real meters

Every CRC-valid frame of the public libsml-testing collection reads: 216 frames from EMH, ISKRA, DZG, eBZ, Holley, Itron, EasyMeter and Dr. Neuhaus meters, strictly for every meter but one. Three meters deviate from the specification; sml-kit reads them when you name the deviation:

Meter Deviation Option
Holley DTZ541 (some) CRC-16/KERMIT instead of X-25 ParseOptions::lenient()
Holley DTZ541-ZDBA, DDZ285 SML_Time without its choice tag ParseOptions::lenient()
DZG DVS74 (affected serials) 356.24 W sent as −299.12 W .with_dzg_power(true)

The capture tool tells you which one your meter needs:

cargo run --example decode -- capture.bin   # raw or hex: readings, and what your meter needs

Features

Transport v1 sans-I/O Decoder over an owned or borrowed buffer; encode_frame
Readings in one call readout, GetListResponse::quantity; exact Quantity with unit
Values OBIS (parse, names), the DLMS unit table, SML_Time, ServerId as printed on the meter
Energy direction direction, signed_power from the status word, for meters that send power without a sign
Every message body Open, Close, GetList, Get/SetProcParameter, GetProfileList/Pack, Attention: typed, read and written
Request files SmlFile: open, requests, close with transaction IDs and CRCs in one call
Blocking I/O (std) io::FrameReader over any std::io::Read
Async the same Decoder in Embassy, Tokio or any runtime, no dependency
Simulator sim::Telegram and ListEntry::{number, unsigned} for tests and simulated meters

Not supported: verifying SML signatures (they are exposed as bytes), transport version 2 (no device sends it), and IEC 62056-21 text (an older protocol).

Design

  • Sans-I/O. No ports or clocks inside: feed bytes from a reading head, a meter bus or a file.
  • no_std, no allocator, no dependencies. CI checks the Cortex-M release build for panic paths.
  • Exact. Readings are mantissa × 10^scaler with their unit, never floats: a meter reading is evidence for an invoice.
  • Strict by default. Both CRCs are checked; every deviation a real meter needs is a named option.
  • Never panics on input. Malformed, truncated and random bytes are errors; the parsers are fuzzed.
  • No secrets in logs. Request passwords print as <redacted>.

Examples

cargo run --example readout                                  # a simulated telegram
cargo run --example decode -- tests/corpus/emh-ehz-apollon77.hex
cargo run --example serial -- /dev/ttyUSB0                   # a USB reading head

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. The captures in tests/corpus/ keep their own MIT notices.

Not affiliated with the BSI or any meter manufacturer. SML is specified in BSI TR-03109-1, Annex IV.