Skip to main content

Readout

Struct Readout 

Source
pub struct Readout { /* private fields */ }
Expand description

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.

§What counts as a reply

Hand Readout::reply the bytes of exactly one frame. On a line with noise or a converter that echoes, cut the frame out of the receive buffer with wired::scan (as serial::Bus does). Bytes after the frame make the reply garbled: two slaves answered at once.

During a read, only a slave’s response (RSP_UD, control::is_response) from the requested primary address is a telegram (any address for 254, and for a secondary target, which answers via 253). Anything else is handled like a garbled reply: repeated, then Outcome::NoReply. Every telegram must also carry the expected identity: the one given to Readout::expect_meter, the selection of a secondary target, and the identity of the first telegram; otherwise the readout ends with Outcome::WrongMeter.

These checks attribute data to a meter; they do not authenticate it. Wired M-Bus has no authentication: any device on the line can send a frame with any address and identity.

Implementations§

Source§

impl Readout

Source

pub const fn new(target: Target) -> Self

A readout with the default retries and a limit of 16 telegrams.

Examples found in repository?
examples/master.rs (line 25)
19fn main() {
20    let mut bus = Scripted::default();
21
22    // 1. Read meter 1 completely: SND_NKE, then REQ_UD2 until no more records follow.
23    //    Only telegrams from meter 12345678 count (a swapped meter is caught).
24    let meter = SecondaryAddress::pattern("12345678").unwrap();
25    let mut readout = Readout::new(Target::Primary(1)).expect_meter(meter);
26    while let Some(request) = readout.next_request() {
27        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
28        let event = if reply.is_empty() { readout.timeout(&reply) } else { readout.reply(&reply) };
29        if let Event::Telegram(app) = event {
30            println!("telegram from {}", app.meter().map_or("?".into(), |m| m.to_string()));
31            for record in app.records().into_iter().flatten().flatten() {
32                println!("  {record}");
33            }
34        }
35    }
36    match readout.outcome() {
37        Some(Outcome::Complete) => println!("complete: {} telegrams\n", readout.telegrams()),
38        Some(other) => println!("not usable: {other}\n"),
39        None => unreachable!("the readout is over"),
40    }
41
42    // 2. Find every meter on the bus by secondary address.
43    let mut search = Search::new();
44    while let Some(request) = search.next_request() {
45        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
46        let event = if reply.is_empty() { search.timeout(&reply) } else { search.reply(&reply) };
47        match event {
48            Event::Found(meter) => println!("found {meter}"),
49            Event::Unreadable(pattern) => println!("{pattern} answers but does not name itself"),
50            _ => {}
51        }
52    }
53    println!("{} selections", search.selections());
54}
Source

pub const fn retries(self, retries: u8) -> Self

Repeat a request at most retries times.

Source

pub const fn max_telegrams(self, max: u8) -> Self

Read at most max telegrams (at least 1). A meter that still has records after them ends the readout with Outcome::LimitReached, not Outcome::Complete.

Source

pub const fn expect_meter(self, meter: SecondaryAddress) -> Self

Accept only telegrams from this meter (the identity printed on it, wildcards allowed): e.g. bind a primary address to SecondaryAddress::exact(&address). A telegram with another identity, or none, ends the readout with Outcome::WrongMeter.

Examples found in repository?
examples/master.rs (line 25)
19fn main() {
20    let mut bus = Scripted::default();
21
22    // 1. Read meter 1 completely: SND_NKE, then REQ_UD2 until no more records follow.
23    //    Only telegrams from meter 12345678 count (a swapped meter is caught).
24    let meter = SecondaryAddress::pattern("12345678").unwrap();
25    let mut readout = Readout::new(Target::Primary(1)).expect_meter(meter);
26    while let Some(request) = readout.next_request() {
27        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
28        let event = if reply.is_empty() { readout.timeout(&reply) } else { readout.reply(&reply) };
29        if let Event::Telegram(app) = event {
30            println!("telegram from {}", app.meter().map_or("?".into(), |m| m.to_string()));
31            for record in app.records().into_iter().flatten().flatten() {
32                println!("  {record}");
33            }
34        }
35    }
36    match readout.outcome() {
37        Some(Outcome::Complete) => println!("complete: {} telegrams\n", readout.telegrams()),
38        Some(other) => println!("not usable: {other}\n"),
39        None => unreachable!("the readout is over"),
40    }
41
42    // 2. Find every meter on the bus by secondary address.
43    let mut search = Search::new();
44    while let Some(request) = search.next_request() {
45        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
46        let event = if reply.is_empty() { search.timeout(&reply) } else { search.reply(&reply) };
47        match event {
48            Event::Found(meter) => println!("found {meter}"),
49            Event::Unreadable(pattern) => println!("{pattern} answers but does not name itself"),
50            _ => {}
51        }
52    }
53    println!("{} selections", search.selections());
54}
Source

