Skip to content

Commit b720bd4

Browse files
committed
Add a check to the monitor for interrupted system calls, which simply retries
1 parent 73bc596 commit b720bd4

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

espflash/src/cli/monitor.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
use super::line_endings::normalized;
2-
use crossterm::event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers};
3-
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
1+
use std::{
2+
io::{stdout, ErrorKind, Read, Write},
3+
thread::sleep,
4+
time::Duration,
5+
};
6+
7+
use crossterm::{
8+
event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
9+
terminal::{disable_raw_mode, enable_raw_mode},
10+
};
411
use miette::{IntoDiagnostic, Result};
512
use serialport::SerialPort;
6-
use std::io::{stdout, ErrorKind, Read, Write};
7-
use std::thread::sleep;
8-
use std::time::Duration;
913

10-
/// Converts key events from crossterm into appropriate character/escape sequences which are then
11-
/// sent over the serial connection.
14+
use super::line_endings::normalized;
15+
16+
/// Converts key events from crossterm into appropriate character/escape
17+
/// sequences which are then sent over the serial connection.
1218
///
1319
/// Adapted from https://github.com/dhylands/serial-monitor
1420
fn handle_key_event(key_event: KeyEvent) -> Option<Vec<u8>> {
@@ -83,25 +89,33 @@ pub fn monitor(mut serial: Box<dyn SerialPort>) -> serialport::Result<()> {
8389
println!(" CTRL+C Exit");
8490
println!();
8591

86-
let mut buff = [0; 128];
92+
// Explicitly set the baud rate when starting the serial monitor, to allow using
93+
// different rates for flashing.
8794
serial.set_baud_rate(115_200)?;
8895
serial.set_timeout(Duration::from_millis(5))?;
8996

9097
let _raw_mode = RawModeGuard::new();
98+
9199
let stdout = stdout();
92100
let mut stdout = stdout.lock();
101+
102+
let mut buff = [0; 128];
93103
loop {
94104
let read_count = match serial.read(&mut buff) {
95105
Ok(count) => Ok(count),
96106
Err(e) if e.kind() == ErrorKind::TimedOut => Ok(0),
107+
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
97108
err => err,
98109
}?;
110+
99111
if read_count > 0 {
100112
let data: Vec<u8> = normalized(buff[0..read_count].iter().copied()).collect();
101113
let data = String::from_utf8_lossy(&data);
114+
102115
stdout.write_all(data.as_bytes()).ok();
103116
stdout.flush()?;
104117
}
118+
105119
if poll(Duration::from_secs(0))? {
106120
if let Event::Key(key) = read()? {
107121
if key.modifiers.contains(KeyModifiers::CONTROL) {
@@ -119,12 +133,14 @@ pub fn monitor(mut serial: Box<dyn SerialPort>) -> serialport::Result<()> {
119133
_ => {}
120134
}
121135
}
136+
122137
if let Some(bytes) = handle_key_event(key) {
123138
serial.write_all(&bytes)?;
124139
serial.flush()?;
125140
}
126141
}
127142
}
128143
}
144+
129145
Ok(())
130146
}

0 commit comments

Comments
 (0)