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

Set Windows timeouts to enforce non-blocking read #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

larsch
Copy link

@larsch larsch commented Feb 1, 2023

This is the equivalent of the PR serialport/serialport-rs#79. I would actually recommend completely removing the COMMTIMEOUTS override in mio-serial when/if the patch is accepted on serialport-rs, as this will solve the issue of read not returning available data until buffer is full or timeout. However, without this patch, the fix in serialport-rs does nothing.

This behavior is critical for and request/response protocol. I see 27-30 millisecond minimum latency on reads for data that takes <1ms to transfer, which is unacceptable.

Original change description:

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

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
@estokes
Copy link
Collaborator

estokes commented Feb 8, 2023

Ping me when this is accepted in serialport and I'll give it a detailed look

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