-
Notifications
You must be signed in to change notification settings - Fork 128
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
use read_to_end always return TimedOut err #148
Comments
Thank you for bringing this up! I bet your are encountering a phenomenon from the default implementation of |
Yeah I reached for this API as well when I first started doing serialport operations in Rust. Mainly because I was biased by pySerial & other libraries. The way I initially expected it to work was the following:
This is similar to how the pySerial
works for pretty much all of my use cases Recreating something like that isn't too hard. I use something similar to this in a couple projects fn read_until_timeout<const BUFF: usize>(
port: &mut dyn SerialPort,
length_limit: Option<NonZeroUsize>,
) -> std::io::Result<Box<[u8]>> {
let mut output = Vec::with_capacity(BUFF);
let mut buf = [0_u8; BUFF];
loop {
let res = port.read(&mut buf[..]);
match res {
// Returning zero means EOF. I'm not sure if a serial device can receive EOF
Ok(0) => break Ok(output.into()),
// Put some data in to the buffer
Ok(fill) => {
output.extend_from_slice(&buf[..fill]);
if let Some(max_size) = length_limit {
if output.len() == max_size.into() {
output.truncate(max_size.into());
break Ok(output.into());
}
}
}
// Timeout is a success case, so return the data.
Err(err) if std::io::ErrorKind::TimedOut == err.kind() => break Ok(output.into()),
Err(err) => break Err(err),
}
}
} |
I use serialport-rs on windows11, in use read_to_end(SerialPort tarit) always return TimedOut err
err:called
Result::unwrap()
on anErr
value: Custom { kind: TimedOut, error: "Operation timed out" }The text was updated successfully, but these errors were encountered: