diff --git a/src/command.rs b/src/command.rs index a007a23..2672aeb 100644 --- a/src/command.rs +++ b/src/command.rs @@ -310,4 +310,36 @@ impl Command { 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()), + } + } } diff --git a/src/main.rs b/src/main.rs index 8159e4b..24eb074 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ mod modem; mod command; use anyhow; +use std::time::Duration; +use std::thread; use esp_idf_hal::prelude::*; use esp_idf_hal::peripherals::Peripherals; use esp_idf_hal::serial; @@ -48,20 +50,24 @@ fn main() -> anyhow::Result<()> { config::A1_GPRS_AP.password, )?; - loop { - if mdm.is_gprs_attached()? { - if mdm.tcp_is_ssl_enabled()? { - mdm.tcp_ssl_disable()?; - } - let _ = mdm.get_ip_addr()?; - - println!("connecting to server!"); - let _ = mdm.tcp_set_quick_mode(true); - let _ = mdm.tcp_connect("51.158.66.64", 9988)?; - let _ = mdm.tcp_send("aaaaa")?; - break; + if mdm.is_gprs_attached()? { + if mdm.tcp_is_ssl_enabled()? { + mdm.tcp_ssl_disable()?; } - println!("!!!!!!!!!!!!!!!! GPRS NOT ATTACHED !!!!!!!!!!!!!!!!"); + let _ = mdm.get_ip_addr()?; + + println!("connecting to server!"); + 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_send("aaaaa")?; + thread::sleep(Duration::from_millis(1000)); + let _ = mdm.tcp_receive_query_len()?; + let reply = mdm.tcp_receive(6)?; + println!("+++++++++++++++++++++++++++++++++"); + println!("{}", reply); + println!("+++++++++++++++++++++++++++++++++"); + let _ = mdm.tcp_close_connection()?; } Ok(()) diff --git a/src/modem.rs b/src/modem.rs index 700c166..6e240e3 100644 --- a/src/modem.rs +++ b/src/modem.rs @@ -251,8 +251,27 @@ impl Modem { 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<()> { self.send_data(payload)?; Ok(()) } + + pub fn tcp_receive(&mut self, size: usize) -> Result { + self.send_command(Command::tcp_receive(size)) + } + + pub fn tcp_close_connection(&mut self) -> Result { + self.send_command(Command::tcp_close()) + } }