⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 handlegpsdata.c

📁 一个用来实现GPS数据接收和处理分析的程序
💻 C
📖 第 1 页 / 共 2 页
字号:
				if(temp == 'N')
					gpsdatt.Year = 0x80;
				else if(temp == 'S')
					gpsdatt.Year = 0x00;
				else return(0);
				printf("%d\n",temp);
				break;
			case 5:
				//处理经度-度
				printf("====Longtitue========\n");
				j = 1;
				do
				{
					j++;
					tempdot = p[i + j];
					if(j > 8)
						break;
				}while(tempdot != '.');
				printf("%d ",tempdot);
				printf("%d ",temp);
				temp &= 0x0F;
				if(j > 2)
				{
					temp &= 0x0F;
					temp = (temp << 3) + (temp << 1);
					temp += (p[i++] & 0x0F);
					if(j > 3)
					{
						temp = (temp << 3) + (temp << 1);
						temp += (p[i++] & 0x0F);
					}
				}
				gpsdatt.Longitue[0] = temp;
				gpsdatt.Longitue[0] &= 0xFF;
				
				//处理经度-分
				temp = p[i++];
				printf("%d ",temp);
				temp &= 0x0F;
				temp = (temp << 3) + (temp << 1);
				temp += (p[i++] & 0x0F);
				printf("%d ",temp);
				gpsdatt.Longitue[1] = temp;
				gpsdatt.Longitue[1] &= 0xFF;
	
				//处理经度-分小数
				i++;
				temp = p[i++];
				printf("%d ",temp);
				temp &= 0x0F;
				temp = (temp << 3) + (temp << 1);
				temp += (p[i++] & 0x0F);
				printf("%d ",temp);
				//temp1 = temp * 100;		//分小数高位
				gpsdatt.Longitue[2] = temp;
				gpsdatt.Longitue[2] &= 0xFF;
				
				temp = p[i++];
				printf("%d ",temp);
				temp &= 0x0F;
				temp = (temp << 3) + (temp << 1);
				temp += (p[i++] & 0x0F);	//分小数低位
				printf("%d ",temp);
				//temp1 += temp;
				gpsdatt.Longitue[3] = temp;
				gpsdatt.Longitue[3] &= 0xFF;
				printf("%x,%x,%x,%x\n",gpsdatt.Longitue[0],gpsdatt.Longitue[1], \
				gpsdatt.Longitue[2],gpsdatt.Longitue[3]);
				break;
			case 6:
				//经度-东经 or 西经
				if(temp == 'E')
					gpsdatt.Year |= 0x40;
				else if(temp == 'W')
					gpsdatt.Year &= 0xBF;
				else return(0);
				printf("%d\n",temp);
				break;
			case 7:
				printf("=====speed========\n");
				temp &= 0x0F;
				tempdot = temp;
				for(;;)
				{
					temp = p[i] & 0x0F;
					printf("%d ",temp);
					if(temp == 0x0C)// || temp == 0x0E)
						break;
					
					if(temp == 0x0E)	//temp = '.' or temp = ','
					{
						i++;
						temp = p[i];
						if(temp >= '5')
							tempdot++;
						break;
					}
					i++;
					tempdot = tempdot * 10 + temp;
				}
				gpsdatt.Speed = tempdot;
				gpsdatt.Speed &= 0xFF;
				printf("%x\n",gpsdatt.Speed);
				break;
			case 8:
				printf("===Course====\n");
				temp &= 0x0F;
				j = (int)temp;
				for(;;)
				{
					temp = p[i] & 0x0F;
					printf("%d,",temp);
					if(temp == 0x0E || temp == 0x0C)
						break;
					i++;
					j = j * 10 + temp;
				}
				j >>= 1;
				gpsdatt.Course = (unsigned char) j;
				gpsdatt.Course &= 0xFF;
				printf("%x\n",gpsdatt.Course);
				break;
			case 9:
				printf("====year====\n");
				temp &= 0x0F;
				printf("%d ",temp);
				temp = (temp << 3) + (temp << 1);
				temp += (p[i++] & 0x0F);
				gpsdatt.Date = temp;
				gpsdatt.Date &= 0xFF;
				
				temp = p[i++];
				temp &= 0x0F;
				printf("%d ",temp);
				temp = (temp << 3) + (temp << 1);
				temp += (p[i++] & 0x0F);
				gpsdatt.Month = temp;
				gpsdatt.Month &= 0xFF;
				
				temp = p[i++];
				
				temp &= 0x0F;
				printf("%d ",temp);
				temp = (temp << 3) + (temp << 1);
				temp += (p[i++] & 0x0F);
				gpsdatt.Year = temp;
				gpsdatt.Year &= 0xFF;
				//gpsdatt.Year |= temp;
				printf("%d,%d,%d\n",gpsdatt.Year,gpsdatt.Month,gpsdatt.Date);
				//return(setflag);
				//goto end;
				break;
			default:
				break;
			}
			
			do
			{
				temp = p[i++];
				if(i > length)
					break;
			}while(temp!=',');
			i--;	
		}      
	}
//end:
	if(setflag == 1)
	{
		memcpy((char*)&gpsdat,(char*)&gpsdatt, 16);
	}
	return(setflag);
}
/*******************************************************************************************
//名称 :Handle_GpsData
//功能: 预处理读出的GPS数据
//输入参数: 1.读入的原始GPS数据,2,处理GPS数据的函数指针
//输出参数: 处理状态是否成功
//返回: 成功,返回1,失败返回0
******************************************************************************************/
int Handle_GpsData(char *sentence,HandleGpsFrame Handle_Gps_Data)
{
 	char rmc_status = 0;
	int done = 0x7F,Okflag = 0,len;
	unsigned char *ptGPS;
	unsigned char ch,checksum = 0,chedat;
	
	Okflag = Verify_Gps_Data(sentence);
	if(Okflag!=SUCCESSVERIFYGPSDATA) 
		return ERRVERIFYGPSDATA;
	sentence++;
	if(strncmp(sentence,"GPRMC",5)==0)
	{
		rmc_status = 1;
		printf("sentence:%s\n",sentence);
		done = Handle_Gps_Data(sentence);   //处理GPS RMC类型数据
	}
	if(rmc_status)
	{
		
	}
	return (done);		
}
/*******************************************************************************************
//名称 :GetGpsData
//功能: 从GPS模块缓冲区中读取GPS数据
//输入参数: 用来处理GPS数据的函数指针
//输出参数: 无
//返回: 无
*************关于GetGpsData函数从GPS模块中读取数据并调用数据处理程序,实现的例子:***************************
************说明:为了提高效率,所以采用每次按块字节读取,而不会丢失数据处理,所以会把第一次没有处理完的数据暂存起来*************
buff [  ]  ={     "$GPRMC,234911.567,A,2233.6237,N,11405.5020,E,6.65,271.48,300604,,*06\r\n$GPRMC,234912.567,A,2233.6241,N,"}  
 步骤1:HeaderInfo=GPRMC,234911.567,A,2233.6237,N,11405.5020,E,6.65,271.48,300604,,*06\r\n$GPRMC,234912.567,A,2233.6241,N,";
 步骤2:GpsInfo=GPRMC,234911.567,A,2233.6237,N,11405.5020,E,6.65,271.48,300604,,*06\r\n$GPRMC,234912.567,A,2233.6241,N,";
 步骤3:EndInfo="$GPRMC,234912.567,A,2233.6241,N,";  并调用数据处理程序处理GpsInfo
  步骤4:数据处理程序返回,清空GpsInfo,把EndInfo复制到GpsInfo中,GpsInfo=$GPRMC,234912.567,A,2233.6241,N,";  
  步骤5:把GpsInfo中的数据复制到GpsBuf中,开始第二次数据读取
******************************************************************************************/
void GetGpsData(HandleGpsFrame Handgps)
{
	int done=0,len=0;
	//unsigned char buff[MAXREADBUFSIZE];
	unsigned char GpsInfo[240];
	unsigned char buff[100];
	unsigned char GpsBuf[240];
	unsigned char tmpbuf[240];
	unsigned char *HeaderInfo;
	unsigned char *EndInfo;
	unsigned char recgps;
	int flag;
	memset(GpsBuf,0,240);
	while(1)
	{
		recgps = TRUE;
		//bzero(buff,100);
		memset(buff,0,sizeof(buff));
		len = read(nmea_fd,buff,100);//从GPS模块读取数据到缓冲区buff中,每次读取100字节,nmea_fd:设备对应的文件描述符
		//printf("len=%d\n",len);
		//printf("buf:%s\n",buff);
		#ifdef DATA_PACKET_TEST
		Data_Packet_Test(buff,0);
		#endif
		strcat(GpsBuf,buff);//把每次读取的内容追加到GpsBuf中
		if((len>0)&&(len<=100))
		{
			buff[len]='\0';
			if((HeaderInfo = strchr(GpsBuf,'$')) != NULL)//找出头标志'$',并返回'$'后的字符串----------------------     步骤1
			{
				bzero(GpsInfo,240);
				strcpy(GpsInfo,HeaderInfo);		//把'$'后的字符串复制到临时缓冲区GpsInfo中-------------------------------     步骤2
			}
			else
			{
				bzero(GpsInfo,240);
				strcat(GpsInfo,GpsBuf);//如果找不到'$',则追加到GpsInfo中				-------------------------------     步骤2‘
				//if(strlen(GpsInfo) > 130)
				//	bzero(GpsInfo,150);
			}
			printf("ginfo:%s\n",GpsInfo);
			while((EndInfo=strchr(GpsInfo,'\n'))!=NULL)//找出结尾字符串'\n',并返回该位置地址--------------------------     步骤3
			{
				printf("GpsInfo:%s\n",GpsInfo);
				
				//GpsInfo=EndInfo;
				done=Handle_GpsData(GpsInfo,Handgps); //调用GPS数据处理程序
				printf("done:%d\n",done);
				if(done == 1)
				{
				}
				else if(done != -1)
				{
				}
				//else continue;
				/**取出没有处理的部分字符串,追加到GpsInfo中*/
				bzero(tmpbuf,240);
				EndInfo++;
				strcpy(tmpbuf,EndInfo);
				bzero(GpsInfo,sizeof(GpsInfo));          
				strcpy(GpsInfo,tmpbuf);												//----------------------     步骤4
			}
			memset(GpsBuf,0,sizeof(GpsBuf));
			strcat(GpsBuf,GpsInfo);													//----------------------     步骤5
			printf("GpsBuf:%s\n",GpsBuf);
		}
	}
	#undef DATA_PACKET_TEST
	recgps = FALSE;
}
/*******************************************************************************************
//名称 :GPSDevice_On
//功能: GPS模块电源开启
//输入参数: 无
//输出参数: 无
//返回: 无
******************************************************************************************/
void GPSDevice_On()
{
	//int Power_Status;
//	Power_Status=redio_bit(GPS_POWER);  // 读取 I/O口的电平状态,GPS_POWER:GPS模块电源控制I/O口
//	if(Power_Status!=1)			//如果为低,则对其置位,拉高使能,打开GPS设备电源		
		setio_bit(GPS_POWER);	//置位GPS_POWER
		return;
}
/*******************************************************************************************
//名称 :GPSDevice_Off
//功能: GPS模块电源关闭
//输入参数: 无
//输出参数: 无
//返回: 无
******************************************************************************************/
void GPSDevice_Off()
{
	int Power_Status;
	Power_Status=redio_bit(GPS_POWER);// 读取 I/O口的电平状态,GPS_POWER:GPS模块电源控制I/O口
	if(Power_Status!=0)		//如果为高,则对其清零,关闭GPS设备电源	
		clrio_bit(GPS_POWER);  //置零GPS_POWER
	return;
}
/*******************************************************************************************
//名称 :ThreadGps
//功能: GPS数据处理模块线程
//输入参数: 无
//输出参数: 无
//返回: 无
******************************************************************************************/
void ThreadGps()
{
	init_ioport();  //I/O口初始化
	GPSDevice_On();  //GPS模块电源开启
	nmea_fd=Open_UartPort(ttyS0_Gps); //打开设备"/dev/ttys3"
	if(nmea_fd<0)
		exit(1);
	while(1)
	{
		printf("Gps Data that deal with.... \n");
		GetGpsData(Handle_Gps_RMC);    //读取GPS数据,并调用数据处理函数
	}
	close(nmea_fd);      //关闭设备
	GPSDevice_Off();		//关闭GPS模块电源
}
/*******************************************************************************************
//名称 :main
//功能: 测试接口函数
//输入参数: 无
//输出参数: 函数执行状态
//返回: 成功:返回0
******************************************************************************************/
int GPSDATA_MAIN()
{
		ThreadGps();
		return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -