GSM07的AT指令集编码完成,A6/A6C/A7扩展指令编码完成。

编译通过,未测试
This commit is contained in:
大石头X2 2017-03-09 17:00:28 +08:00
parent 216f70fbd0
commit bfd42d6c16
5 changed files with 401 additions and 17 deletions

View File

@ -5,7 +5,7 @@
#include "Device\WatchDog.h"
#include "Config.h"
#include "Drivers\GSM07.h"
#include "Drivers\A67.h"
#include "TokenNet\TokenController.h"
#include "TokenNet\TokenConfig.h"
@ -121,7 +121,7 @@ NetworkInterface* AP0803::CreateGPRS()
{
debug_printf("\r\nCreateGPRS::Create \r\n");
auto net = new GSM07();
auto net = new A67();
net->Init(COM4, 9600);
net->Set(PE0, PD3, P0);
net->SetLed(*Leds[0]);

110
Drivers/A67.cpp Normal file
View File

@ -0,0 +1,110 @@
#include "Kernel\Sys.h"
#include "Kernel\Task.h"
#include "Kernel\TTime.h"
#include "Kernel\WaitHandle.h"
#include "Device\SerialPort.h"
#include "A67.h"
#include "Config.h"
#include "App\FlushPort.h"
#define NET_DEBUG DEBUG
//#define NET_DEBUG 0
#if NET_DEBUG
#define net_printf debug_printf
#else
#define net_printf(format, ...)
#endif
/******************************** 扩展指令 ********************************/
bool A67::GetGPS()
{
auto rs = At.Send("AT+GPS\r\n", "OK");
return rs.Contains("OK");
}
bool A67::SetGPS(bool enable, int rate)
{
String cmd = "AT+GPS=";
cmd = cmd + (enable ? "1" : "0");
if (!At.SendCmd(cmd)) return false;
if (rate == 0) return true;
cmd = "AT+GPSRD=";
cmd += rate;
return At.SendCmd(cmd);
}
bool A67::GetAGPS()
{
auto rs = At.Send("AT+AGPS\r\n", "OK");
return rs.Contains("OK");
}
bool A67::SetAGPS(bool enable, int rate)
{
String cmd = "AT+AGPS=";
cmd = cmd + (enable ? "1" : "0");
if (!At.SendCmd(cmd)) return false;
if (rate == 0) return true;
cmd = "AT+GPSRD=";
cmd += rate;
return At.SendCmd(cmd);
}
bool A67::CameraStart(int mode)
{
String cmd = "AT+CAMSTART=";
cmd += mode;
return At.SendCmd(cmd);
}
bool A67::CameraStop() { return At.SendCmd("AT+CAMSTOP"); }
bool A67::CameraCapture() { return At.SendCmd("AT+CAMCAP"); }
String A67::CameraRead(int from, int to)
{
String cmd = "AT+CAMRD=";
cmd += from;
if (to > 0) cmd = cmd + "," + to;
cmd += "\r\n";
return At.Send(cmd, "OK");
}
// "192.168.1.111/A6C/123.jpg",80
bool A67::CameraPost(const String& url, int port)
{
String cmd = "AT+CAMPOST=";
cmd = cmd + "\"" + url + "\"," + port;
return At.SendCmd(cmd);
}
String A67::HttpGet(const String& url, int port)
{
String cmd = "AT+HTTPGET=";
cmd = cmd + url + "," + port;
cmd += "\r\n";
return At.Send(cmd, "");
}
bool A67::HttpPost(const String& url, int port, const Buffer& data)
{
String cmd = "AT+HTTPPOST=";
cmd = cmd + url + "," + port;
return SendData(cmd, data);
}

39
Drivers/A67.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef __A67_H__
#define __A67_H__
#include "Device\Port.h"
#include "App\AT.h"
#include "Net\Socket.h"
#include "Net\NetworkInterface.h"
#include "Net\ITransport.h"
#include "Message\DataStore.h"
#include "Message\Pair.h"
#include "GSM07.h"
// A6/A6C/A7
class A67 : public GSM07
{
public:
/******************************** 扩展指令 ********************************/
bool GetGPS();
bool SetGPS(bool enable, int rate = 0);
bool GetAGPS();
bool SetAGPS(bool enable, int rate = 0);
bool CameraStart(int mode);
bool CameraStop();
bool CameraCapture();
String CameraRead(int from, int to = 0);
// "192.168.1.111/A6C/123.jpg",80
bool CameraPost(const String& url, int port = 80);
String HttpGet(const String& url, int port = 80);
bool HttpPost(const String& url, int port, const Buffer& data);
};
#endif

View File

@ -45,6 +45,7 @@ GSM07::GSM07()
Speed = 10;
APN = "CMNET";
Mux = true;
Led = nullptr;
@ -135,10 +136,10 @@ bool GSM07::CheckReady()
// 先关一会电,然后再上电,让它来一个完整的冷启动
if (!_Power.Empty())
{
_Power.Open(); // 使用前必须Open
_Power.Open(); // 使用前必须Open
_Power.Down(20);
}
if (!_Reset.Empty()) _Reset.Open(); // 使用前必须Open
if (!_Reset.Empty()) _Reset.Open(); // 使用前必须Open
// 每两次启动会有一次打开失败,交替
if (!_Reset.Empty())
@ -201,9 +202,11 @@ void GSM07::Config()
{
Echo(true);
At.SendCmd("AT+CIPSHUT\r");
At.SendCmd("AT+CGCLASS=\"B\"\r");
//At.SendCmd("AT+CGCLASS=\"B\"\r");
SetClass("B");
SetAPN(APN, false);
At.SendCmd("AT+CGATT=1\r");
AttachMT(true);
//At.SendCmd("AT+CGATT=1\r");
// 先断开已有连接
//At.SendCmd("AT+CIPSHUT\r");
//设置APN
@ -321,5 +324,222 @@ void GSM07::SetAPN(cstring apn, bool issgp)
String GSM07::GetIMSI() { return At.Send("AT+CIMI\r\n", "OK"); }
String GSM07::GetIMEI() { return At.Send("AT+EGMR\r\n", "OK"); }
// 查询SIM的CCID也可以用于查询SIM是否存或者插好
String GSM07::GetCCID() { return At.Send("AT+CCID\r\n", "OK"); }
// 获取运营商名称
String GSM07::GetMobiles() { return At.Send("AT+COPN\r\n", "OK"); }
/******************************** 网络服务 ********************************/
bool GSM07::AttachMT(bool flag)
{
if (flag)
return At.SendCmd("AT+CGATT=1");
else
return At.SendCmd("AT+CGATT=0");
}
IPAddress GSM07::GetIP()
{
auto rs = At.Send("AT+CIMI\r\n", "OK");
rs.Show(true);
rs = At.Send("AT+CIFSR\r\n", "OK");
rs.Show(true);
int p = rs.IndexOf("\r\n");
int q = rs.IndexOf("\r\n", p + 1);
rs = rs.Substring(p, q - p - 2);
return IPAddress::Parse(rs);
}
bool GSM07::SetClass(cstring mode)
{
if (!mode)mode = "B";
String str = "AT_CGCLASS=\"";
str += mode;
str += "\"";
return At.SendCmd(str);
}
/******************************** TCP/IP ********************************/
bool GSM07::IPStart(const NetUri& remote)
{
String str = "AT+CIPSTART=\"";
switch (remote.Type)
{
case NetType::Tcp:
case NetType::Http:
str += "TCP";
break;
case NetType::Udp:
str += "UDP";
break;
default:
return false;
}
//str = str + "\",\"" + remote.Address + "\",\"" + remote.Port + "\"";
str = str + "\",\"" + remote.Address + "\"," + remote.Port;
return At.SendCmd(str);
}
bool GSM07::SendData(const String& cmd, const Buffer& bs)
{
// 重发3次AT指令避免busy
int i = 0;
for (i = 0; i < 3; i++)
{
//auto rt = _Host.Send(cmd, ">", "OK", 1600);
// 不能等待OK而应该等待>,因为发送期间可能给别的指令碰撞
auto rt = At.Send(cmd, ">", "ERROR", 1600);
if (rt.Contains(">")) break;
Sys.Sleep(500);
}
if (i < 3 && At.Send(bs.AsString(), "SEND OK", "ERROR", 1600).Contains("SEND OK"))
{
return true;
}
/*// 发送失败,关闭链接,下一次重新打开
if (++_Error >= 10)
{
_Error = 0;
Close();
}*/
return false;
}
bool GSM07::IPSend(int index, const Buffer& data)
{
assert(data.Length() <= 1024, "每次最多发送1024字节");
/*
AT+CIPSEND=5,12345 //同步发送字符串
AT+CIPSEND=5 //出现”>”后可以发送5个字节的二进制数据
AT+CIPSEND //出现”>”后可以发送以CTRL+Z结尾的字符串
*/
String cmd = "AT+CIPSEND=";
if (Mux) cmd = cmd + index + ",";
cmd = cmd + data.Length() + "\r\n";
return SendData(cmd, data);
}
bool GSM07::IPClose(int index)
{
String cmd = "AT+CIPSEND=";
if (Mux) cmd = cmd + index + ",";
return At.SendCmd("AT+CIPCLOSE\r\n");
}
bool GSM07::IPShutdown(int index)
{
return At.SendCmd("AT+CIPCLOSE\r\n");
}
String GSM07::IPStatus() { return At.Send("AT+CIPSTATUS\r\n", "OK"); }
bool GSM07::SetAutoSendTimer(bool enable, ushort time)
{
String cmd = "AT+CIPATS=";
cmd = cmd + (enable ? "1" : "0") + "," + time;
return At.SendCmd(cmd);
}
IPAddress GSM07::DNSGetIP(const String& domain)
{
String cmd = "AT+CDNSGIP=";
cmd += domain;
auto rs = At.Send(cmd, "OK");
rs.Show(true);
int p = rs.IndexOf("\r\n");
int q = rs.IndexOf("\r\n", p + 1);
rs = rs.Substring(p, q - p - 2);
return IPAddress::Parse(rs);
}
bool GSM07::IPMux(bool enable)
{
String cmd = "AT+CIPMUX=";
cmd = cmd + (enable ? "1" : "0");
return At.SendCmd(cmd);
}
bool GSM07::IPHeartConfig(int index, int mode, int value)
{
/*
AT+CIPHCFG=?
AT+CIPHCFG=mode,param;
Mode: 0, 5-7200
1 100
2 100
AT+CIPHCFG?
AT+CIPHCFG=0,15,15
AT+CIPHCFG=1,553435ff,16 553435ff
AT+CIPHCFG=2,883435ee, 16 883435ee
*/
String cmd = "AT+CIPHCFG=";
if (Mux) cmd = cmd + index + ",";
cmd = cmd + mode + ",";
auto cs = (cstring)value;
if (mode == 0) return At.SendCmd(cmd + value);
if (mode == 1) return At.SendCmd(cmd + cs);
if (mode == 2) return At.SendCmd(cmd + cs);
return false;
}
bool GSM07::IPHeart(int index, bool enable)
{
String cmd = "AT+CIPHMODE=";
if (Mux) cmd = cmd + index + ",";
cmd = cmd + (enable ? "1" : "0");
return At.SendCmd(cmd);
}
// 透明传输
bool GSM07::IPTransparentConfig(int mode, int value)
{
/*
AT+CIPTCFG=?
AT+CIPTCFG=mode,param;
Mode: 0, 0-5
1 0-3000
2 10-100
3 1000-8000
AT+CIPTCFG
AT+CIPTCFG=0,15,15
*/
String cmd = "AT+CIPTCFG=";
cmd = cmd + mode + ",";
return At.SendCmd(cmd + value);
}
bool GSM07::IPTransparent(bool enable)
{
if (enable)
return At.SendCmd("AT+CIPTCFG=1");
else
return At.SendCmd("+++");
}

View File

@ -18,6 +18,7 @@ class GSM07 : public NetworkInterface
public:
AT At; // AT操作对象
cstring APN;
bool Mux; // 开启多路socket模式最多同时开启4路
IDataPort* Led; // 指示灯
@ -41,7 +42,7 @@ public:
virtual Socket* CreateSocket(NetType type);
/******************************** 基础AT指令 ********************************/
/******************************** 基础指令 ********************************/
bool Test();
bool Reset(bool soft);
String GetVersion();
@ -52,13 +53,32 @@ public:
void SetAPN(cstring apn, bool issgp);
String GetIMSI();
String GetIMEI();
// 查询SIM的CCID也可以用于查询SIM是否存或者插好
String GetCCID();
// 获取运营商名称
String GetMobiles();
/******************************** 功能指令 ********************************/
IPAddress GetIP(bool sta);
/******************************** 网络服务 ********************************/
bool AttachMT(bool flag);
IPAddress GetIP();
bool SetClass(cstring mode);
/******************************** TCP/IP ********************************/
bool IPStart(const NetUri& remote);
bool IPSend(int index, const Buffer& data);
bool SendData(const String& cmd, const Buffer& bs);
bool IPClose(int index);
bool IPShutdown(int index);
String IPStatus();
bool SetAutoSendTimer(bool enable, ushort time);
IPAddress DNSGetIP(const String& domain);
bool IPMux(bool enable);
bool IPHeartConfig(int index, int mode, int value);
bool IPHeart(int index, bool enable);
/******************************** 发送指令 ********************************/
// 透明传输
bool IPTransparentConfig(int mode, int value);
bool IPTransparent(bool enable);
private:
IPEndPoint _Remote; // 当前数据包远程地址
@ -74,11 +94,6 @@ private:
// 多个硬件socket
int* _sockets[5];
// 分析+IPD接收数据。返回被用掉的字节数
uint ParseReceive(const Buffer& bs);
// 分析关键字。返回被用掉的字节数
uint ParseReply(const Buffer& bs);
// 数据到达
void OnReceive(Buffer& bs);
};