AppTaskGps.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * AppTaskGps.c
  3. *
  4. * Created on: 2022年2月16日
  5. * Author: QiXiang_CHENJIE
  6. */
  7. #include "AppTaskGps.h"
  8. void strdel(char *str, char c);
  9. uint32 location_handle(char *in1);
  10. void GpsDataDecode(uint8 *msg);
  11. void GpsTask(void *pvParameters)
  12. {
  13. (void)pvParameters;
  14. GpsDataQueueHandle = xQueueCreate(1, sizeof(GPSInfo));//长度为1才可以允许覆写
  15. Dio_WriteChannel(DioConf_DioChannel_PTD1_GPIO_OUT_MCU_GPS_POW_EN, STD_ON);//GPS开机
  16. uint16 pReadLen = 0;
  17. uint8 UartRecvPtr[512];
  18. while(1)
  19. {
  20. vTaskDelay(pdMS_TO_TICKS(10));
  21. memset(UartRecvPtr,0,512);
  22. UART_Receive_Data(UART_LPUART2,UartRecvPtr,&pReadLen,1000);
  23. if(pReadLen>0)
  24. {
  25. GpsDataDecode(UartRecvPtr);
  26. }
  27. }
  28. }
  29. void GpsDataDecode(uint8 *msg)
  30. {
  31. //char temp[] = "$GNRMC,082626.000,A,2939.91801,N,10637.09500,E,0.543,30.254,261120,,,A,V*17";
  32. char *p = NULL;
  33. char *p2 = NULL;
  34. const char *delim = "\n";
  35. GPSInfo GpsInfoData;
  36. uint8 index = 0;
  37. char *databuffer[20];
  38. uint32 speedtemp;
  39. uint32 latitude;
  40. uint32 longitude;
  41. uint16 direction;
  42. memset((uint8 *)&GpsInfoData, 0x00, sizeof(GPSInfo));
  43. p = strtok(msg, delim); //将信息进行分割
  44. p2 = strtok(NULL, delim);
  45. p = strtok(p, ","); //只取第1行的信息RMC
  46. //p = strtok(temp,",");//模拟测试
  47. if (strcmp(p, "$GNRMC") == 0)
  48. {
  49. index = 0;
  50. while (p)
  51. {
  52. databuffer[index] = p;
  53. p = strtok(NULL, ",");
  54. index++;
  55. }
  56. if (index > 5 && strcmp(databuffer[2], "A") == 0)
  57. {
  58. GpsInfoData.locateMark = 0x01; //有效,东经,北纬写定
  59. strdel(databuffer[3], '.');
  60. strdel(databuffer[5], '.');
  61. strdel(databuffer[7], '.');
  62. speedtemp = atol(databuffer[7]) * 1852 / 1e5; //节换算单位,1节=1.852km每小时
  63. GpsInfoData.speed[0] = (speedtemp >> 8) & 0xFF;
  64. GpsInfoData.speed[1] = speedtemp & 0xFF;
  65. latitude = location_handle(databuffer[3]);
  66. GpsInfoData.latitude[0] = latitude >> 24;
  67. GpsInfoData.latitude[1] = latitude >> 16;
  68. GpsInfoData.latitude[2] = latitude >> 8;
  69. GpsInfoData.latitude[3] = latitude;
  70. longitude = location_handle(databuffer[5]);
  71. GpsInfoData.longitude[0] = longitude >> 24;
  72. GpsInfoData.longitude[1] = longitude >> 16;
  73. GpsInfoData.longitude[2] = longitude >> 8;
  74. GpsInfoData.longitude[3] = longitude;
  75. if (speedtemp >= 50) //大于5km/h才输出方位
  76. {
  77. direction = atol(databuffer[8]);
  78. GpsInfoData.direction[0] = direction >> 8;
  79. GpsInfoData.direction[1] = direction;
  80. }
  81. else
  82. {
  83. GpsInfoData.direction[0] = 0xff;
  84. GpsInfoData.direction[1] = 0xff;
  85. }
  86. // if (speedtemp >= 30 && speedtemp <= 1500 && BattWorkStateDelay == 0x01)
  87. // {
  88. // AppDataInfo.appDataModify = true;
  89. // AppDataInfo.AccMileage = speedtemp / 36 + AppDataInfo.AccMileage;
  90. // if (AppDataInfo.AccMileage >= 0xfffffffe)
  91. // {
  92. // AppDataInfo.AccMileage = 0;
  93. // }
  94. // }累计里程的累加
  95. }
  96. // GpsInfoData.AccMileage[0] = AppDataInfo.AccMileage >> 24;
  97. // GpsInfoData.AccMileage[1] = AppDataInfo.AccMileage >> 16;
  98. // GpsInfoData.AccMileage[2] = AppDataInfo.AccMileage >> 8;
  99. // GpsInfoData.AccMileage[3] = AppDataInfo.AccMileage;
  100. }
  101. p2 = strtok(p2, ","); //只取第2行的信息GGA
  102. memset(databuffer, 0x00, 20);
  103. if (strcmp(p2, "$GNGGA") == 0)
  104. {
  105. index = 0;
  106. while (p2)
  107. {
  108. databuffer[index] = p2;
  109. p2 = strtok(NULL, ",");
  110. index++;
  111. }
  112. if (index > 9 && (strcmp(databuffer[6], "1") == 0 || strcmp(databuffer[6], "2") == 0))
  113. {
  114. GpsInfoData.satelliteNum = atol(databuffer[7]); //卫星数目写入
  115. strdel(databuffer[9], '.');
  116. uint16 alt = 0;
  117. alt = atol(databuffer[9]) / 10 + 1000;
  118. GpsInfoData.altitude[0] = alt >> 8; //海拔
  119. GpsInfoData.altitude[1] = alt;
  120. strdel(databuffer[2], '.');
  121. strdel(databuffer[4], '.');
  122. latitude = location_handle(databuffer[2]);
  123. GpsInfoData.latitude[0] = latitude >> 24;
  124. GpsInfoData.latitude[1] = latitude >> 16;
  125. GpsInfoData.latitude[2] = latitude >> 8;
  126. GpsInfoData.latitude[3] = latitude;
  127. longitude = location_handle(databuffer[4]);
  128. GpsInfoData.longitude[0] = longitude >> 24;
  129. GpsInfoData.longitude[1] = longitude >> 16;
  130. GpsInfoData.longitude[2] = longitude >> 8;
  131. GpsInfoData.longitude[3] = longitude;
  132. }
  133. }
  134. xQueueOverwrite(GpsDataQueueHandle,(uint8 *)&GpsInfoData);
  135. }
  136. void strdel(char *str, char c)
  137. {
  138. char *p = str;
  139. while (*str)
  140. {
  141. if (*str != c)
  142. *p++ = *str;
  143. str++;
  144. }
  145. *p = '\0';
  146. }
  147. uint32 location_handle(char *in1)
  148. {
  149. uint32 location_temp;
  150. uint32 location_degree;
  151. uint32 location_min;
  152. location_temp = atol(in1);
  153. location_degree = location_temp / (1e7);
  154. location_degree = location_degree * (1e6);
  155. location_min = location_temp - location_degree * 10;
  156. location_min = location_min / 6;
  157. location_temp = location_degree + location_min;
  158. return location_temp;
  159. }