Skip to content

Commit

Permalink
Ensure usb buffers are cleared between api calls
Browse files Browse the repository at this point in the history
If the previous request was not completely processed, there is a possibility that the response from the old request was read instead of the response from the new request.
  • Loading branch information
hduelme authored and becky112358 committed Oct 9, 2024
1 parent cf8ce20 commit e142d11
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/port.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;

use serialport::{Error, ErrorKind, Result, SerialPort, SerialPortType};
use serialport::{ClearBuffer, Error, ErrorKind, Result, SerialPort, SerialPortType};

use crate::common::MESSAGE_LENGTH;

Expand Down Expand Up @@ -55,7 +55,10 @@ pub fn connect() -> Result<Box<dyn SerialPort>> {
.timeout(Duration::from_secs(TIMEOUT_S))
.open()
{
Ok(port) => return Ok(port),
Ok(port) => {
port.clear(ClearBuffer::All)?;
return Ok(port)
},
Err(err) => result = err,
}
}
Expand Down

0 comments on commit e142d11

Please sign in to comment.