move read_line to IterableRx, improve read_response
This commit is contained in:
parent
626c47c58f
commit
5a190e4890
1 changed files with 28 additions and 21 deletions
49
src/modem.rs
49
src/modem.rs
|
@ -20,6 +20,7 @@ pub enum ModemError {
|
||||||
CommandError(String),
|
CommandError(String),
|
||||||
SetupError(String),
|
SetupError(String),
|
||||||
SendDataError,
|
SendDataError,
|
||||||
|
ReadError,
|
||||||
TimeoutError,
|
TimeoutError,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +47,26 @@ impl<UART: serial::Uart> IterableRx<UART> {
|
||||||
println!("clearing serial rx");
|
println!("clearing serial rx");
|
||||||
self.reset(Duration::from_millis(500)).for_each(drop);
|
self.reset(Duration::from_millis(500)).for_each(drop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reads a whole line (that ends with \\n) within the given `timeout` passed on input.
|
||||||
|
fn read_line(&mut self, timeout: Duration) -> Result<String> {
|
||||||
|
let mut line: String = self.reset(timeout)
|
||||||
|
.map(|b| char::from(b))
|
||||||
|
.take_while(|c| *c != '\n')
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// \r must come right before \n on read; take_while excludes the matched element.
|
||||||
|
if line.ends_with('\r') {
|
||||||
|
line.push('\n');
|
||||||
|
Ok(line)
|
||||||
|
}
|
||||||
|
else if self.timeout.as_millis() == 0 {
|
||||||
|
Err(ModemError::TimeoutError)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Err(ModemError::ReadError)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<UART: serial::Uart> Iterator for IterableRx<UART> {
|
impl<UART: serial::Uart> Iterator for IterableRx<UART> {
|
||||||
|
@ -117,15 +138,6 @@ impl<UART: serial::Uart> Modem<UART> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a whole line (that ends with \\n) within the given `timeout` passed on input.
|
|
||||||
///
|
|
||||||
fn read_line(&mut self, timeout: Duration) -> String {
|
|
||||||
self.rx.reset(timeout)
|
|
||||||
.map(|b| char::from(b))
|
|
||||||
.take_while(|c| *c != '\n')
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reads the serial RX until a \\n char is encoutered, or a timeout is reached. The timeout is
|
/// Reads the serial RX until a \\n char is encoutered, or a timeout is reached. The timeout is
|
||||||
/// provided on input via the `timeout` argument. The first argument `contains` is checked
|
/// provided on input via the `timeout` argument. The first argument `contains` is checked
|
||||||
/// against a line in the response, if it's there the reading stops.
|
/// against a line in the response, if it's there the reading stops.
|
||||||
|
@ -138,19 +150,14 @@ impl<UART: serial::Uart> Modem<UART> {
|
||||||
let match_text: String = contains.unwrap_or("\n".to_string());
|
let match_text: String = contains.unwrap_or("\n".to_string());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let line = self.read_line(timeout.saturating_sub(start.elapsed()));
|
let timeout = timeout - start.elapsed();
|
||||||
if line != "" {
|
let line = self.rx.read_line(timeout)?;
|
||||||
println!("Read {} bytes from serial ({})", line.len(), line);
|
print!("Read {} bytes from serial: {}", line.len(), line);
|
||||||
response.push_str(&line);
|
response.push_str(&line);
|
||||||
if line.contains("ERROR") || line.contains(&match_text) {
|
if line.contains("ERROR") || line.contains(&match_text) {
|
||||||
println!("Found match {} for line {} ; exiting response reader now ...", match_text, line);
|
println!("Found match {} for line {} ; exiting response reader now ...", match_text, line);
|
||||||
println!("-----------------------------------------------------------");
|
|
||||||
break Ok(response.to_string())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
println!("-----------------------------------------------------------");
|
println!("-----------------------------------------------------------");
|
||||||
println!("got empty line from read_line :(");
|
break Ok(response.to_string())
|
||||||
break Err(ModemError::TimeoutError)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue