match payload v.s. sent bytes length in Serial::send_bytes

This commit is contained in:
Vladan Popovic 2022-07-20 12:20:17 +02:00
parent a01b6d3a7b
commit db3cd1548e
1 changed files with 11 additions and 5 deletions

View File

@ -8,7 +8,7 @@ use esp_idf_hal::serial::{self, Rx, Tx};
#[derive(Debug)]
pub enum SerialError {
ReadError,
WriteError(String),
WriteError,
TimeoutError,
}
@ -79,13 +79,19 @@ impl<UART: serial::Uart> SerialIO<UART> {
}
pub fn send_bytes(&mut self, payload: &[u8], eos: Option<u8>) -> Result<usize> {
//self.rx.clear();
let mut num_bytes = 0;
for b in payload.iter() {
nb::block!(self.tx.write(*b))
.map_err(|err| SerialError::WriteError(format!("Error writing '{}' to serial, Original error {}", b, err)))?;
.map_err(|err| SerialError::WriteError)?;
num_bytes += 1;
}
if num_bytes == payload.len() {
eos.map(|b| nb::block!(self.tx.write(b)));
Ok(num_bytes + if eos.is_none() { 0 } else { 1 })
}
else {
Err(SerialError::WriteError)
}
eos.map(|b| nb::block!(self.tx.write(b)));
Ok(payload.len() + if eos.is_none() { 0 } else { 1 })
}
/// Reads a whole line (that ends with \\n) within the given `timeout` passed on input.