@@ -61,6 +61,11 @@ constexpr auto DELAY_BEFORE_READ_WRITE = std::chrono::milliseconds(200);
6161// if opening a UART port right after close.
6262constexpr auto DELAY_BEFORE_OPEN = std::chrono::milliseconds(200 );
6363
64+ #if defined(_WIN32)
65+ // Wait for 20ms for data before returning from a read
66+ constexpr auto readTotalTimeoutConstant = 20 ;
67+ #endif
68+
6469struct UartTransport ::impl : Transport
6570{
6671 std::array<uint8_t , UartTransportBufferSize> readBuffer;
@@ -283,6 +288,26 @@ struct UartTransport::impl : Transport
283288 }
284289#endif
285290
291+ #if defined(_WIN32)
292+ ::COMMTIMEOUTS timeouts;
293+
294+ // See
295+ // https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-commtimeouts
296+ // for documentation of these parameters
297+ timeouts.ReadIntervalTimeout = MAXDWORD;
298+ timeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
299+ timeouts.ReadTotalTimeoutConstant = readTotalTimeoutConstant;
300+
301+ timeouts.WriteTotalTimeoutMultiplier = 0 ;
302+ timeouts.WriteTotalTimeoutConstant = 0 ;
303+
304+ if (!::SetCommTimeouts (serialPort->native_handle (), &timeouts))
305+ {
306+ const auto error = std::error_code (errno, std::system_category ());
307+ throw std::system_error (error, " Failed to set communication timeout parameters" );
308+ }
309+ #endif
310+
286311 // Wait a bit before making the device available since there are problems
287312 // if data is sent right after open.
288313 //
0 commit comments