GpsData.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. SUT_GPS_INF sutGpsInfo;
  17. GPS_DATA_TYPE GPS_Data_Type;
  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)
  97. token[token_index] = NULL;
  98. //*(token[token_index] + ptr_each) = 0; //
  99. ptr_each = 0;
  100. token_index++;
  101. token[token_index] = buf+index+1; //skip this ','
  102. if(*(token[token_index]+1) == '*')
  103. {
  104. size_each_token[token_index] = 1; //cut of *hh\r\n
  105. }
  106. }
  107. else
  108. {
  109. ptr_each++;
  110. }
  111. index++;
  112. }
  113. RunMake(THIS_FILE_ID);
  114. if(GPS_Data_Type==GNRMC)
  115. {
  116. //UTC time:hms
  117. if(token[GPRMC_UTC1_TOKEN_INDEX])
  118. {
  119. strncpy((char*)tmp_char,(char*)token[0],size_each_token[0]);
  120. translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp); //去掉.xx
  121. //小时需要加8 因为北京是东8区
  122. hl=bcd_tmp[0]&0x0f;
  123. hh=bcd_tmp[0]&0xf0;
  124. hl+=8;
  125. if(hl>=10){
  126. hl-=10;
  127. hh+=0x10;
  128. //if(hh>0x20)hh=0; 处理GPS时间问题 V520_P1
  129. }
  130. sutGpsInfo.hour=(hh | hl);
  131. sutGpsInfo.minu = bcd_tmp[1];
  132. sutGpsInfo.sec = bcd_tmp[2];
  133. }
  134. RunMake(THIS_FILE_ID);
  135. //Fix status
  136. if(token[GPRMC_STATUS_TOKEN_INDEX])
  137. {
  138. memset(tmp_char , 0 ,sizeof(tmp_char));
  139. strncpy((char*)tmp_char,(char*)token[1],size_each_token[1]);
  140. //if(*tmp_char == 'A'){
  141. if(*tmp_char == 'A'){
  142. sutGpsInfo.isGpsValid = 1;
  143. if(0==sutGpsInfo.locationflag){
  144. sutGpsInfo.locationTime=os_time_get()-sutGpsInfo.locationTime;
  145. sutGpsInfo.locationflag=1;
  146. }
  147. //SlwTrace(INF,"sutGpsInfo.isGpsValid1!",1);
  148. }else{
  149. sutGpsInfo.isGpsValid = 0;
  150. sutGpsInfo.locationflag=0;
  151. }
  152. }
  153. RunMake(THIS_FILE_ID);
  154. if(sutGpsInfo.isGpsValid == 0)return;
  155. //latitude 纬度
  156. /*=====================================
  157. UTC time:hms status latitue north? longitue east? 速度(节) 方位角 UTC time 磁偏角 方向 模式 checksum
  158. $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
  159. For example: $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,010.5,324.7,150706,,,A*50
  160. */
  161. if(token[GPRMC_LATITUE_TOKEN_INDEX])
  162. {
  163. RunMake(THIS_FILE_ID);
  164. memset(tmp_char , 0 ,sizeof(tmp_char));
  165. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  166. strncpy((char*)(tmp_char+1),(char*)token[2],size_each_token[2]);
  167. tmp_char[0] = '0';
  168. RunMake(THIS_FILE_ID);
  169. for(i = 5; i < 8;i++)
  170. tmp_char[i] = tmp_char[i+1];
  171. tmp_char[8] = 0; //小数点后面去掉最后1位
  172. translate_digitAscii_to_bcd(tmp_char,8,bcd_tmp); //去掉.xx
  173. //North or south
  174. memset(tmp_char , 0 ,sizeof(tmp_char));
  175. if(token[GPRMC_NORTH_TOKEN_INDEX])
  176. {
  177. strncpy((char*)tmp_char,(char*)token[3],size_each_token[3]);
  178. // if(*tmp_char == 'S')
  179. // bcd_tmp[0] |= 0x80;
  180. sutGpsInfo.NSFlag=*tmp_char;
  181. sutGpsInfo.latitue = (bcd_tmp[3] | (bcd_tmp[2] << 8) |(bcd_tmp[1] << 16) |(bcd_tmp[0] << 24));
  182. }
  183. RunMake(THIS_FILE_ID);
  184. }
  185. //longitude 经度
  186. if(token[GPRMC_longitue_TOKEN_INDEX])
  187. {
  188. memset(tmp_char , 0 ,sizeof(tmp_char));
  189. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  190. RunMake(THIS_FILE_ID);
  191. strncpy((char*)tmp_char,(char*)token[4],size_each_token[4]);
  192. for(i = 5; i < 8;i++)
  193. tmp_char[i] = tmp_char[i+1];
  194. tmp_char[8] = 0;
  195. translate_digitAscii_to_bcd(tmp_char,8,bcd_tmp); //去掉.xx
  196. RunMake(THIS_FILE_ID);
  197. //east or west
  198. if(token[GPRMC_EAST_TOKEN_INDEX])
  199. {
  200. memset(tmp_char , 0 ,sizeof(tmp_char));
  201. strncpy((char*)tmp_char,(char*)token[5],size_each_token[5]);
  202. // if(*tmp_char == 'W')
  203. // bcd_tmp[0] |= 0x80;
  204. sutGpsInfo.EWFlag=*tmp_char;
  205. sutGpsInfo.longitue = (bcd_tmp[3] | (bcd_tmp[2] << 8) |(bcd_tmp[1] << 16) |(bcd_tmp[0] << 24));
  206. }
  207. }
  208. //Speed
  209. if(token[GPRMC_SPEED_TOKEN_INDEX])
  210. {
  211. RunMake(THIS_FILE_ID);
  212. memset(tmp_char , 0 ,sizeof(tmp_char));
  213. strncpy((char*)tmp_char,(char*)token[GPRMC_SPEED_TOKEN_INDEX],size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
  214. index = 0;
  215. while(index < size_each_token[GPRMC_SPEED_TOKEN_INDEX])
  216. {
  217. if(tmp_char[index] == '.')
  218. break;
  219. index++;
  220. }
  221. dig_bit_num = size_each_token[GPRMC_SPEED_TOKEN_INDEX]-index-1;
  222. //这里,index代表小数点前有多少位,dig_bit_num代表小数点后面有多少位
  223. RunMake(THIS_FILE_ID);
  224. //ol_debug("dig_bit_num:%d,all len:%d",dig_bit_num,size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
  225. #if 0
  226. if(index == 3)
  227. {
  228. speed_jie = (tmp_char[0]-0x30)*100
  229. + (tmp_char[1]-0x30)*10
  230. + (tmp_char[2]-0x30);
  231. }
  232. else if(index == 2)
  233. {
  234. speed_jie = (tmp_char[0]-0x30)*10
  235. + (tmp_char[1]-0x30);
  236. }
  237. else
  238. speed_jie = (tmp_char[0]-0x30);
  239. if(dig_bit_num > 0)
  240. speed_jie += (tmp_char[index+1]-0x30)*0.1;
  241. #else
  242. speed_jie = atoi(tmp_char);
  243. if(dig_bit_num>0)
  244. speed_jie += (tmp_char[index+1]-0x30)*0.1;
  245. #endif
  246. speed = (unsigned short)(speed_jie*1.852);
  247. sutGpsInfo.speed = ( ((speed/100)<<8) | (((speed /10)%10)<<4) | (speed %10) );
  248. //ol_debug("speed_jie to speed:%d,xinan speed:%d",speed,sutGpsInfo.speed);
  249. }
  250. RunMake(THIS_FILE_ID);
  251. //Aspect 方向
  252. if(token[GPRMC_ASPECT_TOKEN_INDEX])
  253. {
  254. memset(tmp_char , 0 ,sizeof(tmp_char));
  255. strncpy((char*)tmp_char,(char*)token[GPRMC_ASPECT_TOKEN_INDEX],size_each_token[GPRMC_ASPECT_TOKEN_INDEX]);
  256. #if 0
  257. aspect = (unsigned short)((tmp_char[0]-0x30)*100
  258. + (tmp_char[1]-0x30)*10
  259. + (tmp_char[2]-0x30));
  260. //aspect = 150;
  261. sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
  262. #else
  263. aspect=atoi(tmp_char);
  264. aspect %=360;
  265. sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
  266. #endif
  267. }
  268. RunMake(THIS_FILE_ID);
  269. //UTC2
  270. if(token[GPRMC_UTC2_TOKEN_INDEX])
  271. {
  272. memset(tmp_char , 0 ,sizeof(tmp_char));
  273. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  274. strncpy((char*)tmp_char,(char*)token[GPRMC_UTC2_TOKEN_INDEX],size_each_token[GPRMC_UTC2_TOKEN_INDEX]);
  275. translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp);
  276. sutGpsInfo.day = bcd_tmp[0];
  277. sutGpsInfo.month= bcd_tmp[1];
  278. sutGpsInfo.year = bcd_tmp[2];
  279. }
  280. }
  281. if(GPS_Data_Type==GNGGA){
  282. //printf("get GPS GNGGA-------------------------------------\r\n");
  283. if(token[GPRMC_STARTNUM_TOKEN_INDEX])
  284. {
  285. strncpy((char*)tmp_char,(char*)token[GPRMC_STARTNUM_TOKEN_INDEX],size_each_token[GPRMC_STARTNUM_TOKEN_INDEX]);
  286. // bcd_tmp[0]=tmp_char[0];
  287. // bcd_tmp[1]=tmp_char[1];
  288. // bcd_tmp[2]=0;
  289. sutGpsInfo.startnum=atoi(tmp_char);
  290. //printf("get GPS startnum--------------------------%d-----------\r\n",sutGpsInfo.startnum);
  291. }
  292. if(token[GPRMC_HIGHT_TOKEN_INDEX])
  293. {
  294. memset(tmp_char , 0 ,sizeof(tmp_char));
  295. strncpy((char*)tmp_char,(char*)token[GPRMC_HIGHT_TOKEN_INDEX],size_each_token[GPRMC_HIGHT_TOKEN_INDEX]);
  296. // bcd_tmp
  297. for(i=0,j=0;i<size_each_token[GPRMC_HIGHT_TOKEN_INDEX];i++){
  298. if(tmp_char[i]=='.'){
  299. i++;
  300. continue;
  301. }
  302. bcd_tmp[j]=tmp_char[i];
  303. i++;
  304. j++;
  305. }
  306. }
  307. sutGpsInfo.hight=atoi(bcd_tmp);
  308. // printf("get GPS hight--------------------%d---------\r\n",sutGpsInfo.hight);
  309. }
  310. }
  311. #ifdef GPS_DEBUG_VALID
  312. static unsigned char sucTimeH,sucTimeL;
  313. static unsigned long susTime=(7*3600+30*60+0);
  314. static unsigned long sucLatitue=0x02237542;//;
  315. static unsigned long sucLongitue=0x11401316;//;
  316. #endif
  317. /*************************************************************************************
  318. * 根据GPS模块返回的nema数据,提取并放入sutGpsInfo中
  319. **************************************************************************************/
  320. void process_nema(char *nema)
  321. {
  322. static int susTime=61200;
  323. uint8_t sucTimeH,sucTimeL;
  324. static unsigned char sucCt=0;
  325. int len;
  326. // if(nema[0]!='$' || nema[3]!='R' || nema[5]!='C')return;
  327. //GNRMC
  328. if(nema[0]=='$' && nema[3]=='R' && nema[5]=='C') {
  329. GPS_Data_Type=GNRMC;
  330. }else if(nema[0]=='$' && nema[4]=='G' && nema[5]=='A'){
  331. GPS_Data_Type=GNGGA;
  332. }else GPS_Data_Type=GPS_NULL;
  333. //GPGGA 字段 8 高度 6 可用卫星数
  334. if(GPS_Data_Type==GPS_NULL) return;
  335. if(++sucCt>2){
  336. sucCt=0;
  337. }
  338. len=strlen(&nema[7]);
  339. if(len>(UART3_RX_BUFFER_SIZE-10)){
  340. SlwTrace(INF,"UART2RECV OVER!",1);
  341. }
  342. process_gps_data(&nema[7],len);
  343. sutGpsInfo.isGpsWork=1;
  344. sutGpsInfo.GpsInactiveCt=0;
  345. #if 1
  346. #ifdef GPS_DEBUG_VALID
  347. SlwTrace(DEBUG,"GPS_DEBUG_VALID!!!");
  348. if(sutGpsInfo.isGpsValid==0){
  349. sutGpsInfo.isGpsValid=1;
  350. sutGpsInfo.latitue=sucLatitue;
  351. sutGpsInfo.longitue=sucLongitue;
  352. sutGpsInfo.speed=0x4321;
  353. sutGpsInfo.aspect=0x7654;
  354. sutGpsInfo.StatisticsMile=0x123456;
  355. sutGpsInfo.year=0x15;
  356. sutGpsInfo.month=0x07;
  357. sutGpsInfo.day=0x26;
  358. susTime++;
  359. sutGpsInfo.hour=(unsigned char)(susTime/3600);
  360. sutGpsInfo.minu=(unsigned char)((susTime%3600)/60);
  361. sutGpsInfo.sec=(unsigned char)(susTime%60);
  362. sucTimeH=sutGpsInfo.hour/10;
  363. sucTimeL=sutGpsInfo.hour%10;
  364. sutGpsInfo.hour=(sucTimeH<<4)+sucTimeL;
  365. sucTimeH=sutGpsInfo.minu/10;
  366. sucTimeL=sutGpsInfo.minu%10;
  367. sutGpsInfo.minu=(sucTimeH<<4)+sucTimeL;
  368. sucTimeH=sutGpsInfo.sec/10;
  369. sucTimeL=sutGpsInfo.sec%10;
  370. sutGpsInfo.sec=(sucTimeH<<4)+sucTimeL;
  371. }
  372. #endif
  373. #else
  374. sutGpsInfo.isGpsValid=1;
  375. sutGpsInfo.latitue=0x4A993;//305555
  376. sutGpsInfo.longitue=0x1260DC;//1204444
  377. sutGpsInfo.speed=0x4321;
  378. sutGpsInfo.aspect=0x7654;
  379. sutGpsInfo.StatisticsMile=0x123456;
  380. sutGpsInfo.year=0x17;
  381. sutGpsInfo.month=0x11;
  382. sutGpsInfo.day=0x20;
  383. susTime++;
  384. sutGpsInfo.hour=(unsigned char)(susTime/3600);
  385. sutGpsInfo.minu=(unsigned char)((susTime%3600)/60);
  386. sutGpsInfo.sec=(unsigned char)(susTime%60);
  387. sucTimeH=sutGpsInfo.hour/10;
  388. sucTimeL=sutGpsInfo.hour%10;
  389. sutGpsInfo.hour=(sucTimeH<<4)+sucTimeL;
  390. sucTimeH=sutGpsInfo.minu/10;
  391. sucTimeL=sutGpsInfo.minu%10;
  392. sutGpsInfo.minu=(sucTimeH<<4)+sucTimeL;
  393. sucTimeH=sutGpsInfo.sec/10;
  394. sucTimeL=sutGpsInfo.sec%10;
  395. sutGpsInfo.sec=(sucTimeH<<4)+sucTimeL;
  396. sutGpsInfo.NSFlag = 'N';
  397. sutGpsInfo.EWFlag = 'E';
  398. #endif
  399. }