From 954a62297db09bf190d485bb5d165687cc6af671 Mon Sep 17 00:00:00 2001 From: "N. Kartoredjo" Date: Fri, 16 Feb 2024 09:26:18 +0100 Subject: [PATCH] Fix write timeout for COM ports (#158) * Fix write timeout for COM ports * Add the fix to the changelog --------- Co-authored-by: Nando Kartoredjo --- CHANGELOG.md | 3 +++ src/windows/com.rs | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13b25a8a..e9479029 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ project adheres to [Semantic Versioning](https://semver.org/). ### Fixed * Fixes a bug where `available_ports()` returned disabled devices on Windows. [#144](https://github.com/serialport/serialport-rs/pull/144) +* Fixes a bug on Windows where the `WriteTotalTimeoutConstant` field hasn't been + configured properly when the `set_timeout` method is called. + [#124](https://github.com/serialport/serialport-rs/issues/124) ### Removed diff --git a/src/windows/com.rs b/src/windows/com.rs index 662cc196..5573ab77 100644 --- a/src/windows/com.rs +++ b/src/windows/com.rs @@ -240,14 +240,14 @@ impl SerialPort for COMPort { } fn set_timeout(&mut self, timeout: Duration) -> Result<()> { - let milliseconds = timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000; + let milliseconds = timeout.as_millis(); let mut timeouts = COMMTIMEOUTS { ReadIntervalTimeout: 0, ReadTotalTimeoutMultiplier: 0, ReadTotalTimeoutConstant: milliseconds as DWORD, WriteTotalTimeoutMultiplier: 0, - WriteTotalTimeoutConstant: 0, + WriteTotalTimeoutConstant: milliseconds as DWORD, }; if unsafe { SetCommTimeouts(self.handle, &mut timeouts) } == 0 {