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
41
src/modem.rs
41
src/modem.rs
|
@ -20,6 +20,7 @@ pub enum ModemError {
|
|||
CommandError(String),
|
||||
SetupError(String),
|
||||
SendDataError,
|
||||
ReadError,
|
||||
TimeoutError,
|
||||
}
|
||||
|
||||
|
@ -46,6 +47,26 @@ impl<UART: serial::Uart> IterableRx<UART> {
|
|||
println!("clearing serial rx");
|
||||
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> {
|
||||
|
@ -117,15 +138,6 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
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
|
||||
/// 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.
|
||||
|
@ -138,20 +150,15 @@ impl<UART: serial::Uart> Modem<UART> {
|
|||
let match_text: String = contains.unwrap_or("\n".to_string());
|
||||
|
||||
loop {
|
||||
let line = self.read_line(timeout.saturating_sub(start.elapsed()));
|
||||
if line != "" {
|
||||
println!("Read {} bytes from serial ({})", line.len(), line);
|
||||
let timeout = timeout - start.elapsed();
|
||||
let line = self.rx.read_line(timeout)?;
|
||||
print!("Read {} bytes from serial: {}", line.len(), line);
|
||||
response.push_str(&line);
|
||||
if line.contains("ERROR") || line.contains(&match_text) {
|
||||
println!("Found match {} for line {} ; exiting response reader now ...", match_text, line);
|
||||
println!("-----------------------------------------------------------");
|
||||
break Ok(response.to_string())
|
||||
}
|
||||
} else {
|
||||
println!("-----------------------------------------------------------");
|
||||
println!("got empty line from read_line :(");
|
||||
break Err(ModemError::TimeoutError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue