进一步完善SIM900A,发送数据时,很难退出发送模式

This commit is contained in:
大石头 2017-05-07 20:00:01 +08:00
parent b0e2521bac
commit 23e58ded2e
9 changed files with 159 additions and 51 deletions

View File

@ -86,7 +86,7 @@ String AT::Send(const String& cmd, cstring expect, cstring expect2, uint msTimeo
#if NET_DEBUG
uint tid = 0;
auto task = &Task::Current();
if (task)tid = task->ID;
if (task) tid = task->ID;
#endif
// 判断是否正在发送其它指令
@ -134,7 +134,7 @@ String AT::Send(const String& cmd, cstring expect, cstring expect2, uint msTimeo
#if NET_DEBUG
// 只有AT指令显示日志
if (!at || (expect && expect[0] == '>')) enableLog = false;
//if (!at || (expect && expect[0] == '>')) enableLog = false;
if (enableLog)
{
we.Command = &cmd;

View File

@ -1,6 +1,7 @@
#include "AP0803.h"
#include "Drivers\A67.h"
#include "Drivers\Sim900A.h"
#include "Message\ProxyFactory.h"
@ -28,17 +29,11 @@ AP0803::AP0803()
Current = this;
}
NetworkInterface* AP0803::CreateGPRS()
static NetworkInterface* CreateGPRS(GSM07* net, const SerialConfig& gsm, OutputPort* led)
{
debug_printf("\r\nCreateGPRS::Create \r\n");
auto net = new GSM07();
net->Init(Gsm.Com, Gsm.Baudrate);
net->Set(Gsm.Power, Gsm.Reset, Gsm.LowPower);
net->SetLed(*Leds[0]);
net->DataKeys.Add("A6", "+CIPRCV:");
net->DataKeys.Add("SIM900A", "\r\n+IPD,");
net->Init(gsm.Com, gsm.Baudrate);
net->Set(gsm.Power, gsm.Reset, gsm.LowPower);
if (led) net->SetLed(*led);
if (!net->Open())
{
@ -49,11 +44,30 @@ NetworkInterface* AP0803::CreateGPRS()
return net;
}
NetworkInterface* AP0803::CreateA67()
{
debug_printf("\r\nCreateA67::Create \r\n");
auto net = new A67();
return CreateGPRS(net, Gsm, Leds[0]);
}
NetworkInterface* AP0803::CreateSIM900A()
{
debug_printf("\r\nCreateSIM900A::Create \r\n");
auto net = new Sim900A();
return CreateGPRS(net, Gsm, Leds[0]);
}
static void OnInitNet(void* param)
{
auto& bsp = *(AP0803*)param;
bsp.CreateGPRS();
//bsp.CreateGPRS();
bsp.CreateSIM900A();
bsp.Client->Open();
}

View File

@ -22,7 +22,8 @@ public:
AP0803();
// 打开GPRS
NetworkInterface* CreateGPRS();
NetworkInterface* CreateA67();
NetworkInterface* CreateSIM900A();
void InitNet();
void InitProxy();

View File

@ -23,6 +23,23 @@ A67::A67() :GSM07() {
At.DataKey = "+CIPRCV:";
}
// 数据到达
void A67::OnReceive(Buffer& bs)
{
// +CIPRCV:61,xxx
auto str = bs.AsString();
int p = str.IndexOf(",");
if (p < 0) p = str.IndexOf(":");
if (p <= 0) return;
int len = str.Substring(0, p).ToInt();
// 检查长度
if (p + 1 + len > bs.Length()) len = bs.Length() - p - 1;
auto data = bs.Sub(p + 1, len);
OnProcess(0, data, _Remote);
}
/******************************** 扩展指令 ********************************/
bool A67::GetGPS()
{

View File

@ -25,6 +25,10 @@ public:
String HttpGet(const String& url, int port = 80);
bool HttpPost(const String& url, int port, const Buffer& data);
protected:
// 数据到达
virtual void OnReceive(Buffer& bs);
};
#endif

View File

@ -97,7 +97,7 @@ bool GSM07::OnOpen()
if (!At.Open()) return false;
// 回显
Echo(false);
Echo(true);
// 先检测AT失败再重启。保证模块处于启动状态降低网络注册时间损耗
//if (!Test(1, 1000) && !CheckReady())
@ -108,9 +108,9 @@ bool GSM07::OnOpen()
return false;
}
#if NET_DEBUG
// 获取版本
GetVersion();
#if NET_DEBUG
/*auto ver = GetVersion();
net_printf("版本:");
ver.Show(true);*/
@ -136,7 +136,7 @@ bool GSM07::OnOpen()
// 接收数据时是否增加IP头提示
At.SendCmd("AT+CIPHEAD=1");
At.Received.Bind(&GSM07::OnReceive, this);
At.Received.Bind<GSM07>([](GSM07& gsm, Buffer& bs) { gsm.OnReceive(bs); }, this);
return true;
}
@ -309,26 +309,19 @@ Socket* GSM07::CreateSocket(NetType type)
// 数据到达
void GSM07::OnReceive(Buffer& bs)
{
// +CIPRCV:61,xxx
auto str = bs.AsString();
int p = str.IndexOf(",");
if (p < 0) p = str.IndexOf(":");
if (p <= 0) return;
int len = str.Substring(0, p).ToInt();
// 检查长度
if (p + 1 + len > bs.Length()) len = bs.Length() - p - 1;
auto data = bs.Sub(p + 1, len);
OnProcess(0, bs, _Remote);
}
void GSM07::OnProcess(int index, Buffer& data, const IPEndPoint& remotre)
{
Received(data);
// 分发到各个Socket
int idx = 0;
auto es = (GSMSocket**)Sockets;
auto sk = es[idx];
auto sk = es[index];
if (sk)
{
sk->OnProcess(data, _Remote);
sk->OnProcess(data, remotre);
}
}
@ -372,21 +365,6 @@ String GSM07::GetVersion()
At.Send("AT+CGMI");
At.Send("AT+CGMM");
// 如果没有设置DataKey则自动计算
if (rs.Length() > 0 && !At.DataKey) {
for (int i = 0; i < DataKeys.Count(); i++) {
if (rs.Contains(DataKeys.Keys()[i])) {
At.DataKey = DataKeys.Values()[i];
auto key = At.DataKey;
if (key == nullptr) key = "";
debug_printf("GSM07.DataKey=%s\r\n", key);
break;
}
}
}
return rs;
}

