• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::io::{self, Write};
2 use std::time::Duration;
3 
4 use clap::{Arg, Command};
5 
6 use serialport::{DataBits, StopBits};
7 
main()8 fn main() {
9     let matches = Command::new("Serialport Example - Heartbeat")
10         .about("Write bytes to a serial port at 1Hz")
11         .disable_version_flag(true)
12         .arg(
13             Arg::new("port")
14                 .help("The device path to a serial port")
15                 .required(true),
16         )
17         .arg(
18             Arg::new("baud")
19                 .help("The baud rate to connect at")
20                 .use_value_delimiter(false)
21                 .required(true)
22                 .validator(valid_baud),
23         )
24         .arg(
25             Arg::new("stop-bits")
26                 .long("stop-bits")
27                 .help("Number of stop bits to use")
28                 .takes_value(true)
29                 .possible_values(["1", "2"])
30                 .default_value("1"),
31         )
32         .arg(
33             Arg::new("data-bits")
34                 .long("data-bits")
35                 .help("Number of data bits to use")
36                 .takes_value(true)
37                 .possible_values(["5", "6", "7", "8"])
38                 .default_value("8"),
39         )
40         .arg(
41             Arg::new("rate")
42                 .long("rate")
43                 .help("Frequency (Hz) to repeat transmission of the pattern (0 indicates sending only once")
44                 .takes_value(true)
45                 .default_value("1"),
46         )
47         .arg(
48             Arg::new("string")
49                 .long("string")
50                 .help("String to transmit")
51                 .takes_value(true)
52                 .default_value("."),
53         )
54         .get_matches();
55 
56     let port_name = matches.value_of("port").unwrap();
57     let baud_rate = matches.value_of("baud").unwrap().parse::<u32>().unwrap();
58     let stop_bits = match matches.value_of("stop-bits") {
59         Some("2") => StopBits::Two,
60         _ => StopBits::One,
61     };
62     let data_bits = match matches.value_of("data-bits") {
63         Some("5") => DataBits::Five,
64         Some("6") => DataBits::Six,
65         Some("7") => DataBits::Seven,
66         _ => DataBits::Eight,
67     };
68     let rate = matches.value_of("rate").unwrap().parse::<u32>().unwrap();
69     let string = matches.value_of("string").unwrap();
70 
71     let builder = serialport::new(port_name, baud_rate)
72         .stop_bits(stop_bits)
73         .data_bits(data_bits);
74     println!("{:?}", &builder);
75     let mut port = builder.open().unwrap_or_else(|e| {
76         eprintln!("Failed to open \"{}\". Error: {}", port_name, e);
77         ::std::process::exit(1);
78     });
79 
80     println!(
81         "Writing '{}' to {} at {} baud at {}Hz",
82         &string, &port_name, &baud_rate, &rate
83     );
84     loop {
85         match port.write(string.as_bytes()) {
86             Ok(_) => {
87                 print!("{}", &string);
88                 std::io::stdout().flush().unwrap();
89             }
90             Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (),
91             Err(e) => eprintln!("{:?}", e),
92         }
93         if rate == 0 {
94             return;
95         }
96         std::thread::sleep(Duration::from_millis((1000.0 / (rate as f32)) as u64));
97     }
98 }
99 
valid_baud(val: &str) -> std::result::Result<(), String>100 fn valid_baud(val: &str) -> std::result::Result<(), String> {
101     val.parse::<u32>()
102         .map(|_| ())
103         .map_err(|_| format!("Invalid baud rate '{}' specified", val))
104 }
105