AppTaskGps.c 4.7 KB

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