pub struct Telegram<'a> {
pub server_id: &'a [u8],
pub file_id: &'a [u8],
pub entries: &'a [ListEntry<'a>],
}Expand description
A simulated meter telegram.
Fields§
§server_id: &'a [u8]The meter’s server ID (10 bytes on German meters).
file_id: &'a [u8]The file ID shared by the three messages.
entries: &'a [ListEntry<'a>]The entries of the GetListResponse.
Implementations§
Source§impl Telegram<'_>
impl Telegram<'_>
Sourcepub fn write(&self, out: &mut [u8]) -> Result<usize>
pub fn write(&self, out: &mut [u8]) -> Result<usize>
Write the three messages into out. Returns the number of bytes written: the
unframed payload.
Examples found in repository?
examples/readout.rs (line 27)
11fn main() -> Result<(), sml_kit::Error> {
12 // What a basic meter pushes on its optical interface, roughly every second.
13 let entries = [
14 ListEntry::bytes(Obis::MANUFACTURER, b"SIM"),
15 ListEntry::bytes(Obis::DEVICE_ID, &[0x0A, 0x01, 0x53, 0x49, 0x4D, 0, 0, 0, 0, 1]),
16 ListEntry::unsigned(Obis::ENERGY_IMPORT, Unit::WH, -1, 123_456_789),
17 ListEntry::unsigned(Obis::ENERGY_EXPORT, Unit::WH, -1, 4_200),
18 ListEntry::number(Obis::POWER, Unit::W, 0, 1_337),
19 ];
20 let telegram = Telegram {
21 server_id: &[0x0A, 0x01, 0x53, 0x49, 0x4D, 0, 0, 0, 0, 1],
22 file_id: b"F1",
23 entries: &entries,
24 };
25
26 let mut payload = [0u8; 512];
27 let len = telegram.write(&mut payload)?;
28 let mut wire = [0u8; 600];
29 let wire_len = encode_frame(&payload[..len], &mut wire)?;
30 println!("{wire_len} bytes on the wire");
31
32 // The receiving side: bytes in, verified frames out, readings in one call.
33 let mut decoder = Decoder::new([0u8; 1024]);
34 let mut result = Ok(());
35 decoder.push_slice(&wire[..wire_len], |frame| {
36 let list = match frame.and_then(readout) {
37 Ok(list) => list,
38 Err(e) => return result = Err(e),
39 };
40 println!("meter {}", list.server_id);
41 for entry in list.entries {
42 match entry {
43 Ok(e) => match e.quantity() {
44 Some(q) => println!("{:<24} {q}", e.obis.to_string()),
45 None => println!("{:<24} {:02x?}", e.obis.to_string(), e.value),
46 },
47 Err(err) => result = Err(err),
48 }
49 }
50 });
51 result
52}Trait Implementations§
Auto Trait Implementations§
impl<'a> Forget for Telegram<'a>
impl<'a> Freeze for Telegram<'a>
impl<'a> Move for Telegram<'a>
impl<'a> RefUnwindSafe for Telegram<'a>
impl<'a> Send for Telegram<'a>
impl<'a> Sync for Telegram<'a>
impl<'a> Unpin for Telegram<'a>
impl<'a> UnsafeUnpin for Telegram<'a>
impl<'a> UnwindSafe for Telegram<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more