#converter #parser #translator

ve_direct

Victron Energy Direct protocol parser and units converter

8 releases

0.1.7 Sep 15, 2022
0.1.6 Sep 15, 2022

#2132 in Parser implementations

MIT license

68KB
1.5K SLoC

VE.Direct

Victron Energy Direct protocol parser and units converter

Parser takes raw input from uart serial port and outputs parsed map of fields, then it is passed through to converter for standardization and translation


lib.rs:

Victron Energy Direct protocol parser and converter. Project provides parser for protocol packets and converter for standardizing units and translation of the packet

Example

use tokio::io::AsyncReadExt;
use tokio_serial::SerialPortBuilderExt;
use std::time::Duration;
use crate::converter::convert;
use crate::parser::Parser;

#[tokio::main]
async fn main() -> tokio_serial::Result<()> {
// initialize serial port
    let mut port = tokio_serial::new("/dev/serial0", 19200)
        .timeout(Duration::from_secs(5))
        .open_native_async()?;

    // initialize buffer and parser
    let mut buf: Vec<u8> = vec![0; 2048];
    let mut parser = Parser::new();

    loop{
        // read loop
        if let Ok(r) = port.read(&mut buf).await {
            // data from serial port are served in chunks so it takes couple loops to get one packet parsed
            if let Ok(parsed) = parser.parse_slice(&buf[..r]) {
                // when it is parsed do conversion
                println!("{:?}", convert(parsed));
            }
        }
    }
}

Dependencies

~2.5–3.5MB
~68K SLoC