read reply from tcp server

This commit is contained in:
Vladan Popovic 2022-06-21 02:30:00 +02:00
parent 55e5b0dc96
commit 10c61f5d78
3 changed files with 70 additions and 13 deletions

View file

@ -310,4 +310,36 @@ impl Command {
contains: Some("OK".to_string()), contains: Some("OK".to_string()),
} }
} }
pub fn tcp_receive(size: usize) -> Command {
Command {
text: format!("AT+CIPRXGET=2,{}", size),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn tcp_receive_query_len() -> Command {
Command {
text: "AT+CIPRXGET=4".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn tcp_set_manual_receive() -> Command {
Command {
text: "AT+CIPRXGET=1".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("OK".to_string()),
}
}
pub fn tcp_close() -> Command {
Command {
text: "AT+CIPCLOSE=0".to_string(),
timeout: Duration::from_millis(3000),
contains: Some("CLOSE OK".to_string()),
}
}
} }

View file

@ -4,6 +4,8 @@ mod modem;
mod command; mod command;
use anyhow; use anyhow;
use std::time::Duration;
use std::thread;
use esp_idf_hal::prelude::*; use esp_idf_hal::prelude::*;
use esp_idf_hal::peripherals::Peripherals; use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::serial; use esp_idf_hal::serial;
@ -48,7 +50,6 @@ fn main() -> anyhow::Result<()> {
config::A1_GPRS_AP.password, config::A1_GPRS_AP.password,
)?; )?;
loop {
if mdm.is_gprs_attached()? { if mdm.is_gprs_attached()? {
if mdm.tcp_is_ssl_enabled()? { if mdm.tcp_is_ssl_enabled()? {
mdm.tcp_ssl_disable()?; mdm.tcp_ssl_disable()?;
@ -57,11 +58,16 @@ fn main() -> anyhow::Result<()> {
println!("connecting to server!"); println!("connecting to server!");
let _ = mdm.tcp_set_quick_mode(true); let _ = mdm.tcp_set_quick_mode(true);
let _ = mdm.tcp_set_manual_receive()?;
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")?;
break; thread::sleep(Duration::from_millis(1000));
} let _ = mdm.tcp_receive_query_len()?;
println!("!!!!!!!!!!!!!!!! GPRS NOT ATTACHED !!!!!!!!!!!!!!!!"); let reply = mdm.tcp_receive(6)?;
println!("+++++++++++++++++++++++++++++++++");
println!("{}", reply);
println!("+++++++++++++++++++++++++++++++++");
let _ = mdm.tcp_close_connection()?;
} }
Ok(()) Ok(())

View file

@ -251,8 +251,27 @@ impl<UART: serial::Uart> Modem<UART> {
Ok(()) Ok(())
} }
pub fn tcp_set_manual_receive(&mut self) -> Result<()> {
self.send_command(Command::tcp_set_manual_receive())?;
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> {
self.send_command(Command::tcp_receive(size))
}
pub fn tcp_close_connection(&mut self) -> Result<String> {
self.send_command(Command::tcp_close())
}
} }