Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override COMMTIMEOUTS to not wake up with empty data on Windows #10

Merged
merged 1 commit into from
Oct 6, 2018

Conversation

yuja
Copy link
Contributor

@yuja yuja commented Oct 2, 2018

The serialport crate sets ReadTotalTimeoutConstant to the specified timeout
value 1, which makes the reader wake up quite often with no payload, and
confuses the tokio wrapper 2 since a 0-byte read usually means EOF.

The MSDN doc 3 is unclear how we can configure a COM port to make it behave
like a socket, but I found an example on StackOverflow 4, so tried it and
it just worked. Still the serial_printline.rs example printed "Read timed out!"
per loop, but the whole "Read would have blocked." mess went away. And my
tokio-based program worked on Windows in the same way as on Linux.

Since I'm not a Windows user, I can't say this is the best and most correct
solution. QtSerialPort appears to set ReadIntervalTimeout to MAXDWORD, but
it didn't go well. I've tested this change on VirtualBox with a pseudo serial
device connected to the host's TCP socket.

The serialport crate sets ReadTotalTimeoutConstant to the specified timeout
value [1], which makes the reader wake up quite often with no payload, and
confuses the tokio wrapper [2] since a 0-byte read usually means EOF.

 [1]: https://gitlab.com/susurrus/serialport-rs/blob/2.3.0/src/windows/com.rs#L224
 [2]: berkowski#7

The MSDN doc [3] is unclear how we can configure a COM port to make it behave
like a socket, but I found an example on StackOverflow [4], so tried it and
it just worked. Still the serial_printline.rs example printed "Read timed out!"
per loop, but the whole "Read would have blocked." mess went away. And my
tokio-based program worked on Windows in the same way as on Linux.

 [3]: https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_commtimeouts
 [4]: https://stackoverflow.com/a/26007086/10435339

Since I'm not a Windows user, I can't say this is the best and most correct
solution. QtSerialPort appears to set ReadIntervalTimeout to MAXDWORD, but
it didn't go well. I've tested this change on VirtualBox with a pseudo serial
device connected to the host's TCP socket.
@berkowski
Copy link
Owner

I'm pretty much in the same boat when it comes to the windows API. From the sources you've linked I think I can understand how the solution you've found should mimic how the unix side works. I'm inclined to say if it works for you, that's good enough for now.

@berkowski berkowski merged commit 16ba623 into berkowski:master Oct 6, 2018
@yuja
Copy link
Contributor Author

yuja commented Oct 7, 2018

Thanks!

@yuja yuja deleted the windows-timeout branch October 7, 2018 00:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants