📄 gprs.cpp
字号:
#include "gprs.h"
#include "escsys.h"
#include "serial.h"
#include <iostream.h>
#include "escsscanf.h"
#include "esctime.h"
GPRS::GPRS(EscInt poolSize):PhysicalLayer(poolSize)
{
chnlType = PCT_GPRS;
bHardwareError = false;
ReadTimeoutTimes = 0;
bProfileCreated = false;
#if ACTUAL_OS == OS_LINUX
fdPower = open("/dev/mygprs", O_RDWR);
fdCom = open("/dev/ttyS2", O_RDWR);
InitDev(fdCom,9600,8,1,'N',20);
powerOn();
reset();
setMode();
#else
fdPower = 0;
fdCom = 0;
#endif
}
GPRS::~GPRS()
{
powerOff();
close(fdCom);
close(fdPower);
}
void GPRS::reset()
{
char resetCmd[] = "AT&F\x0D";
int len = 20;
char buf[20];
readLine(buf,len);
len = 20;
readLine(buf,len);
}
EscBool GPRS::send(EscChar *buf,EscInt length)
{
if(!chnlOpend)
{
return ESC_FALSE;
}
int times = 0;
int writtenBytes=0;
while(times<5)
{
EscInt connectionStatus = getServiceState();
if(connectionStatus==4)
{
int bytesToSend = requestToWrite(length-writtenBytes);
if(bytesToSend>0)
{
write(fdCom,buf+writtenBytes,bytesToSend);
writtenBytes+=bytesToSend;
//cout<<"write data"<<endl;
if(writtenBytes==length)
{
EscChar recvBuf[200];
int len=200;
if(readLine(recvBuf,len))
{
if(recvBuf[len-4]=='O' && recvBuf[len-3]=='K')
{
cout<<"-write--OK----"<<endl;
return ESC_TRUE;
}
else
{
cout<<"---error---"<<endl;
return ESC_FALSE;
}
}
else
{
cout<<"---timeout----"<<endl;
return ESC_FALSE;
}
}
}
else
{
escSleep(50);
}
}
else
{
if(connectionStatus==5 ||connectionStatus==6)
{
cout<<"connection state 5,closed"<<endl;
closeChannel();
chnlOpend = ESC_FALSE;
}
}
times++;
}
return ESC_FALSE;
}
EscInt GPRS::recv(EscChar *buf,EscInt length,EscInt maxTimeOut,EscBool *timeoutOccur)
{
if(length>1500)
{
length = 1500;
}
EscInt len = length+20,temp=40;
char *recvBuf = new char[len];
EscInt len1=40,len2=40;
sendATRecvCommand(length);
if(readLine(recvBuf,len1) && readLine(recvBuf,len2))
{
if(recvBuf[0] == '^' && recvBuf[1] == 'S' && recvBuf[2] == 'I' && recvBuf[3] == 'S' && recvBuf[4] == 'R')
{
EscInt dataLen,profile;
if(sscanf(recvBuf,"^SISR:%d,%d,",&profile,&dataLen)==2)
{
if(dataLen>0)
{
readLine(recvBuf,len);
//cout<<"recv dataLen:"<<dataLen<<" len:"<<len<<endl;
if(len>0)
{
escstrncpy(buf,recvBuf,len);
recvBuf[len]=0;
//cout<<"received:"<<recvBuf<<endl;
readLine(recvBuf,temp);//read ok.
delete []recvBuf;
return len-2;
}
}
else
{
readLine(recvBuf,temp);
delete []recvBuf;
return 0;
}
}
}
}
delete []recvBuf;
return 0;
}
void GPRS::initChannel(TGPRSConfig *pChnlCfg)
{
if(!pChnlCfg)
{
return;
}
else
{
strcpy(ChannelConfig.ServerIp,pChnlCfg->ServerIp);
ChannelConfig.ServerPort = pChnlCfg->ServerPort;
strcpy(ChannelConfig.UserName,pChnlCfg->UserName);
strcpy(ChannelConfig.Pwd,pChnlCfg->Pwd);
strcpy(ChannelConfig.Apn,pChnlCfg->Apn);
}
}
EscBool GPRS::openChannel()
{
EscChar connectCmd[] = "AT^SISO=0\x0D";//"AT+CSQ\x0D\x0A";
if (chnlOpend)
{
return ESC_TRUE;
}
if(!bProfileCreated)
{
createConnectProfile(ChannelConfig.UserName,ChannelConfig.Pwd,ChannelConfig.Apn);
if(bHardwareError)
{
return ESC_FALSE;
}
createServiceProfile(ChannelConfig.ServerIp,ChannelConfig.ServerPort);
}
if(!bProfileCreated)
{
return ESC_FALSE;
}
//cout<<"open channel ";
if(!sendATCommand(connectCmd,20))
{
int state;
state = getServiceState();
if(state==4)
{
chnlOpend = ESC_TRUE;
cout << "succeed" <<endl;
return ESC_TRUE;
}
else
{
chnlOpend = ESC_FALSE;
cout << "failed,state:" <<state<<endl;
if(state==6 || state==5)
{
closeChannel();
}
return ESC_FALSE;
}
}
else
{
chnlOpend = ESC_FALSE;
//cout << "failed" <<endl;
return ESC_FALSE;
}
}
void GPRS::closeChannel()
{
EscChar closeCmd[] = "at^sisc=0\x0D";
if(!sendATCommand(closeCmd))
{
chnlOpend = ESC_FALSE;
}
}
void GPRS::powerOn()
{
EscChar ReadBuff,SendBuff;
read(fdPower,&ReadBuff,1);
if(!ReadBuff)
{
cout<<"off to on"<<endl;
SendBuff = 0x3;
write(fdPower,&SendBuff,1);
}
else
{
//cout<<"being on already"<<endl;
bPowerOn=ESC_TRUE;
}
}
void GPRS::powerOff()
{
EscChar SendBuff,ReadBuff;
printf("power off called\n\r");
read(fdPower,&ReadBuff,1);
if(ReadBuff)
{
SendBuff = 0x0;
write(fdPower,&SendBuff,1);
}
bPowerOn=ESC_FALSE;
}
void GPRS::createConnectProfile(EscChar *user,EscChar *pwd,EscChar *apn)
{
EscChar setConTypeCmd[]="at^sics=0,conType,GPRS0\x0D";
EscChar setPwdCmd[100];
EscChar setUserCmd[100];
EscChar setAPNCmd[100];
sprintf(setPwdCmd,"at^sics=0,passwd,\"%s\"\x0D",pwd);
sprintf(setUserCmd,"at^sics=0,user,\"%s\"\x0D",user);
sprintf(setAPNCmd,"at^sics=0,apn,\"%s\"\x0D",apn);
static bool firstTimeRun = true;
extern int watchDogFD;
if(firstTimeRun)
{
while(sendATCommand(setConTypeCmd))
{
write(watchDogFD,NULL,NULL);
if(bHardwareError)
{
return;
}
escSleep(50);
}
firstTimeRun = false;
}
while(sendATCommand(setPwdCmd))
{
if(bHardwareError)
{
return;
}
write(watchDogFD,NULL,NULL);
escSleep(50);
};
while(sendATCommand(setUserCmd))
{
if(bHardwareError)
{
return;
}
write(watchDogFD,NULL,NULL);
escSleep(50);
};
while(sendATCommand(setAPNCmd))
{
if(bHardwareError)
{
return;
}
write(watchDogFD,NULL,NULL);
escSleep(50);
};
}
void GPRS::setMode()
{
EscChar setModeCmd[]="at^scfg=tcp/withurcs,off\x0D";
EscInt wr = write(fdCom,setModeCmd,strlen(setModeCmd));
//cout <<"written :"<<wr<<endl;
EscChar recvBuf[200];
int len=200;
readLine(recvBuf,len);
len=200;
readLine(recvBuf,len);
len=200;
readLine(recvBuf,len);
}
void GPRS::createServiceProfile(EscChar *ip,EscWord port)
{
EscChar setSrvTypeCmd[]="at^siss=0,srvType,socket\x0D";
EscChar setConIdCmd[] = "at^siss=0,conId,0\x0D";
EscChar setIPPortCmd[100];
sprintf(setIPPortCmd,"at^siss=0,address,\"socktcp://%s:%d\"\x0D",ip,port);
static bool firstTimeRun = true;
extern int watchDogFD;
if(firstTimeRun)
{
while(sendATCommand(setSrvTypeCmd))
{
if(bHardwareError)
{
return;
}
write(watchDogFD,NULL,NULL);
escSleep(50);
}
while(sendATCommand(setConIdCmd))
{
if(bHardwareError)
{
return;
}
write(watchDogFD,NULL,NULL);
escSleep(50);
}
firstTimeRun = false;
}
while(sendATCommand(setIPPortCmd))
{
if(bHardwareError)
{
return;
}
write(watchDogFD,NULL,NULL);
escSleep(50);
};
bProfileCreated = true;
}
EscInt GPRS::readLine(EscChar *buf,EscInt &length,EscByte secondsToTimeOut)
{
EscInt len = 0;
//收到指示数据长度的行
//cout<<"<read line>";
while(len<length)
{
int n = read(fdCom,buf+len,1);
if(n==0)
{
cout<<"read line timeout"<<endl;
//cout<<"</read line>"<<endl;
//powerOn();
ReadTimeoutTimes++;
if(ReadTimeoutTimes>=3)
{
bHardwareError = true;
}
return 0;
}
len+=n;
if(len>4 && buf[len-2]=='\x0D' && buf[len-1]=='\x0A')
{
buf[len]= 0;
length = len;
ReadTimeoutTimes = 0;
bHardwareError = false;
//cout<<endl<<"--line-end--"<<endl;
return 1;
}
}
}
EscInt GPRS::sendATCommand(EscChar *pAtCmd,EscByte secondsToTimeOut)
{
EscInt wr = write(fdCom,pAtCmd,strlen(pAtCmd));
cout <<"written :"<<pAtCmd<<endl;
EscChar recvBuf[200];
int len=200;
int len1 = 200;
int len2 = 200;
if(readLine(recvBuf,len1,secondsToTimeOut) && readLine(recvBuf,len2,secondsToTimeOut))
{
if(recvBuf[len2-4]=='O' && recvBuf[len2-3]=='K')
{
//cout<<"-parse--OK----"<<endl;
return 0;
}
else
{
//cout<<"--parse-error---"<<endl;
return 0;
}
}
else
{
//cout<<"-parse--timeout----"<<endl;
return -1;
}
}
void GPRS::sendATRecvCommand(EscInt length)
{
EscChar readCmd[20];
EscChar cr_lf[2];
EscChar ok[6];
sprintf(readCmd,"at^sisr=0,%d\x0D",length);
write(fdCom,readCmd,strlen(readCmd));
}
EscInt GPRS::getSignalQuality()
{
EscChar getSignalQualityCmd[] = "AT+CSQ\x0D";
write(fdCom,getSignalQualityCmd,strlen(getSignalQualityCmd));
EscChar buf[100];
int len=100;
if(readLine(buf,len))//读回发送的命令
{
len = 100;
if(readLine(buf,len))
{
if(buf[0]=='+' && buf[1]=='C' && buf[2]=='S' && buf[3]=='Q')
{
int quality;
if(sscanf(buf,"+CSQ:%d,",&quality)==1)
{
len = 100;
readLine(buf,len);
if(quality<32)
{
return (quality+1)/8;
}
else
{
return 0;
}
}
else
{
len = 100;
readLine(buf,len);
return 0;
}
}
}
else
{
//cout<<"---timeout----"<<endl;
return -1;
}
}
else
{
//cout<<"---timeout----"<<endl;
return -1;
}
}
EscInt GPRS::getServiceState()
{
EscChar getStateCmd[] = "AT^SISI=0\x0D";
write(fdCom,getStateCmd,strlen(getStateCmd));
EscChar buf[100];
int len=100;
if(readLine(buf,len))
{
len = 100;
if(readLine(buf,len))
{
if(buf[0]=='^' && buf[1]=='S' && buf[2]=='I' && buf[3]=='S' && buf[4]=='I')
{
//cout<<endl<<"buf:"<<buf<<endl;
int profile,svrstate;
if(sscanf(buf,"^SISI:%d,%d,",&profile,&svrstate)==2)
{
len = 100;
readLine(buf,len);
//cout<<"servstat:"<<svrstate<<endl;
return svrstate;
}
else
{
len = 100;
readLine(buf,len);
return 0;
}
}
}
else
{
//cout<<"---timeout----"<<endl;
return -1;
}
}
else
{
//cout<<"---timeout----"<<endl;
return -1;
}
//return 4;
}
EscInt GPRS::requestToWrite(EscWord length)
{
EscChar writeCmd[20];
if(length>1500) length = 1500;
sprintf(writeCmd,"AT^SISW=0,%d\x0D",length);
write(fdCom,writeCmd,strlen(writeCmd));
EscChar recvBuf[40];
int len=40;
readLine(recvBuf,len);
len=40;
//readLine(recvBuf,len);
if(readLine(recvBuf,len))
{
if(recvBuf[0]=='^' && recvBuf[1]=='S' && recvBuf[2]=='I' && recvBuf[3]=='S' && recvBuf[4]=='W')
{
int dataLen=0,profile=0;
if(sscanf(recvBuf,"^SISW:%d,%d",&profile,&dataLen))
{
//cout<<"dataLen="<<dataLen<<endl;
return dataLen;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else
{
//cout<<"---timeout----"<<endl;
return 0;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -