GpsData.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /************************************************************************************
  2. * File Name: GpsData.c
  3. * Function Describe:处理GPS模块发回的数据,提取到sutGpsInfo
  4. * Relate Module:
  5. * Explain:
  6. * Writer: ShiLiangWen
  7. * Date: 2015.5.26
  8. $GPGGA,235957.003,,,,,0,0,,,M,,M,,*44
  9. $GPGSA,A,1,,,,,,,,,,,,,,,*1E
  10. $GPGSV,1,1,00*79
  11. $GPRMC,235957.003,V,,,,,0.00,0.00,050180,,,N*4D
  12. *************************************************************************************/
  13. #define THIS_FILE_ID 10
  14. //--------------------------------------------------------------------------------------------
  15. #include "includes.h"
  16. GPS_DATA_TYPE GPS_Data_Type;
  17. SUT_GPS_INF sutGpsInfo;
  18. /*************************************************************************************
  19. *
  20. **************************************************************************************/
  21. int HEX_2_INT32(int hex_val)
  22. {
  23. char tmp_buf[13] = {0};
  24. int val;
  25. snprintf(tmp_buf, sizeof(tmp_buf), "%x", hex_val);
  26. val = atoi (tmp_buf);
  27. return val;
  28. }
  29. /*************************************************************************************
  30. *
  31. **************************************************************************************/
  32. static unsigned char translate_digitAscii_to_bcd(char *ascii_number, unsigned char len, char *bcd_number)
  33. {
  34. int i, j;
  35. char temp[10] = ""; /* extra + and NULL */
  36. unsigned char bcd_index = 0; /* Index into output bcd_number */
  37. int num_len = len;
  38. /* Translate or skip over invalid characters */
  39. for (i = 0, j = 0; i < num_len; i++)
  40. {
  41. temp[j++] = ascii_number[i] & 0x0F;
  42. }
  43. ascii_number[len] = 0;
  44. /* Now that temp has the bcd codes in natural order... Squish them together
  45. * and reverse the order per bcd coding.
  46. */
  47. for (i = 0; i < j; i+=2)
  48. bcd_number[bcd_index++] = ((char)(temp[i] << 4)) | temp[i+1];
  49. bcd_number[len] = 0;
  50. return 0;
  51. }
  52. /*=====================================
  53. UTC time:hms status latitue north? longitue east? 速度(节) 方位角 UTC time 磁偏角 方向 模式 checksum
  54. $GPRMC hhmmss.sss A/V ddmm.mmmm N/S dddmm.mmmm E/W speed aspect ddmmyy 000-180 E/W A/D/E/N *hh
  55. For example: $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
  56. */
  57. #define GPRMC_UTC1_TOKEN_INDEX 0
  58. #define GPRMC_STATUS_TOKEN_INDEX 1
  59. #define GPRMC_LATITUE_TOKEN_INDEX 2
  60. #define GPRMC_NORTH_TOKEN_INDEX 3
  61. #define GPRMC_longitue_TOKEN_INDEX 4
  62. #define GPRMC_EAST_TOKEN_INDEX 5
  63. #define GPRMC_SPEED_TOKEN_INDEX 6
  64. #define GPRMC_ASPECT_TOKEN_INDEX 7
  65. #define GPRMC_UTC2_TOKEN_INDEX 8
  66. #define GPRMC_HIGHT_TOKEN_INDEX 8
  67. #define GPRMC_STARTNUM_TOKEN_INDEX 6
  68. /*************************************************************************************
  69. *
  70. **************************************************************************************/
  71. void process_gps_data(char *buf,uint32_t size) //这里的buf就是从GPRMC,后开始
  72. {
  73. char tmp_char[16] = {0};
  74. char bcd_tmp[8] = {0};
  75. char *token[16] = {NULL};
  76. char size_each_token[16] = {0};
  77. unsigned char hh,hl;
  78. unsigned char token_index = 0;
  79. unsigned char j=0;
  80. int i = 0;
  81. int index = 0;
  82. int dig_bit_num = 0;
  83. int ptr_each = 0;
  84. unsigned short speed;
  85. unsigned short aspect;
  86. // char* g_cGetlongitude=NULL;
  87. // char* g_cGetlatitude=NULL;
  88. double speed_jie = 0.0;
  89. token[token_index] = buf;
  90. RunMake(THIS_FILE_ID);
  91. while(index < size )
  92. {
  93. if(buf[index] == ',')
  94. {
  95. size_each_token[token_index] = ptr_each;
  96. if(ptr_each == 0) token[token_index] = NULL;
  97. //*(token[token_index] + ptr_each) = 0; //
  98. ptr_each = 0;
  99. token_index++;
  100. token[token_index] = buf+index+1; //skip this ','
  101. if(*(token[token_index]+1) == '*')
  102. {
  103. size_each_token[token_index] = 1; //cut of *hh\r\n
  104. }
  105. }
  106. else
  107. {
  108. ptr_each++;
  109. }
  110. index++;
  111. }
  112. RunMake(THIS_FILE_ID);
  113. if(GPS_Data_Type==GNRMC)
  114. {
  115. //UTC time:hms
  116. if(token[GPRMC_UTC1_TOKEN_INDEX])
  117. {
  118. memset(tmp_char , 0 ,sizeof(tmp_char));
  119. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  120. strncpy((char*)tmp_char,(char*)token[0],size_each_token[0]);
  121. #if 1
  122. for(i = 6; i < 9;i++)
  123. tmp_char[i] = tmp_char[i+1];
  124. tmp_char[i]=0;
  125. sutGpsInfo.SendTime=atoi(tmp_char); //311410000
  126. #else
  127. translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp); //去掉.xx
  128. //小时需要加8 因为北京是东8区
  129. hl=bcd_tmp[0]&0x0f;
  130. hh=bcd_tmp[0]&0xf0;
  131. hl+=8;
  132. if(hl>=10){
  133. hl-=10;
  134. hh+=0x10;
  135. //if(hh>0x20)hh=0; 处理GPS时间问题 V520_P1
  136. }
  137. sutGpsInfo.hour=(hh | hl);
  138. sutGpsInfo.minu = bcd_tmp[1];
  139. sutGpsInfo.sec = bcd_tmp[2];
  140. #endif
  141. }
  142. RunMake(THIS_FILE_ID);
  143. //Fix status
  144. if(token[GPRMC_STATUS_TOKEN_INDEX])
  145. {
  146. memset(tmp_char , 0 ,sizeof(tmp_char));
  147. strncpy((char*)tmp_char,(char*)token[1],size_each_token[1]);
  148. //if(*tmp_char == 'A'){
  149. if(*tmp_char == 'A'){
  150. sutGpsInfo.isGpsValid = 1;
  151. //SlwTrace(INF,"sutGpsInfo.isGpsValid1!",1);
  152. }else{
  153. sutGpsInfo.isGpsValid = 0;
  154. //SlwTrace(INF,"sutGpsInfo.isGpsNOtValid1!------------------",1);
  155. }
  156. }
  157. RunMake(THIS_FILE_ID);
  158. if(sutGpsInfo.isGpsValid == 0)return;
  159. //latitude 纬度
  160. /*=====================================
  161. UTC time:hms status latitue north? longitue east? 速度(节) 方位角 UTC time 磁偏角 方向 模式 checksum
  162. $GPRMC hhmmss.ss A/V ddmm.mmmm N/S dddmm.mmmm E/W speed aspect ddmmyy 000-180 E/W A/D/E/N *hh
  163. For example: $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,010.5,324.7,150706,,,A*50
  164. */
  165. if(token[GPRMC_LATITUE_TOKEN_INDEX])
  166. {
  167. RunMake(THIS_FILE_ID);
  168. memset(tmp_char , 0 ,sizeof(tmp_char));
  169. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  170. strncpy((char*)(tmp_char+1),(char*)token[2],size_each_token[2]);
  171. tmp_char[0] = '0';
  172. RunMake(THIS_FILE_ID);
  173. for(i = 5; i < 8;i++)
  174. tmp_char[i] = tmp_char[i+1];
  175. tmp_char[8] = 0; //小数点后面去掉最后1位
  176. translate_digitAscii_to_bcd(tmp_char,8,bcd_tmp); //去掉.xx
  177. //North or south
  178. memset(tmp_char , 0 ,sizeof(tmp_char));
  179. if(token[GPRMC_NORTH_TOKEN_INDEX])
  180. {
  181. strncpy((char*)tmp_char,(char*)token[3],size_each_token[3]);
  182. // if(*tmp_char == 'S')
  183. // bcd_tmp[0] |= 0x80;
  184. sutGpsInfo.NSFlag=*tmp_char;
  185. sutGpsInfo.latitue = (bcd_tmp[3] | (bcd_tmp[2] << 8) |(bcd_tmp[1] << 16) |(bcd_tmp[0] << 24));
  186. }
  187. RunMake(THIS_FILE_ID);
  188. }
  189. //longitude 经度
  190. if(token[GPRMC_longitue_TOKEN_INDEX])
  191. {
  192. memset(tmp_char , 0 ,sizeof(tmp_char));
  193. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  194. RunMake(THIS_FILE_ID);
  195. strncpy((char*)tmp_char,(char*)token[4],size_each_token[4]);
  196. for(i = 5; i < 8;i++) tmp_char[i] = tmp_char[i+1];
  197. tmp_char[8] = 0;
  198. translate_digitAscii_to_bcd(tmp_char,8,bcd_tmp); //去掉.xx
  199. RunMake(THIS_FILE_ID);
  200. //east or west
  201. if(token[GPRMC_EAST_TOKEN_INDEX])
  202. {
  203. memset(tmp_char , 0 ,sizeof(tmp_char));
  204. strncpy((char*)tmp_char,(char*)token[5],size_each_token[5]);
  205. // if(*tmp_char == 'W')
  206. // bcd_tmp[0] |= 0x80;
  207. sutGpsInfo.EWFlag=*tmp_char;
  208. sutGpsInfo.longitue = (bcd_tmp[3] | (bcd_tmp[2] << 8) |(bcd_tmp[1] << 16) |(bcd_tmp[0] << 24));
  209. }
  210. }
  211. //Speed
  212. if(token[GPRMC_SPEED_TOKEN_INDEX])
  213. {
  214. RunMake(THIS_FILE_ID);
  215. memset(tmp_char , 0 ,sizeof(tmp_char));
  216. strncpy((char*)tmp_char,(char*)token[GPRMC_SPEED_TOKEN_INDEX],size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
  217. index = 0;
  218. while(index < size_each_token[GPRMC_SPEED_TOKEN_INDEX])
  219. {
  220. if(tmp_char[index] == '.')
  221. break;
  222. index++;
  223. }
  224. dig_bit_num = size_each_token[GPRMC_SPEED_TOKEN_INDEX]-index-1;
  225. //这里,index代表小数点前有多少位,dig_bit_num代表小数点后面有多少位
  226. RunMake(THIS_FILE_ID);
  227. //ol_debug("dig_bit_num:%d,all len:%d",dig_bit_num,size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
  228. #if 0
  229. if(index == 3)
  230. {
  231. speed_jie = (tmp_char[0]-0x30)*100
  232. + (tmp_char[1]-0x30)*10
  233. + (tmp_char[2]-0x30);
  234. }
  235. else if(index == 2)
  236. {
  237. speed_jie = (tmp_char[0]-0x30)*10
  238. + (tmp_char[1]-0x30);
  239. }
  240. else
  241. speed_jie = (tmp_char[0]-0x30);
  242. if(dig_bit_num > 0)
  243. speed_jie += (tmp_char[index+1]-0x30)*0.1;
  244. #else
  245. speed_jie = atoi(tmp_char);
  246. if(dig_bit_num>0)
  247. speed_jie += (tmp_char[index+1]-0x30)*0.1;
  248. #endif
  249. speed = (unsigned short)(speed_jie*1.852);
  250. sutGpsInfo.speed = ( ((speed/100)<<8) | (((speed /10)%10)<<4) | (speed %10) );
  251. //ol_debug("speed_jie to speed:%d,xinan speed:%d",speed,sutGpsInfo.speed);
  252. }
  253. RunMake(THIS_FILE_ID);
  254. //Aspect 方向
  255. if(token[GPRMC_ASPECT_TOKEN_INDEX])
  256. {
  257. memset(tmp_char , 0 ,sizeof(tmp_char));
  258. strncpy((char*)tmp_char,(char*)token[GPRMC_ASPECT_TOKEN_INDEX],size_each_token[GPRMC_ASPECT_TOKEN_INDEX]);
  259. // #if 0
  260. // aspect = (unsigned short)((tmp_char[0]-0x30)*100
  261. // + (tmp_char[1]-0x30)*10
  262. // + (tmp_char[2]-0x30));
  263. // //aspect = 150;
  264. // sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
  265. //
  266. // #else
  267. // aspect=atoi(tmp_char);
  268. // aspect %=360;
  269. // sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
  270. // #endif
  271. i=0;
  272. j=0;
  273. while(tmp_char[i]){
  274. if(*tmp_char=='.'){
  275. //bcd_tmp
  276. i++;
  277. bcd_tmp[j]=tmp_char[i];
  278. break;
  279. }
  280. bcd_tmp[j]=tmp_char[i];
  281. j++;
  282. i++;
  283. }
  284. sutGpsInfo.aspect=atoi(bcd_tmp);
  285. //printf("get GPS aspect--------------------------%d-----------\r\n",sutGpsInfo.aspect);
  286. }
  287. RunMake(THIS_FILE_ID);
  288. //UTC2
  289. if(token[GPRMC_UTC2_TOKEN_INDEX])
  290. {
  291. memset(tmp_char , 0 ,sizeof(tmp_char));
  292. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  293. strncpy((char*)tmp_char,(char*)token[GPRMC_UTC2_TOKEN_INDEX],size_each_token[GPRMC_UTC2_TOKEN_INDEX]);
  294. translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp);
  295. sutGpsInfo.day = bcd_tmp[0];
  296. sutGpsInfo.month= bcd_tmp[1];
  297. sutGpsInfo.year = bcd_tmp[2];
  298. }
  299. }
  300. if(GPS_Data_Type==GNGGA){
  301. //printf("get GPS GNGGA-------------------------------------\r\n");
  302. if(token[GPRMC_STARTNUM_TOKEN_INDEX])
  303. {
  304. strncpy((char*)tmp_char,(char*)token[GPRMC_STARTNUM_TOKEN_INDEX],size_each_token[GPRMC_STARTNUM_TOKEN_INDEX]);
  305. // bcd_tmp[0]=tmp_char[0];
  306. // bcd_tmp[1]=tmp_char[1];
  307. // bcd_tmp[2]=0;
  308. sutGpsInfo.startnum=atoi(tmp_char);
  309. //printf("get GPS startnum--------------------------%d-----------\r\n",sutGpsInfo.startnum);
  310. }
  311. if(token[GPRMC_HIGHT_TOKEN_INDEX])
  312. {
  313. memset(tmp_char , 0 ,sizeof(tmp_char));
  314. strncpy((char*)tmp_char,(char*)token[GPRMC_HIGHT_TOKEN_INDEX],size_each_token[GPRMC_HIGHT_TOKEN_INDEX]);
  315. // bcd_tmp
  316. for(i=0,j=0;i<size_each_token[GPRMC_HIGHT_TOKEN_INDEX];i++){
  317. if(tmp_char[i]=='.'){
  318. i++;
  319. continue;
  320. }
  321. bcd_tmp[j]=tmp_char[i];
  322. i++;
  323. j++;
  324. }
  325. }
  326. sutGpsInfo.hight=atoi(bcd_tmp);
  327. // printf("get GPS hight--------------------%d---------\r\n",sutGpsInfo.hight);
  328. }
  329. }
  330. #ifdef GPS_DEBUG_VALID
  331. static unsigned char sucTimeH,sucTimeL;
  332. static unsigned long susTime=(7*3600+30*60+0);
  333. static unsigned long sucLatitue=0x02237542;//;
  334. static unsigned long sucLongitue=0x11401316;//;
  335. #endif
  336. /*************************************************************************************
  337. * 根据GPS模块返回的nema数据,提取并放入sutGpsInfo中
  338. //高度真航� 卫星数�
  339. **************************************************************************************/
  340. void process_nema(char *nema)
  341. {
  342. static int susTime=61200;
  343. uint8_t sucTimeH,sucTimeL;
  344. static unsigned char sucCt=0;
  345. int len;
  346. //GNRMC
  347. if(nema[0]=='$' && nema[3]=='R' && nema[5]=='C') {
  348. GPS_Data_Type=GNRMC;
  349. }else if(nema[0]=='$' && nema[4]=='G' && nema[5]=='A'){
  350. GPS_Data_Type=GNGGA;
  351. }else GPS_Data_Type=GPS_NULL;
  352. //GPGGA 字段 8 高度 6 可用卫星数
  353. if(GPS_Data_Type==GPS_NULL) return;
  354. if(++sucCt>2){
  355. sucCt=0;
  356. }
  357. len=strlen(&nema[7]);
  358. if(len>(UART3_RX_BUFFER_SIZE-10)){
  359. SlwTrace(INF,"UART2RECV OVER!",1);
  360. }
  361. //printf("buffer3==%s\r\n",nema);
  362. process_gps_data(&nema[7],len);
  363. sutGpsInfo.isGpsWork=1;
  364. sutGpsInfo.GpsOpened=1;
  365. sutGpsInfo.GpsInactiveCt=0;
  366. }