Skip to content

Commit

Permalink
Split serial output into lines
Browse files Browse the repository at this point in the history
Also drop empty lines
  • Loading branch information
lukipuki committed Oct 24, 2023
1 parent 8ded380 commit 0869521
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl AsyncSerial {
&mut self,
command: &str,
timeout: f64,
) -> Result<Vec<u8>, AsyncSerialError> {
) -> Result<Vec<String>, AsyncSerialError> {
self.serial
.write(format!("{command}\r\n").as_bytes())
.await
Expand All @@ -42,7 +42,15 @@ impl AsyncSerial {
.await;
match result {
Ok(read_result) => match read_result {
Ok(_) => Ok(buffer),
Ok(_) => {
let buffer = String::from_utf8(buffer)
.map_err(|e| AsyncSerialError::ReadError(Box::new(e)))?;
Ok(buffer
.split("\r\n")
.filter(|line| !line.is_empty())
.map(|line| line.to_owned())
.collect())
}
Err(e) => Err(AsyncSerialError::ReadError(Box::new(e))),
},
Err(_) => Err(AsyncSerialError::Timeout(timeout)),
Expand All @@ -53,6 +61,6 @@ impl AsyncSerial {
#[tokio::main]
async fn main() {
let mut serial = AsyncSerial::new("/dev/ttyUSB2").unwrap();
let b = serial.call_with_timeout("AT+CPSI?", 1).await;
let b = serial.call_with_timeout("AT+CPSI?", 1.0).await;
println!("{:?}", b);
}

0 comments on commit 0869521

Please sign in to comment.