Reputation: 194
I am working with the serialport crate on a raspberry. The given example with port.read
works fine. However port.read_to_end
or port.read_to_string
does not work, I get a timeout.
Can anybody explain this behavior? The two functions read all bytes until EOF. I am sending test strings with null termination.
I am more interested in a read_line
function. But this is not directly supported with the serialport crate, is it? Can I use the BufRead trait for this?
Upvotes: 2
Views: 3486
Reputation: 194
Here a minimal example with read_line
. Works when TX and RX are connected.
use serialport;
use std::time::Duration;
use std::io::BufReader;
use std::io::BufRead;
fn main() {
let mut serial_port = serialport::new("/dev/serial0", 9600)
.timeout(Duration::from_millis(1000))
.open()
.expect("Failed to open serial port");
let output = "This is a test.\n".as_bytes();
serial_port.write(output).expect("Write failed!");
serial_port.flush().unwrap();
let mut reader = BufReader::new(serial_port);
let mut my_str = String::new();
reader.read_line(&mut my_str).unwrap();
println!("{}", my_str);
}
Upvotes: 3