#include "includes.h" SUT_GPS_INF sutGpsInfo; SUT_MESS sutMess; const unsigned short GPS_TimeTable[GPS_TABLE_NUM]={0,5,10,15,30,60,120,300}; static unsigned char gpsInit=0; void GPSRestart(void){ gpsInit=1; } void GPSStop(void){ gpsInit=10; } void GPSInit(void){ static unsigned int tick; GPIO_InitTypeDef GPIO_InitStructure; if(gpsInit==0) return; else if(gpsInit==1){ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPS_PWREN_PIN; GPIO_Init(GPS_PWREN_PORT, &GPIO_InitStructure); SlwTrace(INF,"GPS Down...",1); GPS_PWREN_LOW; sutGpsInfo.isGpsWork=0; sutGpsInfo.isGpsValid=0; sutGpsInfo.GpsInactiveCt=0; uTimerStart(&tick, 50); gpsInit=2; }else if(gpsInit==2){ if(uTimerExpired(&tick)){ GPS_PWREN_HIGH; SlwTrace(INF,"GPS Start Done",1); gpsInit=0; } }else if(gpsInit==10){ GPS_PWREN_LOW; SlwTrace(INF,"GPS Down",1); sutGpsInfo.isGpsWork=0; sutGpsInfo.isGpsValid=0; gpsInit=0; } } static void GpsDataProcess(void); PT_THREAD (ptGpsTask(struct pt *pt)){ static timer_t ptTimer; static short GpsSendCt=0; char buf[70]; if(g_ucModemTaskEn==0)return 1;//w //GPSInit(); PT_BEGIN(pt); while(1){ //GpsDataProcess(); GpsInternalChange(); if(sutGpsInfo.isGpsValid && g_uiGpsStat&&GpsSendCt>GPS_TimeTable[newPara.gpsTimeIndex]&&hgs_poc_type==POC_BND){ //AT+POC=loc,22.23211,103.123121,1,23.4,12.5,4,2016/12/29-13:28:15 GpsSendCt=0; snprintf(buf,sizeof(buf),"AT+POC=loc,%d.%d,%d.%d,%d,23.4,12.5,4,%d/%d/%d-%d:%d:%d\r\n",sutGpsInfo.latitue/100000,sutGpsInfo.latitue-((sutGpsInfo.latitue/100000)*100000),sutGpsInfo.longitue/100000,sutGpsInfo.longitue-((sutGpsInfo.longitue/100000)*100000),\ 1,sutGpsInfo.year,sutGpsInfo.month,sutGpsInfo.day,sutGpsInfo.hour,sutGpsInfo.minu,sutGpsInfo.sec); ModemSendAt(buf); printf("GPS SEND=%s\r\n",buf); } PTTimerStart(&pt_timerPool, &ptTimer, 100);//GPS数据1秒才出一条RMC,0.5秒处理一次足够了 //50 PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer)); } PT_END(pt); } void GpsInternalChange() { static unsigned char cnt=2; static unsigned char last_uiGpsStat=2; if(last_uiGpsStat!=g_uiGpsStat){ cnt=0; last_uiGpsStat=g_uiGpsStat; } switch(cnt) { case 0: if(g_uiGpsStat){ ModemSendAt("AT+GPS=\"ON\"\r\n"); ModemSendAt("AT+VIBCTRL=\"POWER\",\"ON\"\r\n"); printf("GPS open--change--r\n"); } else { ModemSendAt("AT+GPS=\"OFF\"\r\n"); ModemSendAt("AT+VIBCTRL=\"POWER\",\"OFF\"\r\n"); } cnt=1; break; case 1: if(g_uiGpsStat)ModemSendAt("AT+GPSANT=1\r\n"); else ModemSendAt("AT+GPSANT=0\r\n"); cnt=2; break; } } static void GpsDataProcess(void) { char *nema=(char *)RxBuffer3; short len; if(g_usUart3RecvLen){ len=strlen(&nema[7]); if(len>(UART3_RX_BUFFER_SIZE-10)){ SlwTrace(INF,"UART2RECV OVER!",1); return; } process_gps_data(&nema[7],len); sutGpsInfo.isGpsWork=1; sutGpsInfo.GpsInactiveCt=0; //////////////////////////////////////////////// g_usUart3RecvLen=0; } } 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 /************************************************************************************* * **************************************************************************************/ 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; 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); //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; }else{ sutGpsInfo.isGpsValid = 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]; } }