8 个版本
0.1.7 | 2022 年 9 月 15 日 |
---|---|
0.1.6 | 2022 年 9 月 15 日 |
#2132 in 解析器实现
68KB
1.5K SLoC
VE.Direct
Victron Energy Direct 协议解析器和单位转换器
解析器从 UART 串行端口获取原始输入,并输出字段的解析映射,然后将其传递给转换器进行标准化和翻译
lib.rs
:
Victron Energy Direct 协议解析器和转换器。该项目提供协议数据包的解析器和标准化单位和数据包翻译的转换器
示例
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));
}
}
}
}
依赖关系
~2.5–3.5MB
~68K SLoC