pub fn next_request(&mut self) -> Option<Request>

The next frame to send, or None when the readout is over.

Examples found in repository?
examples/master.rs (line 26)
19fn main() {
20    let mut bus = Scripted::default();
21
22    // 1. Read meter 1 completely: SND_NKE, then REQ_UD2 until no more records follow.
23    //    Only telegrams from meter 12345678 count (a swapped meter is caught).
24    let meter = SecondaryAddress::pattern("12345678").unwrap();
25    let mut readout = Readout::new(Target::Primary(1)).expect_meter(meter);
26    while let Some(request) = readout.next_request() {
27        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
28        let event = if reply.is_empty() { readout.timeout(&reply) } else { readout.reply(&reply) };
29        if let Event::Telegram(app) = event {
30            println!("telegram from {}", app.meter().map_or("?".into(), |m| m.to_string()));
31            for record in app.records().into_iter().flatten().flatten() {
32                println!("  {record}");
33            }
34        }
35    }
36    match readout.outcome() {
37        Some(Outcome::Complete) => println!("complete: {} telegrams\n", readout.telegrams()),
38        Some(other) => println!("not usable: {other}\n"),
39        None => unreachable!("the readout is over"),
40    }
41
42    // 2. Find every meter on the bus by secondary address.
43    let mut search = Search::new();
44    while let Some(request) = search.next_request() {
45        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
46        let event = if reply.is_empty() { search.timeout(&reply) } else { search.reply(&reply) };
47        match event {
48            Event::Found(meter) => println!("found {meter}"),
49            Event::Unreadable(pattern) => println!("{pattern} answers but does not name itself"),
50            _ => {}
51        }
52    }
53    println!("{} selections", search.selections());
54}
Source

pub const fn outcome(&self) -> Option<Outcome>

How the readout ended; None while it runs (including the deselection).

Examples found in repository?
examples/master.rs (line 36)
19fn main() {
20    let mut bus = Scripted::default();
21
22    // 1. Read meter 1 completely: SND_NKE, then REQ_UD2 until no more records follow.
23    //    Only telegrams from meter 12345678 count (a swapped meter is caught).
24    let meter = SecondaryAddress::pattern("12345678").unwrap();
25    let mut readout = Readout::new(Target::Primary(1)).expect_meter(meter);
26    while let Some(request) = readout.next_request() {
27        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
28        let event = if reply.is_empty() { readout.timeout(&reply) } else { readout.reply(&reply) };
29        if let Event::Telegram(app) = event {
30            println!("telegram from {}", app.meter().map_or("?".into(), |m| m.to_string()));
31            for record in app.records().into_iter().flatten().flatten() {
32                println!("  {record}");
33            }
34        }
35    }
36    match readout.outcome() {
37        Some(Outcome::Complete) => println!("complete: {} telegrams\n", readout.telegrams()),
38        Some(other) => println!("not usable: {other}\n"),
39        None => unreachable!("the readout is over"),
40    }
41
42    // 2. Find every meter on the bus by secondary address.
43    let mut search = Search::new();
44    while let Some(request) = search.next_request() {
45        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
46        let event = if reply.is_empty() { search.timeout(&reply) } else { search.reply(&reply) };
47        match event {
48            Event::Found(meter) => println!("found {meter}"),
49            Event::Unreadable(pattern) => println!("{pattern} answers but does not name itself"),
50            _ => {}
51        }
52    }
53    println!("{} selections", search.selections());
54}
Source

pub const fn is_complete(&self) -> bool

Whether every telegram was read: Outcome::Complete.

Source

pub const fn telegrams(&self) -> u8

How many telegrams were delivered.

Examples found in repository?
examples/master.rs (line 37)
19fn main() {
20    let mut bus = Scripted::default();
21
22    // 1. Read meter 1 completely: SND_NKE, then REQ_UD2 until no more records follow.
23    //    Only telegrams from meter 12345678 count (a swapped meter is caught).
24    let meter = SecondaryAddress::pattern("12345678").unwrap();
25    let mut readout = Readout::new(Target::Primary(1)).expect_meter(meter);
26    while let Some(request) = readout.next_request() {
27        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
28        let event = if reply.is_empty() { readout.timeout(&reply) } else { readout.reply(&reply) };
29        if let Event::Telegram(app) = event {
30            println!("telegram from {}", app.meter().map_or("?".into(), |m| m.to_string()));
31            for record in app.records().into_iter().flatten().flatten() {
32                println!("  {record}");
33            }
34        }
35    }
36    match readout.outcome() {
37        Some(Outcome::Complete) => println!("complete: {} telegrams\n", readout.telegrams()),
38        Some(other) => println!("not usable: {other}\n"),
39        None => unreachable!("the readout is over"),
40    }
41
42    // 2. Find every meter on the bus by secondary address.
43    let mut search = Search::new();
44    while let Some(request) = search.next_request() {
45        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
46        let event = if reply.is_empty() { search.timeout(&reply) } else { search.reply(&reply) };
47        match event {
48            Event::Found(meter) => println!("found {meter}"),
49            Event::Unreadable(pattern) => println!("{pattern} answers but does not name itself"),
50            _ => {}
51        }
52    }
53    println!("{} selections", search.selections());
54}
Source

pub fn timeout(&mut self, received: &[u8]) -> Event<'static>

The timeout passed without a valid reply. received is what arrived (empty for silence): it does not change what happens next, but says what the readout heard (Heard) if it ends with Outcome::NoReply.

Examples found in repository?
examples/master.rs (line 28)
19fn main() {
20    let mut bus = Scripted::default();
21
22    // 1. Read meter 1 completely: SND_NKE, then REQ_UD2 until no more records follow.
23    //    Only telegrams from meter 12345678 count (a swapped meter is caught).
24    let meter = SecondaryAddress::pattern("12345678").unwrap();
25    let mut readout = Readout::new(Target::Primary(1)).expect_meter(meter);
26    while let Some(request) = readout.next_request() {
27        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
28        let event = if reply.is_empty() { readout.timeout(&reply) } else { readout.reply(&reply) };
29        if let Event::Telegram(app) = event {
30            println!("telegram from {}", app.meter().map_or("?".into(), |m| m.to_string()));
31            for record in app.records().into_iter().flatten().flatten() {
32                println!("  {record}");
33            }
34        }
35    }
36    match readout.outcome() {
37        Some(Outcome::Complete) => println!("complete: {} telegrams\n", readout.telegrams()),
38        Some(other) => println!("not usable: {other}\n"),
39        None => unreachable!("the readout is over"),
40    }
41
42    // 2. Find every meter on the bus by secondary address.
43    let mut search = Search::new();
44    while let Some(request) = search.next_request() {
45        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
46        let event = if reply.is_empty() { search.timeout(&reply) } else { search.reply(&reply) };
47        match event {
48            Event::Found(meter) => println!("found {meter}"),
49            Event::Unreadable(pattern) => println!("{pattern} answers but does not name itself"),
50            _ => {}
51        }
52    }
53    println!("{} selections", search.selections());
54}
Source

pub fn reply<'a>(&mut self, frame: &'a [u8]) -> Event<'a>

Hand over what arrived: the bytes of one frame (see the rules).

Examples found in repository?
examples/master.rs (line 28)
19fn main() {
20    let mut bus = Scripted::default();
21
22    // 1. Read meter 1 completely: SND_NKE, then REQ_UD2 until no more records follow.
23    //    Only telegrams from meter 12345678 count (a swapped meter is caught).
24    let meter = SecondaryAddress::pattern("12345678").unwrap();
25    let mut readout = Readout::new(Target::Primary(1)).expect_meter(meter);
26    while let Some(request) = readout.next_request() {
27        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
28        let event = if reply.is_empty() { readout.timeout(&reply) } else { readout.reply(&reply) };
29        if let Event::Telegram(app) = event {
30            println!("telegram from {}", app.meter().map_or("?".into(), |m| m.to_string()));
31            for record in app.records().into_iter().flatten().flatten() {
32                println!("  {record}");
33            }
34        }
35    }
36    match readout.outcome() {
37        Some(Outcome::Complete) => println!("complete: {} telegrams\n", readout.telegrams()),
38        Some(other) => println!("not usable: {other}\n"),
39        None => unreachable!("the readout is over"),
40    }
41
42    // 2. Find every meter on the bus by secondary address.
43    let mut search = Search::new();
44    while let Some(request) = search.next_request() {
45        let reply = bus.transfer(request.bytes(), request.timeout_ms(Baud::B2400));
46        let event = if reply.is_empty() { search.timeout(&reply) } else { search.reply(&reply) };
47        match event {
48            Event::Found(meter) => println!("found {meter}"),
49            Event::Unreadable(pattern) => println!("{pattern} answers but does not name itself"),
50            _ => {}
51        }
52    }
53    println!("{} selections", search.selections());
54}

Trait Implementations§

Source§

impl Clone for Readout

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Readout

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.