read the TCP reply length and return a number

This commit is contained in:
Vladan Popovic 2022-06-22 14:37:55 +02:00
parent 327c86a504
commit 4842f18b34
2 changed files with 21 additions and 12 deletions

View file

@ -62,11 +62,13 @@ fn main() -> anyhow::Result<()> {
let _ = mdm.tcp_connect("51.158.66.64", 9988)?; let _ = mdm.tcp_connect("51.158.66.64", 9988)?;
let _ = mdm.tcp_send("aaaaa")?; let _ = mdm.tcp_send("aaaaa")?;
thread::sleep(Duration::from_millis(1000)); thread::sleep(Duration::from_millis(1000));
let _ = mdm.tcp_receive_query_len()?;
let reply = mdm.tcp_receive(6)?;
println!("+++++++++++++++++++++++++++++++++");
println!("{}", reply);
println!("+++++++++++++++++++++++++++++++++"); println!("+++++++++++++++++++++++++++++++++");
let size = mdm.tcp_receive_reply_len()?;
println!("{}", size);
//println!("+++++++++++++++++++++++++++++++++");
//let reply = mdm.tcp_receive(size)?;
//println!("{}", reply);
//println!("+++++++++++++++++++++++++++++++++");
let _ = mdm.tcp_close_connection()?; let _ = mdm.tcp_close_connection()?;
} }

View file

@ -256,19 +256,26 @@ impl<UART: serial::Uart> Modem<UART> {
Ok(()) Ok(())
} }
pub fn tcp_receive_query_len(&mut self) -> Result<()> {
self.send_command(Command::tcp_receive_query_len())?;
Ok(())
}
pub fn tcp_send(&mut self, payload: &str) -> Result<()> { pub fn tcp_send(&mut self, payload: &str) -> Result<()> {
self.send_data(payload)?; self.send_data(payload)?;
Ok(()) Ok(())
} }
pub fn tcp_receive(&mut self, size: usize) -> Result<String> { pub fn tcp_receive_reply_len(&mut self) -> Result<usize> {
self.send_command(Command::tcp_receive(size)) let reply = self.send_command(Command::tcp_receive_query_len())?;
reply.lines()
.filter(|line| line.contains("+CIPRXGET: 4"))
.next()
.ok_or(ModemError::CommandError("reply not found :/".to_string()))
.map(|line| {
line.split(',')
.into_iter()
.last()
.unwrap_or("x")
.parse::<usize>()
.map_err(|_| ModemError::CommandError(format!("response size should be a number, got {}", line)))
})
.unwrap_or(Err(ModemError::CommandError(format!("received 0 elements from parsing"))))
} }
pub fn tcp_close_connection(&mut self) -> Result<String> { pub fn tcp_close_connection(&mut self) -> Result<String> {