use std::io::{self, Read, Write}; use std::time::Duration; use clap::{App, AppSettings, Arg}; use serialport5::SerialPort; fn main() { let matches = App::new("Serialport Example - Receive Data") .about("Reads data from a serial port and echoes it to stdout") .setting(AppSettings::DisableVersionFlag) .arg( Arg::new("port") .help("The device path to a serial port") .use_delimiter(false) .required(true), ) .arg( Arg::new("baud") .help("The baud rate to connect at") .use_delimiter(false) .required(true) .validator(valid_baud), ) .get_matches(); let port_name = matches.value_of("port").unwrap(); let baud_rate = matches.value_of("baud").unwrap().parse::().unwrap(); let port = SerialPort::builder() .baud_rate(baud_rate) .read_timeout(Some(Duration::from_millis(10))) .write_timeout(Some(Duration::from_millis(10))) .open(port_name); match port { Ok(mut port) => { let mut serial_buf: Vec = vec![0; 1000]; println!("Receiving data on {} at {} baud:", &port_name, &baud_rate); loop { match port.read(serial_buf.as_mut_slice()) { Ok(t) => io::stdout().write_all(&serial_buf[..t]).unwrap(), Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (), Err(e) => eprintln!("{:?}", e), } } } Err(e) => { eprintln!("Failed to open \"{}\". Error: {}", port_name, e); ::std::process::exit(1); } } } fn valid_baud(val: &str) -> Result<(), String> { val.parse::() .map(|_| ()) .map_err(|_| format!("Invalid baud rate '{}' specified", val)) }