Skip to content

Commit

Permalink
Set Windows timeouts to enforce non-blocking read
Browse files Browse the repository at this point in the history
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
  • Loading branch information
larsch authored and sirhcel committed Jun 26, 2024
1 parent 0a75184 commit 73718f8
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/windows/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 73718f8

Please sign in to comment.