-
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.
Remove wait on serial port monitor (#11)
- Loading branch information
Showing
2 changed files
with
16 additions
and
10 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
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 |
---|---|---|
@@ -1,25 +1,30 @@ | ||
use serialport::SerialPort; | ||
use std::cmp::min; | ||
use std::error::Error; | ||
use std::io::{stdout, Read, Write}; | ||
use std::thread::sleep; | ||
use std::{error::Error, time::Duration}; | ||
use std::time::Duration; | ||
|
||
pub fn run_serial_monitor(mut port: Box<dyn SerialPort>) -> Result<(), Box<dyn Error>> { | ||
// Until exit, read from the port and display; and send back anything the user types to the uart | ||
// Except we catch an exit combo | ||
port.set_timeout(Duration::from_millis(1000)) | ||
.expect("Setting port read timeout failed"); | ||
const BUFFER_SIZE: usize = 128; | ||
let mut read_buffer = [0; BUFFER_SIZE]; | ||
loop { | ||
if port.bytes_to_read()? > 0 { | ||
match port.read(&mut read_buffer) { | ||
Ok(bytes_read) => { | ||
let mut out = stdout(); | ||
let _ = out.write(&read_buffer[0..min(bytes_read, BUFFER_SIZE)])?; | ||
match port.read(&mut read_buffer) { | ||
Ok(bytes_read) => { | ||
let mut out = stdout(); | ||
let _ = out.write(&read_buffer[0..min(bytes_read, BUFFER_SIZE)])?; | ||
} | ||
Err(e) => { | ||
match e.kind() { | ||
std::io::ErrorKind::TimedOut => { /*No-op for timeouts */ } | ||
_ => { | ||
println!("Error reading from port {e:?}") | ||
} | ||
} | ||
Err(e) => println!("Error reading from port {e:?}"), | ||
} | ||
} else { | ||
sleep(Duration::from_millis(50)); | ||
} | ||
} | ||
} |