Skip to main content

readout

Function readout 

Source
pub fn readout(payload: &[u8]) -> Result<GetListResponse<'_>>
Expand description

The readings in one transport payload, strictly: every message must parse, and the first GetListResponse is returned. Error::NoReadings if there is none.

For a meter that deviates from the specification, use readout_with.

Examples found in repository?
examples/serial.rs (line 38)
18fn main() -> std::io::Result<()> {
19    let source: Box<dyn Read> = match std::env::args().nth(1) {
20        Some(path) => Box::new(std::fs::File::open(path)?),
21        None => Box::new(std::io::stdin()),
22    };
23    for frame in FrameReader::new(source) {
24        let frame = match frame {
25            Ok(frame) => frame,
26            Err(ReadError::Frame(e)) => {
27                eprintln!("skipped a damaged frame: {e}");
28                continue;
29            }
30            // A quiet line: keep reading.
31            Err(ReadError::Io(e))
32                if matches!(e.kind(), std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock) =>
33            {
34                continue;
35            }
36            Err(ReadError::Io(e)) => return Err(e),
37        };
38        match readout(&frame) {
39            Ok(list) => {
40                let show = |obis| list.quantity(obis).map_or_else(|| "-".to_owned(), |q| q.to_string());
41                println!(
42                    "{}  A+ {}  A- {}  P {}",
43                    list.server_id,
44                    show(Obis::ENERGY_IMPORT),
45                    show(Obis::ENERGY_EXPORT),
46                    show(Obis::POWER)
47                );
48            }
49            Err(e) => eprintln!("no readings: {e} (try `cargo run --example decode` on a capture)"),
50        }
51    }
52    Ok(())
53}