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

GPS编程

开发平台:

C/C++

  1. #include "..config.h"
  2. struct GPSGPRMC GPS_GPRMC;  //GPS数据结构体
  3. char GPS_Time = 0;          //GPS计数器
  4. char GPSState = 0;          //GPS状态
  5. const char GPS_V = 0;       //没定到位的状态
  6. const char GPS_A = 1;       //定到位的状态
  7. //$HZQJ,$GPRMC,064528,A,3002.6166,N,12053.053,E,14.9,276.3,260407,,,ARM_V2.29TEST,5750106008,01001208,0,
  8. const int GPSData_Buf_Len = 130;     //上发的GPS数据长度 
  9. char GPSData_Buf[130] = {0};         //上发的GPS协议数据
  10. char GPSState_ChangeFlag = 0;       //GPS状态改变系数
  11. //GPS数据接收保护,循环里每次减一
  12. //每收到GPRMC数据的时候 GPSData_Protect = GPSData_ProtectMAX ,
  13. //当 GPSData_Protect = 0的时候,GSM,GPS掉电重启动
  14. const int GPSData_ProtectMAX = 10;
  15. int GPSData_Protect = 10;           
  16. /*
  17. **********************************************************************
  18. 函数:GPS_0183
  19. 参数:*p GPS接收的数据
  20. 返回:无
  21. 功能:GPS数据处理
  22. **********************************************************************
  23. */
  24. void GPS_0183(char *p)
  25. {
  26.   int i;
  27.   char GPRMC_Buf[100] = {0};
  28.   char *p1;
  29.   if(strstr(p,"$GPRMC"))
  30.   {
  31.     char *GPRMC_P;
  32.     GPRMC_P = GPRMC_Buf;
  33.     for(i=0;i<100;i++)
  34.     {
  35.       if(*p == 'r') break;
  36.       GPRMC_Buf[i] = *p++;
  37.     }
  38.     
  39.     GPS_Time++;                       //GPS计数器
  40.     
  41.     GPSData_Protect = GPSData_ProtectMAX;
  42.     
  43.     //UART0_SendStr_char(GPRMC_Buf);
  44.     
  45.     if(strstr(GPRMC_Buf,",A,"))       //GPS定到位
  46.     {
  47.       GPSState = GPS_A;               //设置LED状态标志
  48.       State[1] = '0';                 //修改系统GPS状态位  0  表示定到位
  49.       //GPS时间
  50.       p1 = strchr(GPRMC_P,',');
  51.       p1++;
  52.       memset(GPS_GPRMC.GPS3_Time,0,GPS_TD);
  53.       strncpy(GPS_GPRMC.GPS3_Time,p1,(GPS_TD-1));
  54.       GPS_GPRMC.GPS3_Time[GPS_TD-1] = 0;
  55.       //UART0_SendStr_char("rnGPS3_Time==");
  56.       //UART0_SendStr_char(GPS_GPRMC.GPS3_Time);
  57.       //UART0_SendStr_char("==rn");
  58.       //GPS有效位
  59.       p1 = strchr(p1,',');
  60.       p1++;
  61.       memset(GPS_GPRMC.GPS4_A,0,GPS_ANE);
  62.       strncpy(GPS_GPRMC.GPS4_A,p1,(GPS_ANE-1));
  63.       GPS_GPRMC.GPS4_A[GPS_ANE-1] = 0;
  64.       //UART0_SendStr_char("rnGPS4_A==");
  65.       //UART0_SendStr_char(GPS_GPRMC.GPS4_A);
  66.       //UART0_SendStr_char("==rn");
  67.       //GPS纬度
  68.       p1 = strchr(p1,',');
  69.       p1++;
  70.       memset(GPS_GPRMC.GPS5_weidu,0,GPS_wjfLen);
  71.       for(i=0;i<GPS_wjfLen;i++)
  72.       {
  73.         if(*p1 == ',') break;
  74.         GPS_GPRMC.GPS5_weidu[i] = *p1++;
  75.       }
  76.       GPS_GPRMC.GPS5_weidu[GPS_wjfLen-1] = 0;
  77.       //UART0_SendStr_char("rnGPS5_weidu==");
  78.       //UART0_SendStr_char(GPS_GPRMC.GPS5_weidu);
  79.       //UART0_SendStr_char("==rn");
  80.       //GPS南北半球
  81.       p1 = strchr(p1,',');
  82.       p1++;
  83.       memset(GPS_GPRMC.GPS6_NS,0,GPS_ANE);
  84.       strncpy(GPS_GPRMC.GPS6_NS,p1,(GPS_ANE-1));
  85.       GPS_GPRMC.GPS6_NS[GPS_ANE-1] = 0;
  86.       //UART0_SendStr_char("rnGPS6_NS==");
  87.       //UART0_SendStr_char(GPS_GPRMC.GPS6_NS);
  88.       //UART0_SendStr_char("==rn");
  89.       //GPS经度
  90.       p1 = strchr(p1,',');
  91.       p1++;
  92.       memset(GPS_GPRMC.GPS7_jingdu,0,GPS_wjfLen);
  93.       for(i=0;i<GPS_wjfLen;i++)
  94.       {
  95.         if(*p1 == ',') break;
  96.         GPS_GPRMC.GPS7_jingdu[i] = *p1++;
  97.       }
  98.       GPS_GPRMC.GPS7_jingdu[GPS_wjfLen-1] = 0;
  99.       //UART0_SendStr_char("rnGPS7_jingdu==");
  100.       //UART0_SendStr_char(GPS_GPRMC.GPS7_jingdu);
  101.       //UART0_SendStr_char("==rn");
  102.       //GPS东西半球
  103.       p1 = strchr(p1,',');
  104.       p1++;
  105.       memset(GPS_GPRMC.GPS8_EW,0,GPS_ANE);
  106.       strncpy(GPS_GPRMC.GPS8_EW,p1,(GPS_ANE-1));
  107.       GPS_GPRMC.GPS8_EW[GPS_ANE-1] = 0;
  108.       //UART0_SendStr_char("rnGPS8_EW==");
  109.       //UART0_SendStr_char(GPS_GPRMC.GPS8_EW);
  110.       //UART0_SendStr_char("==rn");
  111.       //GPS速率
  112.       p1 = strchr(p1,',');
  113.       p1++;
  114.       memset(GPS_GPRMC.GPS9_Velocity,0,GPS_wjfLen);
  115.       for(i=0;i<GPS_wjfLen;i++)
  116.       {
  117.         if(*p1 == ',') break;
  118.         GPS_GPRMC.GPS9_Velocity[i] = *p1++;
  119.       }
  120.       GPS_GPRMC.GPS9_Velocity[GPS_wjfLen-1] = 0;
  121.       //UART0_SendStr_char("rnGPS9_Velocity==");
  122.       //UART0_SendStr_char(GPS_GPRMC.GPS9_Velocity);
  123.       //UART0_SendStr_char("==rn");
  124.       //GPS方位角
  125.       p1 = strchr(p1,',');
  126.       p1++;
  127.       memset(GPS_GPRMC.GPS10_FWJ,0,GPS_wjfLen);
  128.       for(i=0;i<GPS_wjfLen;i++)
  129.       {
  130.         if(*p1 == ',') break;
  131.         GPS_GPRMC.GPS10_FWJ[i] = *p1++;
  132.       }
  133.       GPS_GPRMC.GPS10_FWJ[GPS_wjfLen-1] = 0;
  134.       //UART0_SendStr_char("rnGPS10_FWJ==");
  135.       //UART0_SendStr_char(GPS_GPRMC.GPS10_FWJ);
  136.       //UART0_SendStr_char("==rn");
  137.       //GPS日期
  138.       p1 = strchr(p1,',');
  139.       p1++;
  140.       memset(GPS_GPRMC.GPS11_Date,0,GPS_TD);
  141.       strncpy(GPS_GPRMC.GPS11_Date,p1,(GPS_TD-1));
  142.       GPS_GPRMC.GPS11_Date[GPS_TD-1] = 0;
  143.       //UART0_SendStr_char("rnGPS11_Date==");
  144.       //UART0_SendStr_char(GPS_GPRMC.GPS11_Date);
  145.       //UART0_SendStr_char("==rn");
  146.       
  147.       memset(GPS_GPRMC.GPS12_wuxiao1,0,GPS_ANE);
  148.       
  149.       memset(GPS_GPRMC.GPS13_wuxiao2,0,GPS_ANE);
  150.         
  151.       memset(GPS_GPRMC.GPS14_V,0,Edition_Len);
  152.       strncpy(GPS_GPRMC.GPS14_V,Edition,(Edition_Len-1));
  153.       GPS_GPRMC.GPS14_V[Edition_Len-1] = 0;
  154.       
  155.       memset(GPS_GPRMC.GPS15_ID,0,TNumber_Len);
  156.       strncpy(GPS_GPRMC.GPS15_ID,TerminalNumber,(TNumber_Len-1));
  157.       GPS_GPRMC.GPS15_ID[TNumber_Len-1] = 0;
  158.       
  159.       memset(GPS_GPRMC.GPS16_State,0,State_Len);
  160.       strncpy(GPS_GPRMC.GPS16_State,State,(State_Len-1));
  161.       GPS_GPRMC.GPS16_State[State_Len-1] = 0;
  162.       
  163.       GPS_GPRMC.GPS17_Return[0] = '0';
  164.       
  165.       memset(GPSData_Buf,0,GPSData_Buf_Len);        //清GPS数据缓存
  166.     
  167.       strcat(GPSData_Buf,GPS_GPRMC.GPS1_Head);
  168.       strcat(GPSData_Buf,",");
  169.       strcat(GPSData_Buf,GPS_GPRMC.GPS2_Command);
  170.       strcat(GPSData_Buf,",");
  171.       strcat(GPSData_Buf,GPS_GPRMC.GPS3_Time);
  172.       strcat(GPSData_Buf,",");
  173.       strcat(GPSData_Buf,GPS_GPRMC.GPS4_A);
  174.       strcat(GPSData_Buf,",");
  175.       strcat(GPSData_Buf,GPS_GPRMC.GPS5_weidu);
  176.       strcat(GPSData_Buf,",");
  177.       strcat(GPSData_Buf,GPS_GPRMC.GPS6_NS);
  178.       strcat(GPSData_Buf,",");
  179.       strcat(GPSData_Buf,GPS_GPRMC.GPS7_jingdu);
  180.       strcat(GPSData_Buf,",");
  181.       strcat(GPSData_Buf,GPS_GPRMC.GPS8_EW);
  182.       strcat(GPSData_Buf,",");
  183.       strcat(GPSData_Buf,GPS_GPRMC.GPS9_Velocity);
  184.       strcat(GPSData_Buf,",");
  185.       strcat(GPSData_Buf,GPS_GPRMC.GPS10_FWJ);
  186.       strcat(GPSData_Buf,",");
  187.       strcat(GPSData_Buf,GPS_GPRMC.GPS11_Date);
  188.       strcat(GPSData_Buf,",");
  189.       strcat(GPSData_Buf,GPS_GPRMC.GPS12_wuxiao1);
  190.       strcat(GPSData_Buf,",");
  191.       strcat(GPSData_Buf,GPS_GPRMC.GPS13_wuxiao2);
  192.       strcat(GPSData_Buf,",");
  193.       strcat(GPSData_Buf,GPS_GPRMC.GPS14_V);
  194.       strcat(GPSData_Buf,",");
  195.       strcat(GPSData_Buf,GPS_GPRMC.GPS15_ID);
  196.       strcat(GPSData_Buf,",");
  197.       strcat(GPSData_Buf,GPS_GPRMC.GPS16_State);
  198.       strcat(GPSData_Buf,",");
  199.       strcat(GPSData_Buf,GPS_GPRMC.GPS17_Return);
  200.       strcat(GPSData_Buf,",");
  201.  
  202.       GPSData_Buf[GPSData_Buf_Len-1] = 0;
  203.       
  204.       GPSState_ChangeFlag = 1;//GPSData_Buf缓存里的数据有更新,GPSState_ChangeFlag的值为1
  205.  
  206.     }
  207.       
  208.     else if(strstr(GPRMC_Buf,",V,"))
  209.     {
  210.       GPSState = GPS_V;
  211.       State[1] = '1';                               //修改系统GPS状态位  1  表示没定到位
  212.     }
  213.   }
  214. }
  215. /*
  216. **********************************************************************
  217. 函数:SendGPSData
  218. 参数:无
  219. 返回:无
  220. 功能:GPS定位的时候发送GPS数据,没定位的时候发送心跳信
  221. **********************************************************************
  222. */
  223. char SendGPSData_Time = 0;
  224. void SendGPSData(void)
  225. {
  226.     if(GSMState == GPRS)
  227.     {
  228.       if(GPS_Time >=30)
  229.       {
  230.         char XinTiaoCon[100] = {0};
  231.         strcat(XinTiaoCon,"$HZQJ,00,");
  232.         strcat(XinTiaoCon,TerminalNumber);
  233.         strcat(XinTiaoCon,",");
  234.         strcat(XinTiaoCon,State);
  235.         strcat(XinTiaoCon,",");
  236.         strcat(XinTiaoCon,Edition);
  237.         strcat(XinTiaoCon,",");
  238.         
  239.         GPS_Time = 0;
  240.         
  241.         if(GPSState == GPS_A)
  242.           UART0_SendStr_char(GPSData_Buf);
  243.         else
  244.         {
  245.           if(GPSState_ChangeFlag == 1) UART0_SendStr_char(GPSData_Buf); //当GPS数据有更新,发送最新的GPS数据
  246.             else UART0_SendStr_char(XinTiaoCon);
  247.         } 
  248.         
  249.         GPSState_ChangeFlag = 0;            //发送完GPS数据后状态改变系数清零
  250.         
  251.         if((SendGPSData_Time++) >= 5) 
  252.         {
  253.           SendGPSData_Time = 0;
  254.           GSMGPS_Reset();     //5次没有返回$HZQJ,01模块掉电重启
  255.         }
  256.       }
  257.     }
  258. }