This commit is contained in:
parent
70d55e909d
commit
ef00bffabe
1 changed files with 39 additions and 1 deletions
40
src/main.rs
40
src/main.rs
|
@ -1,9 +1,46 @@
|
|||
use anyhow::Result;
|
||||
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;
|
||||
|
@ -17,5 +54,6 @@ fn main() -> Result<()> {
|
|||
let mut line = String::new();
|
||||
port.read_line(&mut line)?;
|
||||
println!("{:?}", line);
|
||||
println!("{:?}", parse(line.trim())?);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue