GSM.c
上传用户:buddy3
上传日期:2022-06-12
资源大小:100k
文件大小:5k
- #include "..config.h"
- char IPADDR[]=""60.12.9.242",2,7000"; //求精公司IP地址
- char APN[] = ""CMNET""; //接入点
- char GSMState = 0;
- const char GSM = 0;
- const char GPRS = 1;
- char GSMStart_Flag = 0; //GSM模块初始化标志位,GSM初始化好了 GSMStart_Flag = 1
- int GSM_Init_int1 = 3000;
- char GSM_Init_Flag_Time = 0;
- char GSM_Init_Flag = 0;
- /*
- **********************************************************************
- 函数:GSM_Init
- 参数:无
- 返回:无
- 功能:GSM模块初始化
- **********************************************************************
- */
- void GSM_Init(void)
- {
- if(GSMState == GSM)
- {
- char AT_CGDCONT[40] = {0};
- char AT_DESTINFO[60] = {0};
- //接入点初始化
- strcat(AT_CGDCONT,"AT+CGDCONT=1,"IP",");
- strcat(AT_CGDCONT,APN);
- strcat(AT_CGDCONT,"rn");
- //IP地址初始化
- strcat(AT_DESTINFO,"AT$DESTINFO=");
- strcat(AT_DESTINFO,IPADDR);
- strcat(AT_DESTINFO,"rn");
-
- if((GSM_Init_int1 > 0) && ((--GSM_Init_int1) == 0))
- {
- GSM_Init_int1 = 10000;
- switch(GSM_Init_Flag)
- {
- case 0:
- if((GSM_Init_Flag_Time++) >=5) GSMGPS_Reset();
- UART0_SendStr_char("AT$NOSLEEP=1rn");
- break;
- case 1:
- if((GSM_Init_Flag_Time++) >=5) GSMGPS_Reset();
- UART0_SendStr_char("AT+CPBS="SM"rn");
- break;
- case 2:
- if((GSM_Init_Flag_Time++) >=10) GSMGPS_Reset();
- UART0_SendStr_char("AT+CPBR=1rn");
- break;
- case 3:
- if((GSM_Init_Flag_Time++) >=5) GSMGPS_Reset();
- UART0_SendStr_char(AT_CGDCONT);
- break;
- case 4:
- if((GSM_Init_Flag_Time++) >=5) GSMGPS_Reset();
- UART0_SendStr_char(AT_DESTINFO);
- break;
- case 5:
- if((GSM_Init_Flag_Time++) >=5) GSMGPS_Reset();
- UART0_SendStr_char("AT%CGPCO=1,"PAP,,",1rn");
- break;
- case 6:
- if((GSM_Init_Flag_Time++) >=5) GSMGPS_Reset();
- UART0_SendStr_char("ATD*97#rn");
- break;
- case 7:
-
- break;
- default:
- break;
- }
- }
- }
- }
- /*
- **********************************************************************
- 函数:ReadGSMReturn
- 参数:*p 接收的GSM模块数据
- 返回:无
- 功能:读GSM的AT命令返回值
- **********************************************************************
- */
- void ReadGSMReturn(char *p)
- {
- switch (GSM_Init_Flag)
- {
- case 0:
- if(strstr(p,"OK"))
- {
- GSM_Init_int1 = 100;
- GSM_Init_Flag = 1;
- GSM_Init_Flag_Time = 0;
- }
- break;
- case 1:
- if(strstr(p,"OK"))
- {
- GSM_Init_int1 = 100;
- GSM_Init_Flag = 2;
- GSM_Init_Flag_Time = 0;
- }
- break;
- case 2:
- if(strstr(p,"OK"))
- {
- if(strstr(p,"+CPBR: 1,"")) //取SIM卡里的终端ID号
- {
- char *p1,i;
- p1=strstr(p,""")+1;
-
- for(i=0;i<TNumber_Len;i++)
- {
- if(*p1 == '"') break;
- TerminalNumber[i] =*p1++;
- }
- }
- GSM_Init_int1 = 100;
- GSM_Init_Flag = 3;
- GSM_Init_Flag_Time = 0;
- }
- break;
- case 3:
- if(strstr(p,"OK"))
- {
- GSM_Init_int1 = 100;
- GSM_Init_Flag = 4;
- GSM_Init_Flag_Time = 0;
- }
- break;
- case 4:
- if(strstr(p,"OK"))
- {
- GSM_Init_int1 = 100;
- GSM_Init_Flag = 5;
- GSM_Init_Flag_Time = 0;
- }
- break;
- case 5:
- if(strstr(p,"OK"))
- {
- GSM_Init_int1 = 100;
- GSM_Init_Flag = 6;
- GSM_Init_Flag_Time = 0;
- }
- break;
- case 6:
- if(strstr(p,"Wait Socket Open"))
- {
- GSM_Init_int1 = 1000;
- GSM_Init_Flag = 0;
- GSM_Init_Flag_Time = 0;
-
- GSMState = GPRS;
- State[0] = '0'; //表示挂上GPRS
- GSMStart_Flag = 1;//初始化标志位为1,表示初始化结束
-
- GPS_Time = 30; //挂上GPRS后,马上就发服务器数据
- }
- break;
- default:
- break;
- }
- }
- /*
- **********************************************************************
- 函数:GPRS_Con
- 参数:*p 接收的GSM模块数据
- 返回:无
- 功能:GPRS协议处理
- **********************************************************************
- */
- void GPRS_Con(char *p)
- {
- if(strstr(p,"$HZQJ,01,"))
- {
- GSMState = GPRS;
- SendGPSData_Time = 0; //清心跳或定位数据发送次数计数
- }
- }
- /*
- void WritePhoneBook(char *Book,char *Number)
- {
- char Buf[50] = {0};
- strcat(Buf,"AT+CPBW=");
- strcat(Buf,Number);
- strcat(Buf,",");
- strcat(Buf,""");
-
- strcat(Buf,Book);
- strcat(Buf,"",129,""rn");
- GSMSendStr(Buf);
- }*/