GSM.c
上传用户:buddy3
上传日期:2022-06-12
资源大小:100k
文件大小:5k
源码类别:

GPS编程

开发平台:

C/C++

  1. #include "..config.h"
  2. char IPADDR[]=""60.12.9.242",2,7000"; //求精公司IP地址
  3. char APN[] = ""CMNET""; //接入点
  4. char GSMState = 0;
  5. const char GSM = 0;
  6. const char GPRS = 1;
  7. char GSMStart_Flag = 0;     //GSM模块初始化标志位,GSM初始化好了 GSMStart_Flag = 1
  8. int GSM_Init_int1 = 3000;
  9. char GSM_Init_Flag_Time = 0;
  10. char GSM_Init_Flag = 0;
  11. /*
  12. **********************************************************************
  13. 函数:GSM_Init
  14. 参数:无
  15. 返回:无
  16. 功能:GSM模块初始化
  17. **********************************************************************
  18. */
  19. void GSM_Init(void)
  20. {
  21.   if(GSMState == GSM)
  22.   {
  23.     char AT_CGDCONT[40] = {0};
  24.     char AT_DESTINFO[60] = {0};
  25.     //接入点初始化
  26.     strcat(AT_CGDCONT,"AT+CGDCONT=1,"IP",");  
  27.     strcat(AT_CGDCONT,APN);
  28.     strcat(AT_CGDCONT,"rn");
  29.     //IP地址初始化
  30.     strcat(AT_DESTINFO,"AT$DESTINFO=");
  31.     strcat(AT_DESTINFO,IPADDR);
  32.     strcat(AT_DESTINFO,"rn");
  33.     
  34.     if((GSM_Init_int1 > 0) && ((--GSM_Init_int1) == 0))
  35.     {
  36.       GSM_Init_int1 = 10000;
  37.         switch(GSM_Init_Flag)
  38.         {
  39.           case 0: 
  40.                   if((GSM_Init_Flag_Time++) >=5)  GSMGPS_Reset();
  41.                   UART0_SendStr_char("AT$NOSLEEP=1rn");
  42.             break;
  43.           case 1:
  44.                   if((GSM_Init_Flag_Time++) >=5)  GSMGPS_Reset();
  45.                   UART0_SendStr_char("AT+CPBS="SM"rn");
  46.             break;
  47.           case 2:
  48.                   if((GSM_Init_Flag_Time++) >=10)  GSMGPS_Reset();
  49.                   UART0_SendStr_char("AT+CPBR=1rn");
  50.             break;
  51.           case 3:
  52.                   if((GSM_Init_Flag_Time++) >=5)  GSMGPS_Reset();
  53.                   UART0_SendStr_char(AT_CGDCONT);
  54.             break;
  55.           case 4:
  56.                   if((GSM_Init_Flag_Time++) >=5)  GSMGPS_Reset();
  57.                   UART0_SendStr_char(AT_DESTINFO);
  58.             break;
  59.           case 5:
  60.                   if((GSM_Init_Flag_Time++) >=5)  GSMGPS_Reset();
  61.                   UART0_SendStr_char("AT%CGPCO=1,"PAP,,",1rn");
  62.             break;
  63.           case 6:
  64.                   if((GSM_Init_Flag_Time++) >=5)  GSMGPS_Reset();
  65.                   UART0_SendStr_char("ATD*97#rn");
  66.             break;
  67.           case 7:
  68.                   
  69.             break;
  70.           default:
  71.             break;
  72.         }
  73.     }  
  74.   }
  75. }
  76. /*
  77. **********************************************************************
  78. 函数:ReadGSMReturn
  79. 参数:*p    接收的GSM模块数据
  80. 返回:无
  81. 功能:读GSM的AT命令返回值
  82. **********************************************************************
  83. */
  84. void ReadGSMReturn(char *p)
  85. {
  86.   switch (GSM_Init_Flag)
  87.   {
  88.   case 0:
  89.           if(strstr(p,"OK"))
  90.           {
  91.             GSM_Init_int1 = 100;
  92.             GSM_Init_Flag = 1;
  93.             GSM_Init_Flag_Time = 0;
  94.           }
  95.           break;
  96.   case 1:
  97.           if(strstr(p,"OK"))
  98.           {
  99.             GSM_Init_int1 = 100;
  100.             GSM_Init_Flag = 2;
  101.             GSM_Init_Flag_Time = 0;
  102.           }
  103.           break;
  104.   case 2:
  105.           if(strstr(p,"OK"))
  106.           {
  107.             if(strstr(p,"+CPBR: 1,""))    //取SIM卡里的终端ID号
  108.             {
  109.               char *p1,i; 
  110.               p1=strstr(p,""")+1;
  111.               
  112.               for(i=0;i<TNumber_Len;i++)
  113.               {
  114.                 if(*p1 == '"') break;  
  115.                 TerminalNumber[i] =*p1++;
  116.               }
  117.             }     
  118.             GSM_Init_int1 = 100;
  119.             GSM_Init_Flag = 3;
  120.             GSM_Init_Flag_Time = 0;
  121.           }
  122.           break;
  123.   case 3:
  124.           if(strstr(p,"OK"))
  125.           {
  126.             GSM_Init_int1 = 100;
  127.             GSM_Init_Flag = 4;
  128.             GSM_Init_Flag_Time = 0;
  129.           }
  130.           break;
  131.   case 4:
  132.           if(strstr(p,"OK"))
  133.           {
  134.             GSM_Init_int1 = 100;
  135.             GSM_Init_Flag = 5;
  136.             GSM_Init_Flag_Time = 0;
  137.           }
  138.           break;
  139.   case 5:
  140.           if(strstr(p,"OK"))
  141.           {
  142.             GSM_Init_int1 = 100;
  143.             GSM_Init_Flag = 6;
  144.             GSM_Init_Flag_Time = 0;
  145.           }
  146.           break;
  147.   case 6:
  148.           if(strstr(p,"Wait Socket Open"))
  149.           {
  150.             GSM_Init_int1 = 1000;
  151.             GSM_Init_Flag = 0;
  152.             GSM_Init_Flag_Time = 0;
  153.             
  154.             GSMState = GPRS;
  155.             State[0] = '0';   //表示挂上GPRS
  156.             GSMStart_Flag = 1;//初始化标志位为1,表示初始化结束
  157.             
  158.             GPS_Time = 30;    //挂上GPRS后,马上就发服务器数据
  159.           }
  160.           break;
  161.   default:
  162.           break;
  163.   }
  164. }
  165. /*
  166. **********************************************************************
  167. 函数:GPRS_Con
  168. 参数:*p    接收的GSM模块数据
  169. 返回:无
  170. 功能:GPRS协议处理
  171. **********************************************************************
  172. */
  173. void GPRS_Con(char *p)
  174. {
  175.   if(strstr(p,"$HZQJ,01,"))
  176.   {
  177.       GSMState = GPRS;
  178.       SendGPSData_Time = 0;           //清心跳或定位数据发送次数计数
  179.   }
  180. }
  181. /*
  182. void WritePhoneBook(char *Book,char *Number)
  183. {
  184. char Buf[50] = {0};
  185. strcat(Buf,"AT+CPBW=");
  186. strcat(Buf,Number);
  187. strcat(Buf,",");
  188. strcat(Buf,""");
  189. strcat(Buf,Book);
  190. strcat(Buf,"",129,""rn");
  191. GSMSendStr(Buf);
  192. }*/