60 lines
1.3 KiB
Rust
60 lines
1.3 KiB
Rust
use anyhow::{bail, Error, Result};
|
|
use std::{
|
|
io::{BufRead, BufReader},
|
|
str::FromStr,
|
|
time::Duration,
|
|
};
|
|
|
|
#[derive(Debug)]
|
|
enum Speed {
|
|
Slow,
|
|
Fast,
|
|
Inst,
|
|
}
|
|
|
|
impl FromStr for Speed {
|
|
type Err = Error;
|
|
|
|
fn from_str(s: &str) -> Result<Self> {
|
|
match s {
|
|
"SLOW" => Ok(Self::Slow),
|
|
"FAST" => Ok(Self::Fast),
|
|
"INST" => Ok(Self::Inst),
|
|
_ => bail!("Could not parse speed"),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn parse(input: &str) -> Result<(usize, usize, f32, Speed)> {
|
|
let input = input
|
|
.replace("CPS, ", "")
|
|
.replace(", CPM, ", " ")
|
|
.replace(", uSv/hr, ", " ")
|
|
.replace(", ", " ");
|
|
let input: Vec<&str> = input.split(' ').collect();
|
|
|
|
Ok((
|
|
input[0].parse::<usize>()?,
|
|
input[1].parse::<usize>()?,
|
|
input[2].parse::<f32>()?,
|
|
input[3].parse::<Speed>()?,
|
|
))
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
let port_name = "/dev/serial0";
|
|
let baud_rate = 9600;
|
|
|
|
let port = serialport::new(port_name, baud_rate)
|
|
.timeout(Duration::from_millis(1000))
|
|
.open()?;
|
|
|
|
let mut port = BufReader::new(port);
|
|
loop {
|
|
let mut line = String::new();
|
|
port.read_line(&mut line)?;
|
|
println!("{:?}", line);
|
|
println!("{:?}", parse(line.trim())?);
|
|
}
|
|
}
|