serialport / serialport-rs

A cross-platform serial port library in Rust. Provides a blocking I/O interface and port enumeration including USB device information.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BufRead::read_line() doesn't work when timeout is set to 0

Bktero opened this issue · comments

Hello

I have a code like this:

let port = serialport::new(port_name, baud_rate)
    .timeout(Duration::from_millis(0)) // ----- NO TIMEOUT HERE
    .open()
    .expect("Error: ");

let mut port = BufReader::new(port);
let mut line_buffer = String::new();

match port.read_line(&mut line_buffer) {
    Ok(size) => println!("{} bytes read -> {:?}", size, line_buffer),
    Err(e) => eprintln!("Error while reading: {:?}", e),
}

It will never receive a line. Change the timeout to any value and the line will be received (apparently, when the timeout expires).

This behavior doesn't seem intuitive to me.

I believe it is necessary to avoid zero timeouts, perhaps it is worth adding a corresponding 'assert' into the '.timeout()'.

commented

Timeout means it immediately returns, and will never read a line.