Skip to content

Commit

Permalink
Tweak windows COMMTIMEOUTS settings to match
Browse files Browse the repository at this point in the history
the normal developer experience which is mostly
POSIX-style these days

See ticket de-vri-es#7 for discussion
  • Loading branch information
DanielJoyce committed Aug 25, 2023
1 parent d396c38 commit 82bf38f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use winapi::shared::minwindef::{BOOL, HKEY};
use winapi::shared::winerror;
use winapi::um::{commapi, fileapi, handleapi, ioapiset, minwinbase, synchapi, winbase, winnt, winreg};

const DEFAULT_TIMEOUT: u32 = 10;

pub struct SerialPort {
pub file: std::fs::File,
}
Expand Down Expand Up @@ -37,8 +39,17 @@ impl SerialPort {
unsafe {
let mut timeouts: winbase::COMMTIMEOUTS = std::mem::zeroed();
check_bool(commapi::GetCommTimeouts(file.as_raw_handle(), &mut timeouts))?;
timeouts.ReadIntervalTimeout = 10;
check_bool(commapi::SetCommTimeouts(file.as_raw_handle(), &mut timeouts))?;
// How to set the COMM timeouts so the behavior is a bit more sane
// https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-commtimeouts#remarks
// TODO: Expose these as configuration options on Windows
timeouts.ReadIntervalTimeout = std::u32::MAX;
timeouts.ReadTotalTimeoutMultiplier = std::u32::MAX;
// Default timeout is 10ms
timeouts.ReadTotalTimeoutConstant = DEFAULT_TIMEOUT;
// Set write timeouts
timeouts.WriteTotalTimeoutMultiplier = std::u32::MAX;
timeouts.WriteTotalTimeoutConstant = DEFAULT_TIMEOUT;
timeouts.check_bool(commapi::SetCommTimeouts(file.as_raw_handle(), &mut timeouts))?;
}
Ok(Self::from_file(file))
}
Expand Down Expand Up @@ -66,8 +77,8 @@ impl SerialPort {
unsafe {
let mut timeouts = std::mem::zeroed();
check_bool(commapi::GetCommTimeouts(self.file.as_raw_handle(), &mut timeouts))?;
timeouts.ReadIntervalTimeout = 0;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.ReadIntervalTimeout = std::u32::MAX;
timeouts.ReadTotalTimeoutMultiplier = std::u32::MAX;
timeouts.ReadTotalTimeoutConstant = timeout.as_millis().try_into().unwrap_or(u32::MAX);
check_bool(commapi::SetCommTimeouts(self.file.as_raw_handle(), &mut timeouts))
}
Expand All @@ -85,7 +96,7 @@ impl SerialPort {
unsafe {
let mut timeouts = std::mem::zeroed();
check_bool(commapi::GetCommTimeouts(self.file.as_raw_handle(), &mut timeouts))?;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutMultiplier = std::u32::MAX;
timeouts.WriteTotalTimeoutConstant = timeout.as_millis().try_into().unwrap_or(u32::MAX);
check_bool(commapi::SetCommTimeouts(self.file.as_raw_handle(), &mut timeouts))
}
Expand Down

0 comments on commit 82bf38f

Please sign in to comment.