Skip to content

Commit 69529a7

Browse files
committed
win: Perform zero read size check only when timeout zero
1 parent 656ed51 commit 69529a7

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/windows/com.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,17 @@ impl io::Read for COMPort {
223223
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
224224
let mut read_size = buf.len();
225225

226-
let bytes_to_read = self.bytes_to_read()? as usize;
227-
228-
if self.timeout.as_millis() == 0 && bytes_to_read < read_size {
229-
read_size = bytes_to_read;
230-
}
231-
if read_size == 0 {
232-
return Ok(0);
226+
if self.timeout.as_secs() == 0 && self.timeout.subsec_nanos() == 0 {
227+
//If zero timeout then make sure we can read, before proceeding
228+
//Note that zero timeout will make read operation to wait until at least
229+
//1 byte becomes available.
230+
let bytes_to_read = self.bytes_to_read()? as usize;
231+
if bytes_to_read < read_size {
232+
read_size = bytes_to_read;
233+
}
234+
if read_size == 0 {
235+
return Ok(0);
236+
}
233237
}
234238

235239
let evt_handle = OverlappedHandle::new()?;

0 commit comments

Comments
 (0)