m72 gprs模块的应用编写
#include <fcntl.h>
#include <termios.h>
#include "AppInit.h"
#include "A5_Serial_Operation.h"
#include "Global.h"
#include "Function.h"
#define GPIOA3_HIGH_GPRS_PWREN system("echo 1 >/sys/class/gpio/pioA3/value") //GPRS拉高 ARM-IO11 PA3
#define GPIOA3_LOW_GPRS_PWREN system("echo 0 >/sys/class/gpio/pioA3/value") //GPRS拉低 ARM-IO11 PA3
#define GPIOA5_HIGH_GPRS_RESET system("echo 1 >/sys/class/gpio/pioA5/value") //确定复位 ARM-IO13 PA5
#define GPIOA5_LOW_GPRS_RESET system("echo 0 >/sys/class/gpio/pioA5/value") //取消复位 ARM-IO13 PA5
int Speed_array[] = {B300,B600,B1200,B2400,B4800,B9600,B19200,B38400,B115200};
int Speed_list[] = {300,600,1200,2400,4800,9600,19200,38400,115200};
static const char sUserDev[] = {"/dev/ttyS2"};
//static const char sUserDev[5][20] = {"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2","/dev/ttyS3","/dev/ttyS4"};
#if LINUX_TEST
static const char sGPRSDev[] = {"/dev/ttyUSB0"};
static int GPRSBaund = 19200;
#else
static const char sGPRSDev[] = {"/dev/ttyS3"};
static int GPRSBaund = 115200;
#endif // LINUX_TEST
static int nResetGPRSTimeSpan = 0;
static bool bSetNewBaund = false; //是否需要重新设置波特率
static bool bHaveSetBaund = false; //是否已经成功设置过新的波特率
//关闭串口
void CloseCommPort(CommOption* pCommMsg)
{
if(pCommMsg->m_CommPort>=0) close(pCommMsg->m_CommPort);
pCommMsg->m_CommPort = -1;
pCommMsg->m_ConnFlag = false;
}
bool CheckATAnswer(char* pAnswer)
{
if(strstr(pAnswer,"READY") || strstr(pAnswer,"CONNECT") ||strstr(pAnswer,"NORMAL POWER DOWN")||strstr(pAnswer,"OK")) return true;
return false;
}
bool SendATOrder(char* pATSend,int nCommIndex,int nCount)
{
char pATRead[64] = {0};
Linx_Printf(pATSend);
write(nCommIndex,pATSend,strlen(pATSend));
do
{
usleep(500000);
if(read(nCommIndex,pATRead,sizeof(pATRead))>0)
{
if(strstr(pATRead,"ERROR")) return false;
if(CheckATAnswer(pATRead)) return true;
}
nCount--;
}while(nCount>0);
return false;
}
//硬件复位GPRS模块
void ResetGPRS(CommOption* pCommMsg)
{
SendATOrder("+++",pCommMsg->m_CommPort,10);
if(!SendATOrder("AT+QPOWD=1\r\n",pCommMsg->m_CommPort,10))
{
GPIOA5_HIGH_GPRS_RESET;
Sleep_Time(2);
GPIOA5_LOW_GPRS_RESET;
}
Sleep_Time(5);
}
//启动GPRS
bool StartGPRS(CommOption* pCommMsg)
{
//开机
int i;
ResetGPRS(pCommMsg);
GPIOA3_LOW_GPRS_PWREN; //拉低
Sleep_Time(5);
GPIOA3_HIGH_GPRS_PWREN;
Sleep_Time(5);
for(i=0;i<5;i++) if(SendATOrder("AT\r\n",pCommMsg->m_CommPort,4)) return true;
return false;
}
/*
microcom -s 19200 /dev/ttyS1
AT
ATE0
ATE1
AT+IPR=19200&W
AT+CPIN?
AT+CREG?
AT+CGREG?
AT$MYNETCON=0,"APN","CMNET"
AT$MYNETCON=0,"USERPWD",""
AT$MYNETACT=0,1
AT$MYNETCREATE=0,0,0,"116.247.104.27",6003
+++
AT+CSQ
ATO
AT+QPOWD=1
*/
bool OpenGPRSConnect(CommOption* pCommMsg)
{
char pATOrder[64] = {0};
char pIPaddress[32] = {0};
char pPortAPN[32] = {0};
int i,j;
bool bATRet = false;
static int nStartCount = 0;
if(gbChange) //如果为立即重启
{
gbChange = false;
nStartCount = 0;
}
if(nStartCount>2)
{
long nNowTime = time(NULL);
if((nNowTime-nResetGPRSTimeSpan)<300) return false;
nResetGPRSTimeSpan = nNowTime;
}
nStartCount++;
//启动GPRS
if(!StartGPRS(pCommMsg))
{
if(nStartCount>1 && !bHaveSetBaund)
{
//连续两次启动GPRS失败则判定波特率不正确,需要重新设置波特率
//如果启动失败,则表示波特率为默认的9600,需要重新设置
if(GPRSBaund == 9600)
{
GPRSBaund = 115200;
}
else
{
GPRSBaund = 9600;
}
CloseCommPort(pCommMsg); //第一次重新启动串口
bSetNewBaund = true; //需要设置新的波特率
if(nStartCount>=100) //如果启动100次都没成功一次则表示装置出现问题,重新启动程序
{
gRunStatus = EndMainApp;
return false;
}
}
return false;
}
bHaveSetBaund = true;
for(i=0;i<9;i++)
{
usleep(500000);
switch(i)
{
case 0: //取消AT回显
SendATOrder("ATE0\r\n",pCommMsg->m_CommPort,2);
bATRet = true;
break;
case 1: //设置固定波特率
GPRSBaund = 115200;
sprintf(pATOrder,"AT+IPR=%d&W\r\n",GPRSBaund);
bATRet = SendATOrder(pATOrder,pCommMsg->m_CommPort,6);
if(bATRet)
{
if(bSetNewBaund)
{
Linx_Printf("set gprs_com 115200");
bSetNewBaund = false;
CloseCommPort(pCommMsg); //第二次重新启动串口
return false;
}
}
break;
case 2: //查询SIM卡是否正常
bATRet = SendATOrder("AT+CPIN?\r\n",pCommMsg->m_CommPort,2);
break;
case 3: //查询GSM注册状态
for(j=0;j<3;j++)
{
bATRet = SendATOrder("AT+CREG?\r\n",pCommMsg->m_CommPort,10);
if(bATRet) break;
}
bATRet = true;
break;
case 4: //查询GPRS注册状态
for(j=0;j<6;j++)
{
bATRet = SendATOrder("AT+CGREG?\r\n",pCommMsg->m_CommPort,10);
if(bATRet) break;
}
bATRet = true;
break;
case 5: //注册APN
GetParaData(gParaName[16],pPortAPN);
sprintf(pATOrder,"AT$MYNETCON=0,\"APN\",\"%s\"\r\n",pPortAPN);
bATRet = SendATOrder("AT$MYNETCON=0,\"APN\",\"CMNET\"\r\n",pCommMsg->m_CommPort,10);
break;
case 6: //设置GPRS用户名和密码为空
bATRet = SendATOrder("AT$MYNETCON=0,\"USERPWD\",\"\"\r\n",pCommMsg->m_CommPort,10);
break;
case 7: //激活第0个通道
bATRet = SendATOrder("AT$MYNETACT=0,1\r\n",pCommMsg->m_CommPort,30);
break;
case 8: //设置IP地址和端口号,采用测试服务器IP
GetParaData(gParaName[5],pIPaddress);
GetParaData(gParaName[6],pPortAPN);
sprintf(pATOrder,"AT$MYNETCREATE=0,0,0,\"%s\",%s\r\n",pIPaddress,pPortAPN);
Linx_Printf(pATOrder);
bATRet = SendATOrder(pATOrder,pCommMsg->m_CommPort,90);
break;
}
if(bATRet == false) return false;
nStartCount = 0;
}
return true;
}
//读取串口配置
void IniSetComOption(CommOption* pComm,const char* pDevName,int nBaund)
{
memset(pComm->m_chrCommFile,0,sizeof(pComm->m_chrCommFile));
strcpy(pComm->m_chrCommFile,pDevName);
pComm->m_BaundSpeed=nBaund;
pComm->m_DataBit=8;
pComm->m_StopBit=1;
pComm->m_ParityBit='n';
pComm->m_CommPort = -1;
pComm->m_ConnFlag = false;
}
//打开串口,设置串口属性
bool OpenCommPort(CommOption* pCommMsg)
{
CloseCommPort(pCommMsg);
if(access(pCommMsg->m_chrCommFile,0) == -1)
{
Linx_Printf("Have no GPRS_COM");
return false;
}
pCommMsg->m_CommPort = open(pCommMsg->m_chrCommFile,O_RDWR|O_NDELAY|O_NOCTTY);
if(pCommMsg->m_CommPort<0) return false;
struct termios Options;
int FaultSpeed = 5;
int i;
//读取属性
if(tcgetattr(pCommMsg->m_CommPort,&Options) !=0 ) return false;
tcflush(pCommMsg->m_CommPort,TCIFLUSH);
//设置波特率,默认9600
for(i=0;i<(sizeof(Speed_list)/sizeof(int));i++)
{
if(pCommMsg->m_BaundSpeed == Speed_list[i])
{
FaultSpeed = i;
break;
}
}
cfsetispeed(&Options,Speed_array[FaultSpeed]);
cfsetospeed(&Options,Speed_array[FaultSpeed]);
//修改控制模式
Options.c_cflag |= CLOCAL;
Options.c_cflag |= CREAD;
Options.c_cflag &= ~CRTSCTS;
Options.c_cflag &=~CSIZE;
//设置数据位,默认8位
switch(pCommMsg->m_DataBit)
{
case 7:
Options.c_cflag |= CS7;
break;
case 8:
Options.c_cflag |= CS8;
break;
default:
Options.c_cflag |= CS8;
break;
}
//设置停止位,默认1位
switch(pCommMsg->m_StopBit)
{
case 1:
Options.c_cflag &=~CSTOPB;
break;
case 2:
Options.c_cflag |=CSTOPB;
break;
default:
Options.c_cflag &=~CSTOPB;
break;
}
//设置校验位,默认无校验
switch(pCommMsg->m_ParityBit)
{
case 'n': //无校验
case 'N':
Options.c_cflag &=~PARENB;
Options.c_iflag &=~INPCK;
break;
case 'o': //奇校验
case 'O':
Options.c_cflag |= (PARODD|PARENB);
Options.c_iflag |=INPCK;
break;
case 'e': //偶校验
case 'E':
Options.c_cflag |= PARENB;
Options.c_cflag &= ~PARODD;
Options.c_iflag |=INPCK;
break;
case 's':
case 'S':
Options.c_cflag &=~PARENB;
Options.c_cflag &=~CSTOPB;
break;
default:
Options.c_cflag &=~PARENB;
Options.c_iflag &=~INPCK;
break;
}
//阻塞时有效
Options.c_cc[VTIME] = 0;
Options.c_cc[VMIN] = 0;
// Options.c_oflag &= ~OPOST;
Options.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
Options.c_oflag &= ~(OPOST|ONLCR|OCRNL);
Options.c_iflag &= ~(IXON|IXOFF|INLCR|IGNCR|ICRNL);
if(tcsetattr(pCommMsg->m_CommPort,TCSANOW,&Options) !=0) return false;
sleep(1);
return true;
}
//读取串口数据
int ReadFromCommPort(int CommIndex,char* pReadBuff,int MaxLeng,int nDelayCount) //读取串口数据,返回读取数据长度
{
int nOldRev = 0,nNewRec = 0;
while(nDelayCount>0)
{
usleep(60000);
nNewRec = read(CommIndex,pReadBuff+nOldRev,MaxLeng-nOldRev);
if(nNewRec>0)
{
nOldRev += nNewRec;
continue;
}
else
{
nDelayCount--;
if(nOldRev>0) break;
}
}
return nOldRev;
}
//发送串口数据
void WriteToGPRSCommPort(int CommIndex,char* pWriteBuff,int nLeng) //发送串口数据
{
pthread_mutex_lock(&gMutexThread);
ShowBuffBin("Send:",pWriteBuff,nLeng);
write(CommIndex,pWriteBuff,nLeng);
pthread_mutex_unlock(&gMutexThread);
}
//调试线程口控制入口
void *UserCommSerialOperation()
{
char pRecBuff[COMM_REC_MAXLENG] = {0};
int pRecLeng = 0;
// int nComNumber = 0;
while(gRunAllThreadFlag)
{
usleep(100000);
// nComNumber = nComNumber%5;
if(!gUserComm.m_ConnFlag)
{
IniSetComOption(&gUserComm,sUserDev,9600);
gUserComm.m_ConnFlag = OpenCommPort(&gUserComm);
if(gUserComm.m_ConnFlag)
{
}
else
{
// nComNumber++;
continue;
}
}
pRecLeng = ReadFromCommPort(gUserComm.m_CommPort,pRecBuff,COMM_REC_MAXLENG,1);
if(pRecLeng>0)
{
char sendbuff[1280] = {"test com"};
int sendleng = 8;
ShowBuffBin("recv:",pRecBuff,pRecLeng);
//SolveRecBuff(pRecBuff,pRecLeng,sendbuff,&sendleng,2,&gCallPicStruct[2]);
if(sendleng>0) WriteToGPRSCommPort(gUserComm.m_CommPort,sendbuff,sendleng);
}
else
{
char sendbuff[1280] = {"test com"};
int sendleng = 8;
//SolveRecBuff(pRecBuff,pRecLeng,sendbuff,&sendleng,2,&gCallPicStruct[2]);
if(sendleng>0) WriteToGPRSCommPort(gUserComm.m_CommPort,sendbuff,sendleng);
}
usleep(200000);
// Linx_Printf("CLOSE COM");
// Linx_Printf(sUserDev[nComNumber]);
// CloseCommPort(&gUserComm);
// nComNumber++;
}
CloseCommPort(&gUserComm);
return NULL;
}
//GPRS线程控制入口
void *GPRSCommSerialOperation()
{
char pRecBuff[COMM_REC_MAXLENG] = {0};
int pRecLeng = 0;
while(gRunAllThreadFlag)
{
usleep(50000);
#if LINUX_TEST
//打开串口
if(!gGPRSComm.m_ConnFlag)
{
IniSetComOption(&gGPRSComm,sGPRSDev,GPRSBaund);
gGPRSComm.m_ConnFlag = OpenCommPort(&gGPRSComm);
gGPRSConnFlag = gGPRSComm.m_ConnFlag;
gRunStatus = LoginAndHeartStatus;
if( gGPRSComm.m_ConnFlag)
{
Linx_Printf("Open GPRS_Com Sucess");
}
else
{
Linx_Printf("Open GPRS_Com Failed");
sleep(1);
}
continue;
}
//打开GPRS
if(!gGPRSConnFlag)
{
gLoginFlag = false;
gRunStatus = LoginAndHeartStatus;
gGPRSConnFlag = gGPRSComm.m_ConnFlag;
sleep(1);
continue;
}
#else
//打开串口
if(!gGPRSComm.m_ConnFlag)
{
IniSetComOption(&gGPRSComm,sGPRSDev,GPRSBaund);
gGPRSComm.m_ConnFlag = OpenCommPort(&gGPRSComm);
gGPRSConnFlag = false;
if( gGPRSComm.m_ConnFlag)
{
Linx_Printf("Open GPRS_Com Sucess");
Linx_Printf("Start GPRS ......");
}
else
{
Linx_Printf("Open GPRS_Com Failed");
sleep(1);
}
continue;
}
//打开GPRS
if(!gGPRSConnFlag)
{
gGPRSConnFlag = OpenGPRSConnect(&gGPRSComm);
if( gGPRSConnFlag) Linx_Printf("GPRS is Online");
if(gCanType != 0)
{
gRunStatus = LoginAndHeartStatus;
gLoginFlag = false;
}
sleep(1);
continue;
}
#endif // LINUX_TEST
//读取串口数据
memset(pRecBuff,0,sizeof(pRecBuff));
pRecLeng = ReadFromCommPort(gGPRSComm.m_CommPort,pRecBuff,COMM_REC_MAXLENG,1);
if(pRecLeng>0)
{
//处理接收数据
char sendbuff[1280] = {0};
int sendleng = 0;
SolveRecBuff(pRecBuff,pRecLeng,sendbuff,&sendleng,0,&gCallPicStruct[0]);
if(sendleng>0) WriteToGPRSCommPort(gGPRSComm.m_CommPort,sendbuff,sendleng);
if(gbChange)
{
gGPRSConnFlag = false;
sleep(1);
}
}
}
gGPRSConnFlag = false;
CloseCommPort(&gGPRSComm);
gGPRSComm.m_ConnFlag = false;
return NULL;
}