Skip to content

Commit

Permalink
Create trivial serial port opener to handle waiting for a port
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralim committed May 9, 2024
1 parent bd176ea commit 9306066
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions bestool/src/serial_port_opener.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::time::Duration;

use serialport::SerialPort;
use tracing::info;

pub fn open_serial_port_with_wait(
port_path: &str,
baud_rate: u32,
wait_for_port: bool,
) -> Box<dyn SerialPort> {
// If wait for port is true, we handle it not being openable by retrying while waiting for it
info!("Opening {port_path} @ {baud_rate}");
loop {
let serial_port = serialport::new(port_path, baud_rate);
match serial_port.open() {
Ok(port) => return port,
Err(_) => {
//Port didnt open
if !wait_for_port {
panic!("Unable to open requested Serial Port");
}
}
}
std::thread::sleep(Duration::from_millis(250));
}
}

0 comments on commit 9306066

Please sign in to comment.