From 73718f87b997019e4c36b6f99732e1bb957eccf2 Mon Sep 17 00:00:00 2001 From: Lars Christensen Date: Wed, 1 Feb 2023 22:53:21 +0100 Subject: [PATCH] Set Windows timeouts to enforce non-blocking read Settings ReadIntervalTimeout and ReadTotalTimeoutMultiplier to MAXDWORD in the COMMTIMEOUTS structure on Windows makes Windows behave as normally expected; a read call to the serial port will return immediately if there is any data available, or if a single byte arrives in the buffer. If no data arrives, it will timeout out after the timeout specified in ReadTotalTimeoutConstant. This fixes issues where reading from a serial port would take longer than desired when less data arrives that there is room for in the buffer passed to the read function. See also: https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-commtimeouts --- src/windows/com.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/windows/com.rs b/src/windows/com.rs index 5573ab77..0985bf5d 100644 --- a/src/windows/com.rs +++ b/src/windows/com.rs @@ -10,7 +10,7 @@ use winapi::um::handleapi::*; use winapi::um::processthreadsapi::GetCurrentProcess; use winapi::um::winbase::*; use winapi::um::winnt::{ - DUPLICATE_SAME_ACCESS, FILE_ATTRIBUTE_NORMAL, GENERIC_READ, GENERIC_WRITE, HANDLE, + DUPLICATE_SAME_ACCESS, FILE_ATTRIBUTE_NORMAL, GENERIC_READ, GENERIC_WRITE, HANDLE, MAXDWORD, }; use crate::windows::dcb; @@ -243,8 +243,8 @@ impl SerialPort for COMPort { let milliseconds = timeout.as_millis(); let mut timeouts = COMMTIMEOUTS { - ReadIntervalTimeout: 0, - ReadTotalTimeoutMultiplier: 0, + ReadIntervalTimeout: MAXDWORD, + ReadTotalTimeoutMultiplier: MAXDWORD, ReadTotalTimeoutConstant: milliseconds as DWORD, WriteTotalTimeoutMultiplier: 0, WriteTotalTimeoutConstant: milliseconds as DWORD,