Fine tune reading

This commit is contained in:
hodasemi 2019-11-24 08:37:26 +01:00
parent b673509380
commit 7671b4a90c
3 changed files with 11 additions and 10 deletions

View file

@ -65,7 +65,7 @@ void loop() {
// debug
Serial.println("test");
delay(1000);
delay(2000);
}
void check_pin(byte pin, byte index) {

View file

@ -4,6 +4,7 @@ use serialport::prelude::*;
use utilities::prelude::*;
use std::cell::RefCell;
use std::io;
pub use serialport::{DataBits, FlowControl, Parity, SerialPortSettings, StopBits};
pub use std::time::Duration;
@ -33,13 +34,12 @@ impl Port {
pub fn read(&self) -> VerboseResult<String> {
let mut buf: Vec<u8> = (0..255).collect();
self.serial_port
.try_borrow_mut()?
.read(&mut buf[..])
.map_err(|err| format!("failed reading serial port ({})", err))?;
Ok(String::from_utf8(buf)
.map_err(|err| format!("failed converting utf8 buffer ({})", err))?)
match self.serial_port.try_borrow_mut()?.read(&mut buf[..]) {
Ok(t) => Ok(String::from_utf8(buf[0..t].to_vec())
.map_err(|err| format!("failed converting utf8 buffer ({})", err))?),
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => Ok(String::new()),
Err(err) => create_error!(format!("failed reading serial port ({})", err)),
}
}
pub fn write(&self, msg: &str) -> VerboseResult<()> {

View file

@ -17,8 +17,9 @@ fn main() -> VerboseResult<()> {
let port = Port::open(settings)?;
loop {
if let Ok(msg) = port.read() {
println!("{}", msg);
match port.read() {
Ok(msg) => println!("{}", msg),
Err(err) => println!("{}", err),
}
}
}