Skip to main content

Crate meterbus_wired_datalink

Crate meterbus_wired_datalink 

Source
Expand description

Wired M-Bus data-link fields and frame codecs.

This crate represents and encodes the wired M-Bus frames exchanged by masters and slaves. Its wire formats and message types follow the link-layer portion of EN 13757-2:2018+A1:2023, which uses the FT1.2 format class defined by the EN 60870-5 series.

The API is suitable for M-Bus masters, meters, bridges, and diagnostic tools. It is no_std and supports encoding without dynamic allocation.

§Scope

The crate provides:

  • typed control and address fields;
  • ACK and NACK acknowledgement frames;
  • fixed-length short frames;
  • variable-format control frames without user data; and
  • variable-format long frames containing user data.

Constructors and decoders validate each frame’s structure, checksum, and supported control-field use. Address values remain representable even when they are reserved or inappropriate for a particular exchange; callers can inspect Address::kind and Address::expects_response when applying protocol rules.

This crate does not implement:

  • the wired electrical interface or UART configuration;
  • byte timing, collision handling, retries, or response timeouts;
  • master or slave state machines;
  • application-layer interpretation of the control-information byte or user data.

Those concerns belong to the surrounding physical, transport, and application layers. In particular, application data carried by a LongFrame is specified separately by EN 13757-3.

§Choosing a frame

TypeWire formTypical role
AckFrameOne byte, 0xe5Positively acknowledge an accepted request
NackFrameOne byte, 0xa2Reject an unsupported SND-UD2 request
ShortFrameFive-byte fixed formatSend SND-NKE, REQ-UD1, or REQ-UD2
ControlFrameNine-byte variable format with no user dataCarry control and control-information fields without an application payload
LongFrameVariable format with 1 to 252 user-data bytesSend or respond with application data

A frame constructor rejects a Control value that is incompatible with that frame format. CommunicationType identifies the supported communication type represented by a raw control field.

§Encoding and decoding

Every decode function consumes a slice containing exactly one complete frame. It rejects truncated input, trailing bytes, invalid delimiters, invalid lengths, checksum mismatches, and incompatible control fields as applicable to that frame type. decoder::stream identifies boundaries across arbitrary chunks and can recover from noise without allocation.

Every frame provides encode_into, which writes into a caller-owned buffer and returns only the initialized portion. When the alloc feature is enabled, frames also provide encode, which returns an allocated Vec.

§Example

The following constructs the standard five-byte shape of a REQ-UD2 request for slave address 1, encodes it without allocation, and decodes it again:

use meterbus_wired_datalink::{Address, Control, ShortFrame};

let request = ShortFrame::new(Control::req_ud2(false), Address::new(1))?;
let mut storage = [0_u8; ShortFrame::LEN];
let encoded = request.encode_into(&mut storage)?;

assert_eq!(encoded, [0x10, 0x5b, 0x01, 0x5c, 0x16]);
assert_eq!(ShortFrame::decode(encoded)?, request);

§Allocation and crate features

The crate itself always uses #![no_std].

  • the default feature set is allocation-free;
  • alloc adds allocating encode methods and the collecting decoder::stream::StreamDecoder::push convenience method.

Without alloc, encode with encode_into and stream with push_into.

A valid frame is not necessarily the right frame for the current exchange. Callers must still manage message order, timing, retries, and application data according to EN 13757-2 and EN 13757-3.

Modules§

decoder
Frame decoders.

Structs§

AckFrame
A positive acknowledgement frame.
Address
A data-link address.
Control
A data-link control field.
ControlFrame
A variable-format frame without user data.
FrameEncodeError
Error returned when a buffer cannot hold an encoded Frame.
LongFrame
A variable-format frame containing user data.
NackFrame
A negative acknowledgement frame.
ShortFrame
A fixed-length short frame.

Enums§

AckFrameError
Error produced while encoding or decoding an acknowledgement frame.
AddressKind
The protocol-defined use of a data-link address.
CommunicationType
A supported wired M-Bus communication type.
ControlError
Error produced when a control field is incompatible with a frame.
ControlFrameError
Error produced while constructing, encoding, or decoding a control frame.
Direction
Direction of a supported data-link communication.
Frame
Any supported wired M-Bus frame.
FrameKind
The wire format represented by a Frame.
LongFrameError
Error produced while constructing, encoding, or decoding a long frame.
NackFrameError
Error produced while encoding or decoding a negative acknowledgement frame.
ShortFrameError
Error produced while constructing, encoding, or decoding a short frame.