AppTaskGps.c 4.6 KB

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