AT增加带返回值的Send,简化上层使用

This commit is contained in:
大石头X2 2017-03-10 16:53:08 +08:00
parent 803ab97768
commit 82b7d990f1
3 changed files with 30 additions and 21 deletions

View File

@ -19,6 +19,9 @@
#define net_printf(format, ...) #define net_printf(format, ...)
#endif #endif
static const cstring ok = "OK";
static const cstring err = "ERROR";
struct CmdState struct CmdState
{ {
const String* Command = nullptr; const String* Command = nullptr;
@ -199,13 +202,8 @@ String AT::Send(const String& cmd, cstring expect, cstring expect2, uint msTimeo
} }
// 发送命令,自动检测并加上\r\n等待响应OK // 发送命令,自动检测并加上\r\n等待响应OK
bool AT::SendCmd(const String& cmd, uint msTimeout) String AT::Send(const String& cmd, uint msTimeout)
{ {
TS("AT::SendCmd");
static const cstring ok = "OK";
static const cstring err = "ERROR";
String cmd2; String cmd2;
// 只有AT指令需要检查结尾其它指令不检查避免产生拷贝 // 只有AT指令需要检查结尾其它指令不检查避免产生拷贝
@ -218,7 +216,16 @@ bool AT::SendCmd(const String& cmd, uint msTimeout)
} }
// 二级拦截。遇到错误也马上结束 // 二级拦截。遇到错误也马上结束
auto rt = Send(*p, ok, err, msTimeout, false); return Send(*p, ok, err, msTimeout, false);
}
// 发送命令,自动检测并加上\r\n等待响应OK
bool AT::SendCmd(const String& cmd, uint msTimeout)
{
TS("AT::SendCmd");
// 二级拦截。遇到错误也马上结束
auto rt = Send(cmd, msTimeout);
return rt.Contains(ok); return rt.Contains(ok);
} }

View File

@ -24,6 +24,7 @@ public:
/******************************** 发送指令 ********************************/ /******************************** 发送指令 ********************************/
// 发送指令,在超时时间内等待返回期望字符串,然后返回内容 // 发送指令,在超时时间内等待返回期望字符串,然后返回内容
String Send(const String& cmd, cstring expect, cstring expect2 = nullptr, uint msTimeout = 1000, bool trim = true); String Send(const String& cmd, cstring expect, cstring expect2 = nullptr, uint msTimeout = 1000, bool trim = true);
String Send(const String& cmd, uint msTimeout = 1000);
// 发送命令,自动检测并加上\r\n等待响应OK // 发送命令,自动检测并加上\r\n等待响应OK
bool SendCmd(const String& cmd, uint msTimeout = 1000); bool SendCmd(const String& cmd, uint msTimeout = 1000);
// 等待命令返回 // 等待命令返回

View File

@ -115,10 +115,11 @@ bool GSM07::OnOpen()
if (!At.Open()) return false; if (!At.Open()) return false;
// 回显 // 回显
Echo(false); Echo(true);
// 先检测AT失败再重启。保证模块处于启动状态降低网络注册时间损耗 // 先检测AT失败再重启。保证模块处于启动状态降低网络注册时间损耗
if (!Test(1, 1000) && !CheckReady()) //if (!Test(1, 1000) && !CheckReady())
if (!CheckReady())
{ {
net_printf("GSM07::Open 打开失败!"); net_printf("GSM07::Open 打开失败!");
@ -344,9 +345,9 @@ bool GSM07::Reset(bool soft)
// AT 版本信息 // AT 版本信息
String GSM07::GetVersion() String GSM07::GetVersion()
{ {
//return At.Send("AT+GMR\r\n", "OK"); //return At.Send("AT+GMR");
//return At.Send("AT+GSN\r\n", "OK"); //return At.Send("AT+GSN");
return At.Send("ATI\r\n", "OK"); return At.Send("ATI");
} }
bool GSM07::Sleep(uint ms) bool GSM07::Sleep(uint ms)
@ -368,7 +369,7 @@ bool GSM07::Restore() { return At.SendCmd("AT+RESTORE"); }
String GSM07::GetIMSI() String GSM07::GetIMSI()
{ {
auto rs = At.Send("AT+CIMI\r\n", "OK"); auto rs = At.Send("AT+CIMI");
if (rs.Length() == 0) return rs; if (rs.Length() == 0) return rs;
// 460040492206250 // 460040492206250
@ -417,13 +418,13 @@ String GSM07::GetIMSI()
return rs; return rs;
} }
String GSM07::GetIMEI() { return At.Send("AT+EGMR=2,7\r\n", "OK"); } String GSM07::GetIMEI() { return At.Send("AT+EGMR=2,7"); }
// 查询SIM的CCID也可以用于查询SIM是否存或者插好 // 查询SIM的CCID也可以用于查询SIM是否存或者插好
String GSM07::GetCCID() { return At.Send("AT+CCID\r\n", "OK"); } String GSM07::GetCCID() { return At.Send("AT+CCID"); }
/******************************** 网络服务 ********************************/ /******************************** 网络服务 ********************************/
// 获取运营商名称。非常慢 // 获取运营商名称。非常慢
String GSM07::GetMobiles() { return At.Send("AT+COPN\r\n", "OK"); } String GSM07::GetMobiles() { return At.Send("AT+COPN"); }
String GSM07::GetMobile() String GSM07::GetMobile()
{ {
@ -442,7 +443,7 @@ String GSM07::GetMobile()
*/ */
//At.SendCmd("AT+COPS=0,0"); //At.SendCmd("AT+COPS=0,0");
auto rs = At.Send("AT+COPS?\r\n", "OK"); auto rs = At.Send("AT+COPS?");
// 自动设置APN // 自动设置APN
/*if (!APN) /*if (!APN)
@ -488,7 +489,7 @@ bool GSM07::QueryRegister()
return state == 1 || state == 5; return state == 1 || state == 5;
} }
String GSM07::QuerySignal() { return At.Send("AT+CSQ\r\n", "OK"); } String GSM07::QuerySignal() { return At.Send("AT+CSQ"); }
bool GSM07::AttachMT(bool enable) bool GSM07::AttachMT(bool enable)
{ {
@ -523,10 +524,10 @@ bool GSM07::SetPDP(bool enable)
IPAddress GSM07::GetIP() IPAddress GSM07::GetIP()
{ {
auto rs = At.Send("AT+CIMI\r\n", "OK"); auto rs = At.Send("AT+CIMI");
rs.Show(true); rs.Show(true);
rs = At.Send("AT+CIFSR\r\n", "OK"); rs = At.Send("AT+CIFSR");
rs.Show(true); rs.Show(true);
int p = rs.IndexOf("\r\n"); int p = rs.IndexOf("\r\n");
@ -636,7 +637,7 @@ bool GSM07::IPShutdown(int index)
return At.SendCmd("AT+CIPSHUT\r\n"); return At.SendCmd("AT+CIPSHUT\r\n");
} }
String GSM07::IPStatus() { return At.Send("AT+CIPSTATUS\r\n", "OK"); } String GSM07::IPStatus() { return At.Send("AT+CIPSTATUS"); }
bool GSM07::SetAutoSendTimer(bool enable, ushort time) bool GSM07::SetAutoSendTimer(bool enable, ushort time)
{ {