-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create trivial serial port opener to handle waiting for a port
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |