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 // debug
Serial.println("test"); Serial.println("test");
delay(1000); delay(2000);
} }
void check_pin(byte pin, byte index) { void check_pin(byte pin, byte index) {

View file

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

View file

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