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()),
}
}
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;
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(())

View File

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