123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- /************************************************************************************
- * File Name: GpsData.c
- * Function Describe:处理GPS模块发回的数据,提取到sutGpsInfo
- * Relate Module:
- * Explain:
- * Writer: ShiLiangWen
- * Date: 2015.5.26
- $GPGGA,235957.003,,,,,0,0,,,M,,M,,*44
- $GPGSA,A,1,,,,,,,,,,,,,,,*1E
- $GPGSV,1,1,00*79
- $GPRMC,235957.003,V,,,,,0.00,0.00,050180,,,N*4D
- *************************************************************************************/
- #define THIS_FILE_ID 10
- //--------------------------------------------------------------------------------------------
- #include "includes.h"
- SUT_GPS_INF sutGpsInfo;
- GPS_DATA_TYPE GPS_Data_Type;
- /*************************************************************************************
- *
- **************************************************************************************/
- int HEX_2_INT32(int hex_val)
- {
- char tmp_buf[13] = {0};
- int val;
- snprintf(tmp_buf, sizeof(tmp_buf), "%x", hex_val);
- val = atoi (tmp_buf);
- return val;
- }
- /*************************************************************************************
- *
- **************************************************************************************/
- static unsigned char translate_digitAscii_to_bcd(char *ascii_number, unsigned char len, char *bcd_number)
- {
- int i, j;
- char temp[10] = ""; /* extra + and NULL */
- unsigned char bcd_index = 0; /* Index into output bcd_number */
-
- int num_len = len;
-
-
- /* Translate or skip over invalid characters */
- for (i = 0, j = 0; i < num_len; i++)
- {
- temp[j++] = ascii_number[i] & 0x0F;
- }
-
- ascii_number[len] = 0;
- /* Now that temp has the bcd codes in natural order... Squish them together
- * and reverse the order per bcd coding.
- */
- for (i = 0; i < j; i+=2)
- bcd_number[bcd_index++] = ((char)(temp[i] << 4)) | temp[i+1];
-
- bcd_number[len] = 0;
-
- return 0;
- }
- /*=====================================
- UTC time:hms status latitue north? longitue east? 速度(节) 方位角 UTC time 磁偏角 方向 模式 checksum
- $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
- For example: $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
- */
- #define GPRMC_UTC1_TOKEN_INDEX 0
- #define GPRMC_STATUS_TOKEN_INDEX 1
- #define GPRMC_LATITUE_TOKEN_INDEX 2
- #define GPRMC_NORTH_TOKEN_INDEX 3
- #define GPRMC_longitue_TOKEN_INDEX 4
- #define GPRMC_EAST_TOKEN_INDEX 5
- #define GPRMC_SPEED_TOKEN_INDEX 6
- #define GPRMC_ASPECT_TOKEN_INDEX 7
- #define GPRMC_UTC2_TOKEN_INDEX 8
- #define GPRMC_HIGHT_TOKEN_INDEX 8
- #define GPRMC_STARTNUM_TOKEN_INDEX 6
- /*************************************************************************************
- *
- **************************************************************************************/
- void process_gps_data(char *buf,uint32_t size) //这里的buf就是从GPRMC,后开始
- {
- char tmp_char[16] = {0};
- char bcd_tmp[8] = {0};
- char *token[16] = {NULL};
- char size_each_token[16] = {0};
- unsigned char hh,hl;
- unsigned char token_index = 0;
- unsigned char j=0;
-
- int i = 0;
- int index = 0;
- int dig_bit_num = 0;
- int ptr_each = 0;
- unsigned short speed;
- unsigned short aspect;
- // char* g_cGetlongitude=NULL;
- // char* g_cGetlatitude=NULL;
- double speed_jie = 0.0;
-
- token[token_index] = buf;
- RunMake(THIS_FILE_ID);
- while(index < size )
- {
- if(buf[index] == ',')
- {
- size_each_token[token_index] = ptr_each;
- if(ptr_each == 0)
- token[token_index] = NULL;
- //*(token[token_index] + ptr_each) = 0; //
-
- ptr_each = 0;
- token_index++;
- token[token_index] = buf+index+1; //skip this ','
- if(*(token[token_index]+1) == '*')
- {
- size_each_token[token_index] = 1; //cut of *hh\r\n
- }
- }
- else
- {
- ptr_each++;
- }
- index++;
- }
- RunMake(THIS_FILE_ID);
- if(GPS_Data_Type==GNRMC)
- {
- //UTC time:hms
- if(token[GPRMC_UTC1_TOKEN_INDEX])
- {
- strncpy((char*)tmp_char,(char*)token[0],size_each_token[0]);
- translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp); //去掉.xx
- //小时需要加8 因为北京是东8区
- hl=bcd_tmp[0]&0x0f;
- hh=bcd_tmp[0]&0xf0;
- hl+=8;
- if(hl>=10){
- hl-=10;
- hh+=0x10;
- //if(hh>0x20)hh=0; 处理GPS时间问题 V520_P1
- }
- sutGpsInfo.hour=(hh | hl);
- sutGpsInfo.minu = bcd_tmp[1];
- sutGpsInfo.sec = bcd_tmp[2];
- }
- RunMake(THIS_FILE_ID);
- //Fix status
- if(token[GPRMC_STATUS_TOKEN_INDEX])
- {
- memset(tmp_char , 0 ,sizeof(tmp_char));
- strncpy((char*)tmp_char,(char*)token[1],size_each_token[1]);
- //if(*tmp_char == 'A'){
- if(*tmp_char == 'A'){
- sutGpsInfo.isGpsValid = 1;
-
- if(0==sutGpsInfo.locationflag){
- sutGpsInfo.locationTime=os_time_get()-sutGpsInfo.locationTime;
- sutGpsInfo.locationflag=1;
- }
- //SlwTrace(INF,"sutGpsInfo.isGpsValid1!",1);
- }else{
- sutGpsInfo.isGpsValid = 0;
- sutGpsInfo.locationflag=0;
-
- }
- }
- RunMake(THIS_FILE_ID);
- if(sutGpsInfo.isGpsValid == 0)return;
- //latitude 纬度
- /*=====================================
- UTC time:hms status latitue north? longitue east? 速度(节) 方位角 UTC time 磁偏角 方向 模式 checksum
- $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
- For example: $GPRMC,024813.640,A,3158.4608,N,11848.3737,E,010.5,324.7,150706,,,A*50
- */
- if(token[GPRMC_LATITUE_TOKEN_INDEX])
- {
- RunMake(THIS_FILE_ID);
- memset(tmp_char , 0 ,sizeof(tmp_char));
- memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
- strncpy((char*)(tmp_char+1),(char*)token[2],size_each_token[2]);
- tmp_char[0] = '0';
- RunMake(THIS_FILE_ID);
- for(i = 5; i < 8;i++)
- tmp_char[i] = tmp_char[i+1];
- tmp_char[8] = 0; //小数点后面去掉最后1位
- translate_digitAscii_to_bcd(tmp_char,8,bcd_tmp); //去掉.xx
- //North or south
- memset(tmp_char , 0 ,sizeof(tmp_char));
- if(token[GPRMC_NORTH_TOKEN_INDEX])
- {
- strncpy((char*)tmp_char,(char*)token[3],size_each_token[3]);
- // if(*tmp_char == 'S')
- // bcd_tmp[0] |= 0x80;
- sutGpsInfo.NSFlag=*tmp_char;
- sutGpsInfo.latitue = (bcd_tmp[3] | (bcd_tmp[2] << 8) |(bcd_tmp[1] << 16) |(bcd_tmp[0] << 24));
- }
- RunMake(THIS_FILE_ID);
- }
-
- //longitude 经度
- if(token[GPRMC_longitue_TOKEN_INDEX])
- {
- memset(tmp_char , 0 ,sizeof(tmp_char));
- memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
- RunMake(THIS_FILE_ID);
- strncpy((char*)tmp_char,(char*)token[4],size_each_token[4]);
- for(i = 5; i < 8;i++)
- tmp_char[i] = tmp_char[i+1];
- tmp_char[8] = 0;
- translate_digitAscii_to_bcd(tmp_char,8,bcd_tmp); //去掉.xx
- RunMake(THIS_FILE_ID);
- //east or west
- if(token[GPRMC_EAST_TOKEN_INDEX])
- {
- memset(tmp_char , 0 ,sizeof(tmp_char));
- strncpy((char*)tmp_char,(char*)token[5],size_each_token[5]);
- // if(*tmp_char == 'W')
- // bcd_tmp[0] |= 0x80;
- sutGpsInfo.EWFlag=*tmp_char;
- sutGpsInfo.longitue = (bcd_tmp[3] | (bcd_tmp[2] << 8) |(bcd_tmp[1] << 16) |(bcd_tmp[0] << 24));
- }
- }
-
- //Speed
- if(token[GPRMC_SPEED_TOKEN_INDEX])
- {
- RunMake(THIS_FILE_ID);
- memset(tmp_char , 0 ,sizeof(tmp_char));
- strncpy((char*)tmp_char,(char*)token[GPRMC_SPEED_TOKEN_INDEX],size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
- index = 0;
- while(index < size_each_token[GPRMC_SPEED_TOKEN_INDEX])
- {
- if(tmp_char[index] == '.')
- break;
- index++;
- }
- dig_bit_num = size_each_token[GPRMC_SPEED_TOKEN_INDEX]-index-1;
- //这里,index代表小数点前有多少位,dig_bit_num代表小数点后面有多少位
- RunMake(THIS_FILE_ID);
- //ol_debug("dig_bit_num:%d,all len:%d",dig_bit_num,size_each_token[GPRMC_SPEED_TOKEN_INDEX]);
- #if 0
- if(index == 3)
- {
- speed_jie = (tmp_char[0]-0x30)*100
- + (tmp_char[1]-0x30)*10
- + (tmp_char[2]-0x30);
- }
- else if(index == 2)
- {
- speed_jie = (tmp_char[0]-0x30)*10
- + (tmp_char[1]-0x30);
- }
- else
- speed_jie = (tmp_char[0]-0x30);
- if(dig_bit_num > 0)
- speed_jie += (tmp_char[index+1]-0x30)*0.1;
- #else
- speed_jie = atoi(tmp_char);
- if(dig_bit_num>0)
- speed_jie += (tmp_char[index+1]-0x30)*0.1;
- #endif
- speed = (unsigned short)(speed_jie*1.852);
-
- sutGpsInfo.speed = ( ((speed/100)<<8) | (((speed /10)%10)<<4) | (speed %10) );
- //ol_debug("speed_jie to speed:%d,xinan speed:%d",speed,sutGpsInfo.speed);
- }
- RunMake(THIS_FILE_ID);
- //Aspect 方向
- if(token[GPRMC_ASPECT_TOKEN_INDEX])
- {
- memset(tmp_char , 0 ,sizeof(tmp_char));
- strncpy((char*)tmp_char,(char*)token[GPRMC_ASPECT_TOKEN_INDEX],size_each_token[GPRMC_ASPECT_TOKEN_INDEX]);
- #if 0
- aspect = (unsigned short)((tmp_char[0]-0x30)*100
- + (tmp_char[1]-0x30)*10
- + (tmp_char[2]-0x30));
-
- //aspect = 150;
- sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
-
- #else
- aspect=atoi(tmp_char);
- aspect %=360;
- sutGpsInfo.aspect= ( ((aspect/100) << 8) | (((aspect /10)%10)<<4) | (aspect %10) );
- #endif
- }
- RunMake(THIS_FILE_ID);
- //UTC2
- if(token[GPRMC_UTC2_TOKEN_INDEX])
- {
- memset(tmp_char , 0 ,sizeof(tmp_char));
- memset(bcd_tmp , 0 ,sizeof(bcd_tmp));
- strncpy((char*)tmp_char,(char*)token[GPRMC_UTC2_TOKEN_INDEX],size_each_token[GPRMC_UTC2_TOKEN_INDEX]);
- translate_digitAscii_to_bcd(tmp_char,6,bcd_tmp);
-
- sutGpsInfo.day = bcd_tmp[0];
- sutGpsInfo.month= bcd_tmp[1];
- sutGpsInfo.year = bcd_tmp[2];
- }
- }
-
-
- if(GPS_Data_Type==GNGGA){
- //printf("get GPS GNGGA-------------------------------------\r\n");
-
- if(token[GPRMC_STARTNUM_TOKEN_INDEX])
- {
-
- strncpy((char*)tmp_char,(char*)token[GPRMC_STARTNUM_TOKEN_INDEX],size_each_token[GPRMC_STARTNUM_TOKEN_INDEX]);
-
-
- // bcd_tmp[0]=tmp_char[0];
- // bcd_tmp[1]=tmp_char[1];
- // bcd_tmp[2]=0;
- sutGpsInfo.startnum=atoi(tmp_char);
- //printf("get GPS startnum--------------------------%d-----------\r\n",sutGpsInfo.startnum);
- }
- if(token[GPRMC_HIGHT_TOKEN_INDEX])
- {
- memset(tmp_char , 0 ,sizeof(tmp_char));
- strncpy((char*)tmp_char,(char*)token[GPRMC_HIGHT_TOKEN_INDEX],size_each_token[GPRMC_HIGHT_TOKEN_INDEX]);
- // bcd_tmp
- for(i=0,j=0;i<size_each_token[GPRMC_HIGHT_TOKEN_INDEX];i++){
-
- if(tmp_char[i]=='.'){
-
- i++;
- continue;
- }
- bcd_tmp[j]=tmp_char[i];
- i++;
- j++;
- }
- }
- sutGpsInfo.hight=atoi(bcd_tmp);
- // printf("get GPS hight--------------------%d---------\r\n",sutGpsInfo.hight);
- }
-
- }
- #ifdef GPS_DEBUG_VALID
- static unsigned char sucTimeH,sucTimeL;
- static unsigned long susTime=(7*3600+30*60+0);
- static unsigned long sucLatitue=0x02237542;//;
- static unsigned long sucLongitue=0x11401316;//;
- #endif
- /*************************************************************************************
- * 根据GPS模块返回的nema数据,提取并放入sutGpsInfo中
- **************************************************************************************/
- void process_nema(char *nema)
- {
- static int susTime=61200;
- uint8_t sucTimeH,sucTimeL;
-
-
- static unsigned char sucCt=0;
- int len;
- // if(nema[0]!='$' || nema[3]!='R' || nema[5]!='C')return;
- //GNRMC
- if(nema[0]=='$' && nema[3]=='R' && nema[5]=='C') {
- GPS_Data_Type=GNRMC;
- }else if(nema[0]=='$' && nema[4]=='G' && nema[5]=='A'){
-
- GPS_Data_Type=GNGGA;
- }else GPS_Data_Type=GPS_NULL;
- //GPGGA 字段 8 高度 6 可用卫星数
-
- if(GPS_Data_Type==GPS_NULL) return;
-
- if(++sucCt>2){
- sucCt=0;
- }
- len=strlen(&nema[7]);
- if(len>(UART3_RX_BUFFER_SIZE-10)){
- SlwTrace(INF,"UART2RECV OVER!",1);
-
- }
- process_gps_data(&nema[7],len);
- sutGpsInfo.isGpsWork=1;
- sutGpsInfo.GpsInactiveCt=0;
- #if 1
- #ifdef GPS_DEBUG_VALID
- SlwTrace(DEBUG,"GPS_DEBUG_VALID!!!");
- if(sutGpsInfo.isGpsValid==0){
- sutGpsInfo.isGpsValid=1;
- sutGpsInfo.latitue=sucLatitue;
- sutGpsInfo.longitue=sucLongitue;
- sutGpsInfo.speed=0x4321;
- sutGpsInfo.aspect=0x7654;
- sutGpsInfo.StatisticsMile=0x123456;
- sutGpsInfo.year=0x15;
- sutGpsInfo.month=0x07;
- sutGpsInfo.day=0x26;
-
- susTime++;
- sutGpsInfo.hour=(unsigned char)(susTime/3600);
- sutGpsInfo.minu=(unsigned char)((susTime%3600)/60);
- sutGpsInfo.sec=(unsigned char)(susTime%60);
- sucTimeH=sutGpsInfo.hour/10;
- sucTimeL=sutGpsInfo.hour%10;
- sutGpsInfo.hour=(sucTimeH<<4)+sucTimeL;
-
- sucTimeH=sutGpsInfo.minu/10;
- sucTimeL=sutGpsInfo.minu%10;
- sutGpsInfo.minu=(sucTimeH<<4)+sucTimeL;
-
- sucTimeH=sutGpsInfo.sec/10;
- sucTimeL=sutGpsInfo.sec%10;
- sutGpsInfo.sec=(sucTimeH<<4)+sucTimeL;
- }
- #endif
- #else
- sutGpsInfo.isGpsValid=1;
- sutGpsInfo.latitue=0x4A993;//305555
- sutGpsInfo.longitue=0x1260DC;//1204444
- sutGpsInfo.speed=0x4321;
- sutGpsInfo.aspect=0x7654;
- sutGpsInfo.StatisticsMile=0x123456;
- sutGpsInfo.year=0x17;
- sutGpsInfo.month=0x11;
- sutGpsInfo.day=0x20;
-
- susTime++;
- sutGpsInfo.hour=(unsigned char)(susTime/3600);
- sutGpsInfo.minu=(unsigned char)((susTime%3600)/60);
- sutGpsInfo.sec=(unsigned char)(susTime%60);
- sucTimeH=sutGpsInfo.hour/10;
- sucTimeL=sutGpsInfo.hour%10;
- sutGpsInfo.hour=(sucTimeH<<4)+sucTimeL;
-
- sucTimeH=sutGpsInfo.minu/10;
- sucTimeL=sutGpsInfo.minu%10;
- sutGpsInfo.minu=(sucTimeH<<4)+sucTimeL;
-
- sucTimeH=sutGpsInfo.sec/10;
- sucTimeL=sutGpsInfo.sec%10;
- sutGpsInfo.sec=(sucTimeH<<4)+sucTimeL;
-
- sutGpsInfo.NSFlag = 'N';
- sutGpsInfo.EWFlag = 'E';
- #endif
- }
|