From 69529a705ee6ecebfc7530497f940720d38fbec3 Mon Sep 17 00:00:00 2001 From: Douman Date: Wed, 29 Jun 2022 02:05:06 +0900 Subject: [PATCH] win: Perform zero read size check only when timeout zero --- src/windows/com.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/windows/com.rs b/src/windows/com.rs index 616de47e..69448fcf 100644 --- a/src/windows/com.rs +++ b/src/windows/com.rs @@ -223,13 +223,17 @@ impl io::Read for COMPort { fn read(&mut self, buf: &mut [u8]) -> io::Result { let mut read_size = buf.len(); - let bytes_to_read = self.bytes_to_read()? as usize; - - if self.timeout.as_millis() == 0 && bytes_to_read < read_size { - read_size = bytes_to_read; - } - if read_size == 0 { - return Ok(0); + if self.timeout.as_secs() == 0 && self.timeout.subsec_nanos() == 0 { + //If zero timeout then make sure we can read, before proceeding + //Note that zero timeout will make read operation to wait until at least + //1 byte becomes available. + let bytes_to_read = self.bytes_to_read()? as usize; + if bytes_to_read < read_size { + read_size = bytes_to_read; + } + if read_size == 0 { + return Ok(0); + } } let evt_handle = OverlappedHandle::new()?;