GpsData.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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(GPS_TimeTable[newPara.gpsTimeIndex])
  18. {
  19. sutGpsInfo.g_GpsEnable=1;
  20. }
  21. else{
  22. sutGpsInfo.g_GpsEnable=0;
  23. sutGpsInfo.isGpsWork=0;
  24. }
  25. MSG_INFO(1,"newPara.gpsTimeIndex===%d,%d",newPara.gpsTimeIndex,sutGpsInfo.g_GpsEnable);
  26. }
  27. void GpsInternalChange(unsigned int interval)
  28. {
  29. static signed char step=-1;
  30. static char last_g_GpsStat=-1;
  31. static unsigned short cnt=0;
  32. if(++cnt<(1000/interval)) return;//1秒读一次ADC即可
  33. cnt=0;
  34. if(last_g_GpsStat!=sutGpsInfo.g_GpsEnable){
  35. last_g_GpsStat=sutGpsInfo.g_GpsEnable;
  36. step=0;
  37. }
  38. switch(step)
  39. {
  40. case 0:
  41. if(sutGpsInfo.g_GpsEnable){
  42. msgToModem("AT+GPS=\"ON\"\r\n");
  43. MSG_INFO(1,"GPS open--change--r\n");
  44. }
  45. else {
  46. msgToModem("AT+GPS=\"OFF\"\r\n");
  47. gpsDataInit();
  48. MSG_INFO(1,"GPS OFF--change--r\n");
  49. }
  50. //step=1;
  51. break;
  52. case 1:
  53. if(sutGpsInfo.g_GpsEnable)msgToModem("AT+GPSANT=1\r\n");
  54. else msgToModem("AT+GPSANT=0\r\n");
  55. //step=2;
  56. break;
  57. case 7: //第一次查询需要延时查询
  58. //step=0;
  59. gpsCheckInfo();
  60. break;
  61. break;
  62. }
  63. if(step<8)step++;
  64. }
  65. //12345,1214
  66. //鑾峰彇绗�竴涓�€楀彿鍓嶆暟瀛楁湁鍑犱綅
  67. int getDlen(char *msg){
  68. int i;
  69. for(i=0;i<strlen(msg);i++){
  70. if(msg[i]>0x39 || msg[i]<0x30){
  71. return i;
  72. }
  73. }
  74. return -1;//娌℃湁閫楀彿
  75. }
  76. static unsigned char translate_digitAscii_to_bcd(char *ascii_number, unsigned char len, char *bcd_number)
  77. {
  78. int i, j;
  79. char temp[10] = ""; /* extra + and NULL */
  80. unsigned char bcd_index = 0; /* Index into output bcd_number */
  81. int num_len = len;
  82. /* Translate or skip over invalid characters */
  83. for (i = 0, j = 0; i < num_len; i++)
  84. {
  85. temp[j++] = ascii_number[i] & 0x0F;
  86. }
  87. ascii_number[len] = 0;
  88. /* Now that temp has the bcd codes in natural order... Squish them together
  89. * and reverse the order per bcd coding.
  90. */
  91. for (i = 0; i < j; i+=2)
  92. bcd_number[bcd_index++] = ((char)(temp[i] << 4)) | temp[i+1];
  93. bcd_number[len] = 0;
  94. return 0;
  95. }
  96. /*=====================================
  97. UTC time:hms status latitue north? longitue east? 速度(节) 方位角 UTC time 磁偏角 方向 模式 checksum
  98. $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
  99. For example: $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
  100. */
  101. #define GPRMC_UTC1_TOKEN_INDEX 0
  102. #define GPRMC_STATUS_TOKEN_INDEX 1
  103. #define GPRMC_LATITUE_TOKEN_INDEX 2
  104. #define GPRMC_NORTH_TOKEN_INDEX 3
  105. #define GPRMC_longitue_TOKEN_INDEX 4
  106. #define GPRMC_EAST_TOKEN_INDEX 5
  107. #define GPRMC_SPEED_TOKEN_INDEX 6
  108. #define GPRMC_ASPECT_TOKEN_INDEX 7
  109. #define GPRMC_UTC2_TOKEN_INDEX 8
  110. #define GPRMC_HIGHT_TOKEN_INDEX 8
  111. #define GPRMC_STARTNUM_TOKEN_INDEX 6
  112. #define GPS_BDVISUAL_INDEX 2
  113. /*************************************************************************************
  114. *
  115. **************************************************************************************/
  116. void process_gps_data(char *buf,uint32_t size) //这里的buf就是从GPRMC,后开始
  117. {
  118. char tmp_char[16] = {0};
  119. char bcd_tmp[8] = {0};
  120. char *token[16] = {NULL};
  121. char size_each_token[16] = {0};//长度
  122. unsigned char hh,hl;
  123. unsigned char token_index = 0;
  124. unsigned char j;
  125. int i = 0;
  126. int index = 0;
  127. int dig_bit_num = 0;
  128. int ptr_each = 0;
  129. unsigned short speed;
  130. unsigned short aspect;
  131. double speed_jie = 0.0;
  132. GPS_Data_Type=GNRMC;
  133. sutGpsInfo.isGpsWork=1;
  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. }
  236. if(token[GPRMC_NORTH_TOKEN_INDEX])
  237. {
  238. strncpy((char*)tmp_char,(char*)token[3],size_each_token[3]);
  239. sutGpsInfo.NSFlag=*tmp_char;
  240. }
  241. //longitude 经度
  242. if(token[GPRMC_longitue_TOKEN_INDEX])
  243. {
  244. char *p=token[GPRMC_longitue_TOKEN_INDEX];
  245. int dddmm=atoi(p);
  246. int mmmmm=atoi((p+6));
  247. int ddd=dddmm/100;
  248. int mm=dddmm%100;
  249. int dlen=getDlen(p+6);
  250. if(dlen<0){
  251. sutGpsInfo.isGpsValid = 0;
  252. return;
  253. }
  254. dlen = 6-dlen;
  255. sutGpsInfo.longitue=ddd*1000000;//露脠 路脜麓贸1000000卤露 脮没脢媒虏驴路脰
  256. mm*=1000000;
  257. if(dlen==1) mmmmm*=10;
  258. else if(dlen==2) mmmmm*=100;
  259. else if(dlen==3) mmmmm*=1000;
  260. else if(dlen==4) mmmmm*=10000;
  261. else mmmmm*=100000;
  262. mm+=mmmmm;//路脰 路脜麓贸1000000卤露
  263. ddd=mm/60;//露脠 路脜麓贸1000000卤露 脨隆脢媒虏驴路脰
  264. sutGpsInfo.longitue+=ddd;
  265. if(token[GPRMC_EAST_TOKEN_INDEX])
  266. {
  267. strncpy((char*)tmp_char,(char*)token[5],size_each_token[5]);
  268. sutGpsInfo.EWFlag=*tmp_char;
  269. }
  270. }
  271. //Speed
  272. if(token[GPRMC_SPEED_TOKEN_INDEX])
  273. {
  274. //RunMake(THIS_FILE_ID);
  275. memset(tmp_char , 0 ,sizeof(tmp_char));
  276. strncpy((char*)tmp_char,(char*)token[GPRMC_SPEED_TOKEN_INDEX],size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
  277. index = 0;
  278. while(index < size_each_token[GPRMC_SPEED_TOKEN_INDEX])
  279. {
  280. if(tmp_char[index] == '.')
  281. break;
  282. index++;
  283. }
  284. dig_bit_num = size_each_token[GPRMC_SPEED_TOKEN_INDEX]-index-1;
  285. //这里,index代表小数点前有多少位,dig_bit_num代表小数点后面有多少位
  286. //RunMake(THIS_FILE_ID);
  287. //ol_debug("dig_bit_num:%d,all len:%d",dig_bit_num,size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
  288. #if 0
  289. if(index == 3)
  290. {
  291. speed_jie = (tmp_char[0]-0x30)*100
  292. + (tmp_char[1]-0x30)*10
  293. + (tmp_char[2]-0x30);
  294. }
  295. else if(index == 2)
  296. {
  297. speed_jie = (tmp_char[0]-0x30)*10
  298. + (tmp_char[1]-0x30);
  299. }
  300. else
  301. speed_jie = (tmp_char[0]-0x30);
  302. if(dig_bit_num > 0)
  303. speed_jie += (tmp_char[index+1]-0x30)*0.1;
  304. #else
  305. speed_jie = atoi(tmp_char);
  306. if(dig_bit_num>0)
  307. speed_jie += (tmp_char[index+1]-0x30)*0.1;
  308. #endif
  309. speed = (unsigned short)(speed_jie*1.852);
  310. sutGpsInfo.speed = ( ((speed/100)<<8) | (((speed /10)%10)<<4) | (speed %10) );
  311. //ol_debug("speed_jie to speed:%d,xinan speed:%d",speed,sutGpsInfo.speed);
  312. }
  313. //RunMake(THIS_FILE_ID);
  314. //Aspect 方向
  315. if(token[GPRMC_ASPECT_TOKEN_INDEX])
  316. {
  317. memset(tmp_char , 0 ,sizeof(tmp_char));
  318. strncpy((char*)tmp_char,(char*)token[GPRMC_ASPECT_TOKEN_INDEX],size_each_token[GPRMC_ASPECT_TOKEN_INDEX]);
  319. #if 0
  320. aspect = (unsigned short)((tmp_char[0]-0x30)*100
  321. + (tmp_char[1]-0x30)*10
  322. + (tmp_char[2]-0x30));
  323. //aspect = 150;
  324. sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
  325. #else
  326. aspect=atoi(tmp_char);
  327. aspect %=360;
  328. sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
  329. #endif
  330. }
  331. //RunMake(THIS_FILE_ID);
  332. //UTC2
  333. if(token[GPRMC_UTC2_TOKEN_INDEX])
  334. {
  335. memset(tmp_char , 0 ,sizeof(tmp_char));
  336. memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
  337. strncpy((char*)tmp_char,(char*)token[GPRMC_UTC2_TOKEN_INDEX],size_each_token[GPRMC_UTC2_TOKEN_INDEX]);
  338. translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp);
  339. sutGpsInfo.day = bcd_tmp[0];
  340. sutGpsInfo.month= bcd_tmp[1];
  341. sutGpsInfo.year = bcd_tmp[2];
  342. }
  343. break;
  344. case GPVTG:
  345. break;
  346. case GNGGA:
  347. if(token[GPRMC_STARTNUM_TOKEN_INDEX])
  348. {
  349. strncpy((char*)tmp_char,(char*)token[GPRMC_STARTNUM_TOKEN_INDEX],size_each_token[GPRMC_STARTNUM_TOKEN_INDEX]);
  350. if(size_each_token[GPRMC_STARTNUM_TOKEN_INDEX]<1)return;
  351. // bcd_tmp[0]=tmp_char[0];
  352. // bcd_tmp[1]=tmp_char[1];
  353. // bcd_tmp[2]=0;
  354. sutGpsInfo.startnum=atoi(tmp_char);
  355. }
  356. if(token[GPRMC_HIGHT_TOKEN_INDEX])
  357. {
  358. memset(tmp_char , 0 ,sizeof(tmp_char));
  359. strncpy((char*)tmp_char,(char*)token[GPRMC_HIGHT_TOKEN_INDEX],size_each_token[GPRMC_HIGHT_TOKEN_INDEX]);
  360. // bcd_tmp
  361. for(i=0,j=0;i<size_each_token[GPRMC_HIGHT_TOKEN_INDEX];i++){
  362. if(tmp_char[i]=='.'){
  363. i++;
  364. continue;
  365. }
  366. bcd_tmp[j]=tmp_char[i];
  367. i++;
  368. j++;
  369. }
  370. }
  371. sutGpsInfo.hight=atoi(bcd_tmp);
  372. // printf("get GPS hight--------------------%d---------\r\n",sutGpsInfo.hight);
  373. break;
  374. case GPGSV:
  375. if(token[GPS_BDVISUAL_INDEX])
  376. {
  377. strncpy((char*)tmp_char,(char*)token[GPS_BDVISUAL_INDEX],size_each_token[GPS_BDVISUAL_INDEX]);
  378. sutGpsInfo.GPSVisible_num=atoi(tmp_char);
  379. }
  380. break;
  381. case GBGSV:
  382. if(token[GPS_BDVISUAL_INDEX])
  383. {
  384. strncpy((char*)tmp_char,(char*)token[GPS_BDVISUAL_INDEX],size_each_token[GPS_BDVISUAL_INDEX]);
  385. sutGpsInfo.BDVisible_num=atoi(tmp_char);
  386. }
  387. break;
  388. }
  389. }