Skip to main content

Frame

Enum Frame 

Source
pub enum Frame<'a> {
    Ack,
    Short {
        c: u8,
        a: u8,
    },
    Long {
        c: u8,
        a: u8,
        ci: u8,
        data: &'a [u8],
    },
}
Expand description

A wired M-Bus frame.

Variants§

§

Ack

E5: acknowledgement.

§

Short

A short frame: a request without data.

Fields

§c: u8

C field.

§a: u8

Primary address.

§

Long

A long (or control, when data is empty) frame.

Fields

§c: u8

C field.

§a: u8

Primary address.

§ci: u8

CI field: what the data is.

§data: &'a [u8]

The application data after the CI field.

Implementations§

Source§

impl<'a> Frame<'a>

Source

pub fn parse(buf: &'a [u8]) -> Result<Option<(Self, usize)>>

Parse one frame at the start of buf.

Returns Ok(None) when buf holds the beginning of a valid frame but not all of it yet (read more and call again), and the frame with the number of bytes it used otherwise.

Examples found in repository?
examples/readout.rs (line 15)
14fn main() -> Result<(), mbus_kit::Error> {
15    let Some((Frame::Long { ci, data, .. }, _)) = Frame::parse(&TELEGRAM)? else {
16        return Err(mbus_kit::Error::UnexpectedEnd);
17    };
18    let app = Application::parse(ci, data)?;
19    if let Header::Long(h) = app.header {
20        println!(
21            "{} meter {:08} by {}, version {}",
22            h.address.medium,
23            h.address.id_number().unwrap_or(0),
24            h.address.manufacturer,
25            h.address.version
26        );
27    }
28    if let Body::Records(records) = app.body {
29        for record in records {
30            // e.g. "volume flow · maximum · storage 5 — 0.113 m³/h"
31            println!("  {}", record?);
32        }
33    }
34    Ok(())
35}
More examples
Hide additional examples
examples/master.rs (line 56)
55    fn transfer(&mut self, request: &[u8], _timeout_ms: u32) -> Vec<u8> {
56        match Frame::parse(request) {
57            Ok(Some((Frame::Short { c: 0x40, a: 0xFD }, _))) => {
58                let was = std::mem::take(&mut self.selected);
59                if was { vec![0xE5] } else { Vec::new() }
60            }
61            Ok(Some((Frame::Short { c: 0x40, .. }, _))) => vec![0xE5],
62            Ok(Some((Frame::Long { ci: 0x52, data, .. }, _))) => {
63                // A selection matches if every ID nibble is F or ours (12345678).
64                let ours = [0x78u8, 0x56, 0x34, 0x12];
65                self.selected = data.iter().zip(ours).all(|(&p, o)| {
66                    (p >> 4 == 0xF || p >> 4 == o >> 4) && (p & 0xF == 0xF || p & 0xF == o & 0xF)
67                });
68                if self.selected { vec![0xE5] } else { Vec::new() }
69            }
70            Ok(Some((Frame::Short { a, .. }, _))) if a == 1 || (a == 0xFD && self.selected) => {
71                self.sent += 1;
72                let more = self.sent == 1;
73                let mut data = vec![0x78, 0x56, 0x34, 0x12, 0x2D, 0x2C, 0x08, 0x04, self.sent, 0, 0, 0];
74                data.extend_from_slice(&[0x04, 0x06, 0x1A, 0x92, 0x00, 0x00]); // energy 37.402 MWh
75                data.extend_from_slice(if more {
76                    &[0x1F][..]
77                } else {
78                    &[0x04, 0x13, 0xA8, 0x1B, 0x00, 0x00]
79                });
80                let mut out = [0u8; 64];
81                let n = Frame::Long { c: 0x08, a, ci: 0x72, data: &data }.encode(&mut out).unwrap();
82                out[..n].to_vec()
83            }
84            _ => Vec::new(),
85        }
86    }
Source

pub fn encode(&self, out: &mut [u8]) -> Result<usize>

Write the frame into out. Returns the number of bytes written.

Examples found in repository?
examples/master.rs (line 81)
55    fn transfer(&mut self, request: &[u8], _timeout_ms: u32) -> Vec<u8> {
56        match Frame::parse(request) {
57            Ok(Some((Frame::Short { c: 0x40, a: 0xFD }, _))) => {
58                let was = std::mem::take(&mut self.selected);
59                if was { vec![0xE5] } else { Vec::new() }
60            }
61            Ok(Some((Frame::Short { c: 0x40, .. }, _))) => vec![0xE5],
62            Ok(Some((Frame::Long { ci: 0x52, data, .. }, _))) => {
63                // A selection matches if every ID nibble is F or ours (12345678).
64                let ours = [0x78u8, 0x56, 0x34, 0x12];
65                self.selected = data.iter().zip(ours).all(|(&p, o)| {
66                    (p >> 4 == 0xF || p >> 4 == o >> 4) && (p & 0xF == 0xF || p & 0xF == o & 0xF)
67                });
68                if self.selected { vec![0xE5] } else { Vec::new() }
69            }
70            Ok(Some((Frame::Short { a, .. }, _))) if a == 1 || (a == 0xFD && self.selected) => {
71                self.sent += 1;
72                let more = self.sent == 1;
73                let mut data = vec![0x78, 0x56, 0x34, 0x12, 0x2D, 0x2C, 0x08, 0x04, self.sent, 0, 0, 0];
74                data.extend_from_slice(&[0x04, 0x06, 0x1A, 0x92, 0x00, 0x00]); // energy 37.402 MWh
75                data.extend_from_slice(if more {
76                    &[0x1F][..]
77                } else {
78                    &[0x04, 0x13, 0xA8, 0x1B, 0x00, 0x00]
79                });
80                let mut out = [0u8; 64];
81                let n = Frame::Long { c: 0x08, a, ci: 0x72, data: &data }.encode(&mut out).unwrap();
82                out[..n].to_vec()
83            }
84            _ => Vec::new(),
85        }
86    }

Trait Implementations§

Source§

impl<'a> Clone for Frame<'a>

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<'a> Copy for Frame<'a>

Source§

impl<'a> Debug for Frame<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> Eq for Frame<'a>

Source§

impl<'a> PartialEq for Frame<'a>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for Frame<'a>

Auto Trait Implementations§

§

impl<'a> Forget for Frame<'a>

§

impl<'a> Freeze for Frame<'a>

§

impl<'a> Move for Frame<'a>

§

impl<'a> RefUnwindSafe for Frame<'a>

§

impl<'a> Send for Frame<'a>

§

impl<'a> Sync for Frame<'a>

§

impl<'a> Unpin for Frame<'a>

§

impl<'a> UnsafeUnpin for Frame<'a>

§

impl<'a> UnwindSafe for Frame<'a>

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.