From ef00bffabef480fd5f88cb72c4b01f6f6f7beacf Mon Sep 17 00:00:00 2001 From: finga Date: Fri, 22 Apr 2022 00:20:47 +0200 Subject: [PATCH] Parse serial input --- src/main.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 2b4638e..971348e 100644 --- a/src/main.rs +++ b/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 { + 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::()?, + input[1].parse::()?, + input[2].parse::()?, + input[3].parse::()?, + )) +} + 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())?); } }