parent
216f70fbd0
commit
bfd42d6c16
|
@ -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]);
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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
|
|
@ -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("+++");
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue