GpsData.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #include "includes.h"
  2. SUT_GPS_INF sutGpsInfo;
  3. GPS_DATA_TYPE GPS_Data_Type;
  4. //static unsigned char gpsInit=0;
  5. //
  6. //void GPSRestart(void){
  7. // gpsInit=1;
  8. //}
  9. //void GPSStop(void){
  10. // gpsInit=10;
  11. //}
  12. void GPSInit(void){
  13. memset(&sutGpsInfo,0,sizeof(SUT_GPS_INF));
  14. }
  15. void GpsTimeUpdate(void)
  16. {
  17. if(newPara.gpsEnable){
  18. sutGpsInfo.g_GpsEnable=1;
  19. }else if(newPara.gpsEnable==0){
  20. sutGpsInfo.g_GpsEnable=0;
  21. sutGpsInfo.isGpsWork=0;
  22. }
  23. MSG_INFO(1,"newPara.gpsTimeIndex===%d,%d",newPara.gpsTimeIndex,sutGpsInfo.g_GpsEnable);
  24. }
  25. void GpsInternalChange(unsigned int interval)
  26. {
  27. static signed char step=-1;
  28. static char last_g_GpsStat=-1;
  29. static unsigned short cnt=0;
  30. if(++cnt<(1000/interval)) return;
  31. cnt=0;
  32. if(sutGpsInfo.recnt<5){
  33. sutGpsInfo.recnt++;
  34. return;
  35. }
  36. if(last_g_GpsStat!=sutGpsInfo.g_GpsEnable){
  37. last_g_GpsStat=sutGpsInfo.g_GpsEnable;
  38. step=0;
  39. }
  40. switch(step)
  41. {
  42. case 0:
  43. if(sutGpsInfo.g_GpsEnable){
  44. msgAtSend("AT+GPS=\"ON\"\r\n");
  45. MSG_INFO(1,"GPS open--change--r\n");
  46. }
  47. else {
  48. msgAtSend("AT+GPS=\"OFF\"\r\n");
  49. gpsDataInit();
  50. MSG_INFO(1,"GPS OFF--change--r\n");
  51. }
  52. //step=1;
  53. break;
  54. case 1:
  55. if(sutGpsInfo.g_GpsEnable)msgAtSend("AT+GPSANT=1\r\n");
  56. else msgAtSend("AT+GPSANT=0\r\n");
  57. //step=2;
  58. break;
  59. case 7: //第一次查询需要延时查询
  60. //step=0;
  61. gpsCheckInfo();
  62. break;
  63. break;
  64. }
  65. if(step<8)step++;
  66. }
  67. //12345,1214
  68. //鑾峰彇绗�竴涓�€楀彿鍓嶆暟瀛楁湁鍑犱綅
  69. int getDlen(char *msg){
  70. int i;
  71. for(i=0;i<strlen(msg);i++){
  72. if(msg[i]>0x39 || msg[i]<0x30){
  73. return i;
  74. }
  75. }
  76. return -1;//娌℃湁閫楀彿
  77. }
  78. static unsigned char translate_digitAscii_to_bcd(char *ascii_number, unsigned char len, char *bcd_number)
  79. {
  80. int i, j;
  81. char temp[10] = ""; /* extra + and NULL */
  82. unsigned char bcd_index = 0; /* Index into output bcd_number */
  83. int num_len = len;
  84. /* Translate or skip over invalid characters */
  85. for (i = 0, j = 0; i < num_len; i++)
  86. {
  87. temp[j++] = ascii_number[i] & 0x0F;
  88. }
  89. ascii_number[len] = 0;
  90. /* Now that temp has the bcd codes in natural order... Squish them together
  91. * and reverse the order per bcd coding.
  92. */
  93. for (i = 0; i < j; i+=2)
  94. bcd_number[bcd_index++] = ((char)(temp[i] << 4)) | temp[i+1];
  95. bcd_number[len] = 0;
  96. return 0;
  97. }
  98. /*=====================================
  99. UTC time:hms status latitue north? longitue east? 速度(节) 方位角 UTC time 磁偏角 方向 模式 checksum
  100. $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
  101. For example: $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
  102. */
  103. #define GPRMC_UTC1_TOKEN_INDEX 0
  104. #define GPRMC_STATUS_TOKEN_INDEX 1
  105. #define GPRMC_LATITUE_TOKEN_INDEX 2
  106. #define GPRMC_NORTH_TOKEN_INDEX 3
  107. #define GPRMC_longitue_TOKEN_INDEX 4
  108. #define GPRMC_EAST_TOKEN_INDEX 5
  109. #define GPRMC_SPEED_TOKEN_INDEX 6
  110. #define GPRMC_ASPECT_TOKEN_INDEX 7
  111. #define GPRMC_UTC2_TOKEN_INDEX 8
  112. #define GPRMC_HIGHT_TOKEN_INDEX 8
  113. #define GPRMC_STARTNUM_TOKEN_INDEX 6
  114. #define GPS_BDVISUAL_INDEX 2
  115. /*************************************************************************************
  116. *
  117. **************************************************************************************/
  118. void process_gps_data(char *buf,uint32_t size) //这里的buf就是从GPRMC,后开始
  119. {
  120. char tmp_char[16] = {0};
  121. char bcd_tmp[8] = {0};
  122. char *token[16] = {NULL};
  123. char size_each_token[16] = {0};//长度
  124. unsigned char hh,hl;
  125. unsigned char token_index = 0;
  126. unsigned char j;
  127. int i = 0;
  128. int index = 0;
  129. int dig_bit_num = 0;
  130. int ptr_each = 0;
  131. unsigned short speed;
  132. unsigned short aspect;
  133. double speed_jie = 0.0;
  134. if(GPS_Data_Type==GBGSV|| GPS_Data_Type==GPGSV)return;
  135. token[token_index] = buf;
  136. //RunMake(THIS_FILE_ID);
  137. while(index < size )
  138. {
  139. if(buf[index] == ',')
  140. {
  141. size_each_token[token_index] = ptr_each;
  142. if(ptr_each == 0) token[token_index] = NULL;
  143. //*(token[token_index] + ptr_each) = 0; //
  144. ptr_each = 0;
  145. token_index++;
  146. if(token_index==16)return;
  147. token[token_index] = buf+index+1; //skip this ','
  148. if(*(token[token_index]+1) == '*')
  149. {
  150. size_each_token[token_index] = 1; //cut of *hh\r\n
  151. }
  152. }
  153. else
  154. {
  155. ptr_each++;
  156. }
  157. index++;
  158. }
  159. /*
  160. GPS_NULL,
  161. GNRMC=1,
  162. GPVTG,
  163. GNGGA,
  164. GPGSV,
  165. GBGSV,
  166. */
  167. //RunMake(THIS_FILE_ID);
  168. switch(GPS_Data_Type){
  169. case GNRMC:
  170. //UTC time:hms
  171. if(token[GPRMC_UTC1_TOKEN_INDEX])
  172. {
  173. strncpy((char*)tmp_char,(char*)token[0],size_each_token[0]);
  174. translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp); //去掉.xx
  175. //小时需要加8 因为北京是东8区
  176. hl=bcd_tmp[0]&0x0f;
  177. hh=bcd_tmp[0]&0xf0;
  178. hl+=8;
  179. if(hl>=10){
  180. hl-=10;
  181. hh+=0x10;
  182. //if(hh>0x20)hh=0; 处理GPS时间问题 V520_P1
  183. }
  184. sutGpsInfo.hour=(hh | hl);
  185. sutGpsInfo.minu = bcd_tmp[1];
  186. sutGpsInfo.sec = bcd_tmp[2];
  187. }
  188. //RunMake(THIS_FILE_ID);
  189. //Fix status
  190. if(token[GPRMC_STATUS_TOKEN_INDEX])
  191. {
  192. memset(tmp_char , 0 ,sizeof(tmp_char));
  193. strncpy((char*)tmp_char,(char*)token[1],size_each_token[1]);
  194. //if(*tmp_char == 'A'){
  195. if(*tmp_char == 'A'){
  196. sutGpsInfo.isGpsValid = 1;
  197. }else{
  198. sutGpsInfo.isGpsValid = 0;
  199. }
  200. }
  201. //RunMake(THIS_FILE_ID);
  202. if(sutGpsInfo.isGpsValid == 0)return;
  203. //latitude 纬度
  204. /*=====================================
  205. UTC time:hms status latitue north? longitue east? 速度(节) 方位角 UTC time 磁偏角 方向 模式 checksum
  206. $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
  207. For example: $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,010.5,324.7,150706,,,A*50
  208. */
  209. if(token[GPRMC_LATITUE_TOKEN_INDEX])
  210. {
  211. char *p=token[GPRMC_LATITUE_TOKEN_INDEX];
  212. //ddmm.mmmmm
  213. int ddmm=atoi(p);
  214. int mmmmm=atoi((p+5));
  215. int dd=ddmm/100;
  216. int mm=ddmm%100;
  217. int dlen=getDlen(p+5);
  218. if(dlen<0){
  219. sutGpsInfo.isGpsValid = 0;
  220. return;
  221. }
  222. dlen = 6-dlen;
  223. //double fmm=(double)mm+(double)mmmmm/100000.0;
  224. //nmeaInfo.latitude=(double)dd+fmm/60.0;
  225. sutGpsInfo.latitue=dd*1000000;//露脠 路脜麓贸1000000卤露拢卢脮没脢媒虏驴路脰
  226. mm*=1000000;
  227. if(dlen==1) mmmmm*=10;
  228. else if(dlen==2) mmmmm*=100;
  229. else if(dlen==3) mmmmm*=1000;
  230. else if(dlen==4) mmmmm*=10000;
  231. else mmmmm*=100000;
  232. mm+=mmmmm;//路脰 路脜麓贸1000000卤露
  233. dd=mm/60;//露脠 路脜麓贸1000000卤露 脨隆脢媒虏驴路脰
  234. sutGpsInfo.latitue+=dd;//露脠 路脜麓贸1000000卤露
  235. p[7]=0;
  236. sutGpsInfo.App_latitue=atof(p);
  237. }
  238. if(token[GPRMC_NORTH_TOKEN_INDEX])
  239. {
  240. strncpy((char*)tmp_char,(char*)token[3],size_each_token[3]);
  241. sutGpsInfo.NSFlag=*tmp_char;
  242. }
  243. //longitude 经度
  244. if(token[GPRMC_longitue_TOKEN_INDEX])
  245. {
  246. char *p=token[GPRMC_longitue_TOKEN_INDEX];
  247. int dddmm=atoi(p);
  248. int mmmmm=atoi((p+6));
  249. int ddd=dddmm/100;
  250. int mm=dddmm%100;
  251. int dlen=getDlen(p+6);
  252. if(dlen<0){
  253. sutGpsInfo.isGpsValid = 0;
  254. return;
  255. }
  256. dlen = 6-dlen;
  257. sutGpsInfo.longitue=ddd*1000000;//露脠 路脜麓贸1000000卤露 脮没脢媒虏驴路脰
  258. mm*=1000000;
  259. if(dlen==1) mmmmm*=10;
  260. else if(dlen==2) mmmmm*=100;
  261. else if(dlen==3) mmmmm*=1000;
  262. else if(dlen==4) mmmmm*=10000;
  263. else mmmmm*=100000;
  264. mm+=mmmmm;//路脰 路脜麓贸1000000卤露
  265. ddd=mm/60;//露脠 路脜麓贸1000000卤露 脨隆脢媒虏驴路脰
  266. sutGpsInfo.longitue+=ddd;
  267. p[8]=0;
  268. sutGpsInfo.App_longitue=atof(p);
  269. if(token[GPRMC_EAST_TOKEN_INDEX])
  270. {
  271. strncpy((char*)tmp_char,(char*)token[5],size_each_token[5]);
  272. sutGpsInfo.EWFlag=*tmp_char;
  273. }
  274. }
  275. //Speed
  276. if(token[GPRMC_SPEED_TOKEN_INDEX])
  277. {
  278. //RunMake(THIS_FILE_ID);
  279. memset(tmp_char , 0 ,sizeof(tmp_char));
  280. strncpy((char*)tmp_char,(char*)token[GPRMC_SPEED_TOKEN_INDEX],size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
  281. index = 0;
  282. while(index < size_each_token[GPRMC_SPEED_TOKEN_INDEX])
  283. {
  284. if(tmp_char[index] == '.')
  285. break;
  286. index++;
  287. }
  288. dig_bit_num = size_each_token[GPRMC_SPEED_TOKEN_INDEX]-index-1;
  289. //这里,index代表小数点前有多少位,dig_bit_num代表小数点后面有多少位
  290. //RunMake(THIS_FILE_ID);
  291. //ol_debug("dig_bit_num:%d,all len:%d",dig_bit_num,size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
  292. #if 0
  293. if(index == 3)
  294. {
  295. speed_jie = (tmp_char[0]-0x30)*100
  296. + (tmp_char[1]-0x30)*10
  297. + (tmp_char[2]-0x30);
  298. }
  299. else if(index == 2)
  300. {
  301. speed_jie = (tmp_char[0]-0x30)*10
  302. + (tmp_char[1]-0x30);
  303. }
  304. else
  305. speed_jie = (tmp_char[0]-0x30);
  306. if(dig_bit_num > 0)
  307. speed_jie += (tmp_char[index+1]-0x30)*0.1;
  308. #else
  309. speed_jie = atoi(tmp_char);
  310. if(dig_bit_num>0)
  311. speed_jie += (tmp_char[index+1]-0x30)*0.1;
  312. #endif
  313. speed = (unsigned short)(speed_jie*1.852);
  314. //sutGpsInfo.speed = ( ((speed/100)<<8) | (((speed /10)%10)<<4) | (speed %10) );
  315. sutGpsInfo.speed=speed;
  316. //ol_debug("speed_jie to speed:%d,xinan speed:%d",speed,sutGpsInfo.speed);
  317. }
  318. //RunMake(THIS_FILE_ID);
  319. //Aspect 方向
  320. if(token[GPRMC_ASPECT_TOKEN_INDEX])
  321. {
  322. memset(tmp_char , 0 ,sizeof(tmp_char));
  323. strncpy((char*)tmp_char,(char*)token[GPRMC_ASPECT_TOKEN_INDEX],size_each_token[GPRMC_ASPECT_TOKEN_INDEX]);
  324. #if 0
  325. aspect = (unsigned short)((tmp_char[0]-0x30)*100
  326. + (tmp_char[1]-0x30)*10
  327. + (tmp_char[2]-0x30));
  328. //aspect = 150;
  329. sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
  330. #else
  331. aspect=atoi(tmp_char);
  332. aspect %=360;
  333. //sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
  334. sutGpsInfo.aspect=aspect;
  335. #endif
  336. }
  337. //RunMake(THIS_FILE_ID);
  338. //UTC2
  339. if(token[GPRMC_UTC2_TOKEN_INDEX])
  340. {
  341. memset(tmp_char , 0 ,sizeof(tmp_char));
  342. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  343. strncpy((char*)tmp_char,(char*)token[GPRMC_UTC2_TOKEN_INDEX],size_each_token[GPRMC_UTC2_TOKEN_INDEX]);
  344. translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp);
  345. sutGpsInfo.day = bcd_tmp[0];
  346. sutGpsInfo.month= bcd_tmp[1];
  347. sutGpsInfo.year = bcd_tmp[2];
  348. }
  349. break;
  350. case GPVTG:
  351. break;
  352. case GNGGA:
  353. if(token[GPRMC_STARTNUM_TOKEN_INDEX])
  354. {
  355. strncpy((char*)tmp_char,(char*)token[GPRMC_STARTNUM_TOKEN_INDEX],size_each_token[GPRMC_STARTNUM_TOKEN_INDEX]);
  356. if(size_each_token[GPRMC_STARTNUM_TOKEN_INDEX]<1)return;
  357. // bcd_tmp[0]=tmp_char[0];
  358. // bcd_tmp[1]=tmp_char[1];
  359. // bcd_tmp[2]=0;
  360. sutGpsInfo.startnum=atoi(tmp_char);
  361. }
  362. if(token[GPRMC_HIGHT_TOKEN_INDEX])
  363. {
  364. memset(tmp_char , 0 ,sizeof(tmp_char));
  365. strncpy((char*)tmp_char,(char*)token[GPRMC_HIGHT_TOKEN_INDEX],size_each_token[GPRMC_HIGHT_TOKEN_INDEX]);
  366. /*
  367. // bcd_tmp
  368. for(i=0,j=0;i<size_each_token[GPRMC_HIGHT_TOKEN_INDEX];i++){
  369. if(tmp_char[i]=='.'){
  370. i++;
  371. continue;
  372. }
  373. bcd_tmp[j]=tmp_char[i];
  374. i++;
  375. j++;
  376. }
  377. }
  378. // sutGpsInfo.hight=atoi(bcd_tmp);
  379. */
  380. sutGpsInfo.hight=atoi(tmp_char);
  381. //MSG_INFO(1,"get GPS hight--------------------%d---------\r\n",sutGpsInfo.hight);
  382. }
  383. break;
  384. case GPGSV:
  385. if(token[GPS_BDVISUAL_INDEX])
  386. {
  387. strncpy((char*)tmp_char,(char*)token[GPS_BDVISUAL_INDEX],size_each_token[GPS_BDVISUAL_INDEX]);
  388. sutGpsInfo.GPSVisible_num=atoi(tmp_char);
  389. }
  390. break;
  391. case GBGSV:
  392. if(token[GPS_BDVISUAL_INDEX])
  393. {
  394. strncpy((char*)tmp_char,(char*)token[GPS_BDVISUAL_INDEX],size_each_token[GPS_BDVISUAL_INDEX]);
  395. sutGpsInfo.BDVisible_num=atoi(tmp_char);
  396. }
  397. break;
  398. }
  399. }