serialport-rs
serialport-rs copied to clipboard
`read` is always non-blocking even when no data is available to read
This issue was migrated from GitLab. The original issue can be found here: https://gitlab.com/susurrus/serialport-rs/-/issues/89
The current behavior of read on an open serial port is that it always returns immediately, even when there is no data available in the input buffer. This may be in contradiction with the understanding here (from tty.rs).
It's also not consistent and not obvious how to make it consistent with the description from https://linux.die.net/man/3/cfmakeraw related to canonical/non-canonical modes. Please note that according to that explanation, raw mode does not guarantee that reads blocks if no data is available. Non-canonical mode with MIN > 0; TIME == 0 or MIN == 0; TIME > 0 or MIN > 0; TIME > 0.
Please clarify what is the intention of serialport-rs implementation, and how to make read block when no data is available for a configurable maximum timeout.
Possibly related to #25
I guess that the current solution for 'wait forever' on read would be:
port.set_timeout(Duration::MAX).ok();
At least in posix systems, it is because of this:
#[cfg(target_os = "linux")]
let wait_res = {
let timespec = TimeSpec::milliseconds(milliseconds);
nix::poll::ppoll(slice::from_mut(&mut fd), Some(timespec), SigSet::empty())
};
#[cfg(not(target_os = "linux"))]
let wait_res = nix::poll::poll(slice::from_mut(&mut fd), milliseconds as nix::libc::c_int);
from the file:
src/posix/poll.rs.
In the case of linux systems, the ppoll timeout parameter needs to be None to block without timeout.
In the case of non-linux systems, the poll's timeout parameter needs to be negative to block without timeout.
In both cases the default timeout of the library (0) will cause to always return without blocking.
@eranknafo2001 Agreed, but I don't think there is any way to express 'no timeout' in serialport-rs (see my comment here) -- so we're left with 'wait for a really long time' as a work-around.
I created a PR to fix it #50
Termios is a layer on top of a raw serial port. I think serial port library should just stick to simple support for serial port, and we can write extensions on top. But we shouldn't complicate serial-port-rs.