View File

@ -20,8 +20,6 @@ public:
cstring APN;
bool Mux; // 开启多路socket模式最多同时开启4路
Dictionary<cstring, cstring> DataKeys; // 数据键值配对
IDataPort* Led; // 指示灯
OutputPort _Power; // 电源
@ -83,7 +81,7 @@ public:
/******************************** TCP/IP ********************************/
int IPStart(const NetUri& remote);
bool IPSend(int index, const Buffer& data);
virtual bool IPSend(int index, const Buffer& data);
bool SendData(const String& cmd, const Buffer& bs);
bool IPClose(int index);
bool IPShutdown(int index);
@ -98,7 +96,7 @@ public:
bool IPTransparentConfig(int mode, int value);
bool IPTransparent(bool enable);
private:
protected:
IPEndPoint _Remote; // 当前数据包远程地址
// 打开与关闭
@ -110,7 +108,8 @@ private:
bool CheckReady();
// 数据到达
void OnReceive(Buffer& bs);
virtual void OnReceive(Buffer& bs);
void OnProcess(int index, Buffer& data, const IPEndPoint& remotre);
};
class GSMSocket : public ITransport, public Socket

View File

@ -3,7 +3,94 @@
#include "Sim900A.h"
Sim900A::Sim900A() :GSM07() {}
Sim900A::Sim900A() :GSM07() {
At.DataKey = "\r\n+IPD,";
}
bool Sim900A::OnOpen()
{
// 退出传输
String str((byte)0x1A, 16);
At.Send(str);
return GSM07::OnOpen();
}
// 数据到达
void Sim900A::OnReceive(Buffer& bs)
{
// \r\n+IPD,61:xxx
auto str = bs.AsString();
int p = str.IndexOf(":");
if (p <= 0) return;
int len = str.Substring(0, p).ToInt();
// 检查长度
if (p + 1 + len > bs.Length()) len = bs.Length() - p - 1;
auto data = bs.Sub(p + 1, len);
OnProcess(0, data, _Remote);
}
bool Sim900A::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 + ",";
// 数据较短时,直接发送
if (data.Length() < 256)
{
// 字符串里面不能有特殊字符
bool flag = true;
for (int i = 0; i < data.Length(); i++)
{
if (data[i] == '\0' || data[i] == '\"')
{
flag = false;
break;
}
}
if (flag)
{
cmd = cmd + data.Length() + ",\"" + data.AsString() + "\"";
return At.SendCmd(cmd, 1600);
}
}
cmd = cmd + data.Length() + "\r\n";
return SendData(cmd, data);*/
// 退出传输
String str((byte)0x1A, 16);
At.Send(str);
String cmd = "AT+CIPSEND\r\n";
int i = 0;
for (i = 0; i < 3; i++)
{
//auto rt = _Host.Send(cmd, ">", "OK", 1600);
// 不能等待OK而应该等待>,因为发送期间可能给别的指令碰撞
auto rt = At.Send(cmd, ">", "ERROR", 1600, false);
if (rt.Contains(">")) break;
Sys.Sleep(500);
}
if (i >= 3) return false;
At.Send(data.AsString(), 1000, false);
//String str((byte)0x1A, 16);
return At.Send(str, "SEND OK", "ERROR", 1600).Contains("SEND OK");
}
/*void Sim900A::Init(uint msTimeout)
{

View File

@ -9,6 +9,14 @@ class Sim900A : public GSM07
public:
Sim900A();
virtual bool IPSend(int index, const Buffer& data);
protected:
virtual bool OnOpen();
// 数据到达
virtual void OnReceive(Buffer& bs);
private:
//void Init(uint msTimeout = 1000);
};