Parse serial input
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
finga 2022-04-22 00:20:47 +02:00
parent 70d55e909d
commit ef00bffabe

View file

@ -1,9 +1,46 @@
use anyhow::Result; use anyhow::{bail, Error, Result};
use std::{ use std::{
io::{BufRead, BufReader}, io::{BufRead, BufReader},
str::FromStr,
time::Duration, 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<()> { fn main() -> Result<()> {
let port_name = "/dev/serial0"; let port_name = "/dev/serial0";
let baud_rate = 9600; let baud_rate = 9600;
@ -17,5 +54,6 @@ fn main() -> Result<()> {
let mut line = String::new(); let mut line = String::new();
port.read_line(&mut line)?; port.read_line(&mut line)?;
println!("{:?}", line); println!("{:?}", line);
println!("{:?}", parse(line.trim())?);
} }
} }