#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;
}

m72 gprs模块的应用编写的更多相关文章

  1. 认识和选用常用的几种 GPRS 模块(转)

    源:http://blog.sina.com.cn/s/blog_4d80055a0100e8kr.html 我在这里把常见的GPRS模块分成3种: (1)GPRS DTU(GPRS数传单元,常称GP ...

  2. GSM/GPRS模块 AT指令集C语言编程——基于有方M660+和MSP430单片机

    GSM/GPRS芯片是手机中负责收发短信.拨打电话以及访问GPRS网络的核心器件.有方M660+为深圳有方公司生产的一款超小封装的GSM/GPRS工业无线模块,可以提供高品质的语音.短信.数据业务等功 ...

  3. 关于PPP拨号 和 AT指令实现GPRS模块联网的疑问

    以下内容摘抄自互联网: ppp拨号 与 at命令的疑问 GPRS模块在Linux平台上ppp拨号上网总结与心得 以PPP拨号实现GPRS与因特网的数据通信的具体实现流程 问: 我刚接触GPRS,了解A ...

  4. linux下利用GPRS模块发短信、打电话

    一.开发环境     内核版本:linux-3.0    开发板:FL2440(nandflash:K9F1G08 128M)    GPRS模块:SIM900   二.与发短信和拨号相关的 AT 指 ...

  5. 全球第一款纯数据GPRS模块 有方M590 概述

    更多精彩请到http://blog.tuzhuke.info/?cat=30 M590为全球第一款纯数据GPRS模块,专注数据收发功能,GPRS数据以及短信数据.没有电话语音功能,可以能够拨打或者接听 ...

  6. linux下GPRS模块使用AT命令实现拨接电话,发中英文短信

    开发板           :fl2440 cpu                :  s3c2440(arm9) 开发模块       :A7(GPRS/GPS) 远程登陆软件:PUTTY **** ...

  7. GPRS模块在Linux平台上ppp拨号上网总结与心得

    linux平台的ppp拨号上网,(注明:这里只谈命令行拨号,用linux就要习惯和熟练使用命令行) 在网上常见的有三种方式:1.使用智能的ppp拨号软件wvdial:参考案例:本博客的<使用wv ...

  8. Linux-C实现GPRS模块发送短信

    “GSM模块,是将GSM射频芯片.基带处理芯片.存储器.功放器件等集成在一块线路板上,具有独立的操作系统.GSM射频处理.基带处理并提供标准接口的功能模块.GSM模块根据其提供的数据传输速率又可以分为 ...

  9. linux下GPRS模块的应用程序

    ---------------------------------------------------------------------------------------------------- ...

随机推荐

  1. 着重protected、default区别

    public是所有,在哪都可以访问private是私有,仅在自己类里面可以访问protected是自己包里面可以访问,如果有不同包的类想调用它们,那么这个类必须是定义它们的类的子类.default也是 ...

  2. 照片管家iOS-实现本地相册、视频、安全保护、社交分享源码下载Demo

    <照片管家> APP功能: 1.本地照片批量导入与编辑 2.本地视频存储与播放 3.手势密码.数字密码.TouchID安全保护 4.QQ.微信.微博.空间社交分享 5.其他细节功能. 运用 ...

  3. Foundations of Machine Learning: Boosting

    Foundations of Machine Learning: Boosting Boosting是属于自适应基函数(Adaptive basis-function Model(ABM))中的一种模 ...

  4. [转]TCP(HTTP)长连接和短连接区别和怎样维护长连接

    原文链接 一.HTTP协议和TCP协议 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议.IP协议主要解决网络路由和寻址问题, ...

  5. eclipse 中使用中文JAVA api文档

    http://hi.baidu.com/danghj/item/7625a1be20946e43ba0e1202在eclipse中使用中文JAVA api文档Sun 官方的中文版 Java API 文 ...

  6. RabbitMQ消息队列(四):分发到多Consumer(Publish/Subscribe)[转]

    上篇文章中,我们把每个Message都是deliver(提供)到某个Consumer.在这篇文章中,我们将会将同一个Message deliver(提供)到多个Consumer中.这个模式也被成为 & ...

  7. BIP_BI Pubisher的SQL/XSL/FO扩展函数应用(概念)

    2014-12-01 Created By BaoXinjian

  8. page_address()函数分析

    由于X86平台上面,内存是划分为低端内存和高端内存的,所以在两个区域内的page查找对应的虚拟地址是不一样的. 一. x86上关于page_address()函数的定义 在include/linux/ ...

  9. java日志-纯Java配置使用slf4j配置log4j(转)

    工程目录如下 代码里面用的是slf4j,但是想要用log4j来管理日志,就得添加slf4j本来的jar,然后添加log4j和slf4j箱关联的jar即可. 如果是maven项目的话添加下面的依赖即可 ...

  10. spring中事务配置

    1 如果在方法.类.接口上使用注解的方式声明事务,需要在配置文件中进行配置,以便通知 Spring 容器对标注 @Transactional 注解的 bean 加工处理. 首先需要引入 tx 命名空间 ...