Skip to main content

Module master

Module master 

Source
Expand description

A wired M-Bus master, sans-I/O (EN 13757-2/-3): the frames a master sends, a readout that handles the frame count bit and multi-telegram replies, and a secondary-address search.

Nothing here opens a port or reads a clock. Each state machine says what to send and how long to wait for a reply (Request); you write the bytes, wait, and hand back what arrived (Readout::reply, Search::reply) or that nothing did (Readout::timeout, Search::timeout).

use mbus_kit::master::{Event, Readout, Target};

let mut readout = Readout::new(Target::Primary(5));
while let Some(request) = readout.next_request() {
    // port.write(request.bytes()); wait up to request.timeout_ms(Baud::B2400) for a frame …
    let reply: &[u8] = replies.next().unwrap();
    match readout.reply(reply) {
        Event::Telegram(app) => {
            for r in app.records().into_iter().flatten() {
                println!("{}", r.unwrap());
            }
        }
        Event::NeedMore => { /* read more bytes and call reply() again */ }
        _ => {}
    }
}
assert!(readout.is_complete()); // or readout.outcome(): why it is not

Hand reply exactly one frame (wired::scan finds it behind noise or an echo). A readout delivers only telegrams from the meter it addresses and says how it ended (Outcome): a telegram limit or a broken page is never “complete”.

Modules§

ci
CI values a master sends (EN 13757-3, m-bus.com 6.1).

Structs§

Readout
Read every telegram of one meter: SND_NKE (primary) or selection (secondary), then REQ_UD2 with the frame count bit toggled until no more records follow, then deselection (secondary). A request without a valid reply is repeated with the same FCB, RETRIES times. When it is over, Readout::outcome says whether every telegram was read, or why not.
Request
A frame to send and how long to wait for its reply.
Search
A secondary-address search (wildcard scan): finds every meter on the bus.
SecondaryAddress
A secondary address to select, each part optional (a wildcard).

Enums§

Baud
Baud rates of wired M-Bus.
Event
What happened when a reply (or its absence) was handed to a state machine.
Outcome
How a readout ended: see Readout::outcome.
Target
Whom a readout reads.

Constants§

RETRIES
The default number of repetitions of a request that got no valid reply.

Functions§

application_reset
Reset a slave’s application layer (CI 50), optionally only one telegram type (the subcode: upper nibble the type, e.g. 1x user data, lower nibble the subtelegram). Also drops a readout selection back to the standard telegram.
deselect
Deselect: SND_NKE to the network-layer address.
link_reset
SND_NKE: reset the link layer of a slave (and its FCB); to NETWORK_LAYER, deselect.
request_all_data
Ask for all data in the next REQ_UD2 (global readout request, DIF 7F).
response_timeout_ms
The response timeout at baud: 330 bit times plus 50 ms (EN 13757-2 via m-bus.com 5.4), in milliseconds rounded up: 188 ms at 2400 Bd, 85 ms at 9600 Bd.
select
Select a slave by secondary address (SND_UD to address FD, CI 52, FCB = 1 as for every selection). A slave that matches answers E5 and is then reached at NETWORK_LAYER until deselected; every other slave deselects itself.
select_enhanced
Enhanced selection: also match the fabrication number (record 0C 78, 8 BCD digits; None wildcards it). For two meters that share an identification number. A slave that does not support it does not answer.
set_baud
Switch a slave to another baud rate (CI B8–BF). The slave acknowledges at the old rate and listens at the new one.
set_primary_address
Set a slave’s primary address (SND_UD, CI 51, record 01 7A new). None for the reserved addresses 251–255. Make sure first that nothing answers at new (link_reset to it times out): two slaves on one address cannot be read.
set_time
Set a slave’s clock (CI 51, record 04 6D date and time, type F).