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

📄 cgpsdataparse.cpp

📁 该程序主要功能是获取gps数据,并且提供解析功能,运行ppc2003下,开发导航系统,或者定位系统的同仁们,可以参考一下.
💻 CPP
字号:
/* !@file

模块名:gps模块

文件名:CGpsDataParse.cpp

相关文件:CGpsDataParse.h

文件功能:gps数据解析

作者:万平方

版本: 1.0

日期: 2006年5月23日

*/


#include "StdAfx.h"
#include "CGpsDataParse.h"
//#include "CDeviceData.h"
//#include "CGpsData.h"
#include <vector>
#include <string>
using namespace std;
//解析句子





	class	CTokenizer
	{
	public :

		CTokenizer( char cSplit = ',')

		{

			m_strSplit	=	cSplit;
		}

		CTokenizer( char* cSplit )

		{

			m_strSplit	=	cSplit;
		}

		vector<string>& Tokenize( const char* szSentence )
		{
			m_Values.clear();

			string	strSentence		=	szSentence;

			const char*	seps	=	m_strSplit.c_str();

			const char*		p	=	strSentence.c_str(); 

			const char*		q	=	strpbrk( p, seps );

			while ( q )
			{
				string	str;

				str.assign( p, q-p );

				m_Values.push_back( str );

				int		nSize	=	m_Values.size();

				// move to next
				p	=	q+1;

				q	=	strpbrk( p, seps );
			}

			// 加入尾部
			if ( p	<=	strSentence.c_str() + strSentence.size() )
				m_Values.push_back( string(p) );

			return	m_Values;
		}

	protected :

		vector<string>		m_Values;

		string				m_strSplit;
	};

	CGpsDataParse::CGpsDataParse()
	{
	}

	CGpsDataParse::~CGpsDataParse()
	{
	}

	int CGpsDataParse::ChackCommand(const CDeviceData& dataBuf)
	{
		return 0;
	}

	/*
	函数名:Parse

	输入参数:DevicedataBuf

	输出参数:无

	函数功能:将接收到的数据解析成子句,并判断数据包头用哪种解析方式*/


	bool CGpsDataParse::Parse(const BYTE* DevicedataBuf)
	{


		CTokenizer parseSentences('$');

		vector<string>& vSentences = parseSentences.Tokenize((const char*)((DevicedataBuf)));

		if(vSentences.size() <= 0)

			return false;



		bool bParseSentence = false;

		for(vector<string>::iterator it = vSentences.begin();it != vSentences.end();it++)

		{
			CTokenizer parseSentences(',');

			//get substring;

			string strSentence = (*it).c_str();

			vector<string>& vValues = parseSentences.Tokenize((*it).c_str());

			int nSize = vValues.size();

			if(vValues.size() <= 0)

				continue;

			//parse different type sentence

			string strSentenceTitle = vValues[0];

			if(strSentenceTitle == "GPRMC")

			{


				if(ParseRMC(vValues))

					bParseSentence	=	true;

			}

			else if (strSentenceTitle == "GPGGA")

			{


				if(  ParseGGA(vValues))

					bParseSentence	=	true;
			}

			if(strSentenceTitle == "GPGSA")
			{

				if( ParseGSA(vValues))

					bParseSentence  = true;
			}

		}

		return bParseSentence;

	}

	/*
	函数名:ParseGGA

	输入参数:_values

	输出参数:GpsData

	函数功能:将子串用GGA方式解析*/

	/*
	namespace Location
	{

	CGpsDataParse::CGpsDataParse()
	{
	}

	CGpsDataParse::~CGpsDataParse()
	{
	}

	int CGpsDataParse::ChackCommand(const CDeviceData& dataBuf)
	{
	}

	void CGpsDataParse::Parse(const CDeviceData& DevicedataBuf)
	{
	}

	void CGpsDataParse::ParseGGA(const vector<string>& _values, CGpsData& GpsData)
	{
	}

	void CGpsDataParse::ParseGSA(const vector<string>& _values, CGpsData& GpsBuf)
	{
	}

	void CGpsDataParse::ParseRMC(const vector<string>& _values, CGpsData& GpsData)
	{
	}

	void CGpsDataParse::ProcessSentence(vector<string>& _values)
	{
	}

	}
	*/
	bool CGpsDataParse::ParseGGA(const vector<string>& _values)

	{


		if(_values.size() <= 0)

			return false;

		//0: flag GGA.

		//1 :time.

		int  hour, minute, second;

		if(ParseTime(_values[1].c_str(), hour, minute, second))
		{
		//	GpsData.SetHour(hour);

		//	GpsData.SetMinute(minute);

		//	GpsData.SetSecond(second);

		}
		//2:Latitude.

		double dLat;

		if(ParseDegrees(_values[2].c_str(), dLat))

		{
		//	GpsData.SetLat(_values[3] == "N"? dLat: -dLat);

		}

		//3:Longitude.

		double dLon;

		if(ParseDegrees(_values[4].c_str(),dLon))
		{

		//	GpsData.SetLon( _values[5] == "E" ? dLon: -dLon);

		}

		//4: Quality of GPS fix. (1=(定位sps模式) 0=(未定位))


	//	GpsData.SetPattern((_values[6] == "1"? '1': '0'));


		//5:Skip number of sats tracked for now.
		char *tmpStr = new char[];
		int tmp_num;

		strcpy(tmpStr,_values[7].c_str());

		tmp_num = atoi(tmpStr);

		//GpsData.SetSatNum(tmp_num);

        delete tmpStr;
        tmpStr = NULL;


		//6:Horizontal dilution of precision(HDOP).
		char* tmpHdop = new char[];

		strcpy(tmpHdop,_values[8].c_str());

		float tmp_fhdop = float(atof(tmpHdop));

	//	GpsData.SetFhdop(tmp_fhdop);

        delete tmpHdop;

        tmpHdop = NULL;


		//7:Altitude.
		char* tmpAltitude = new char[];
		strcpy(tmpAltitude,_values[9].c_str());

		float tmp_altitude = float(atof(tmpAltitude));

	//	GpsData.SetAltitude(tmp_altitude);
        
        delete tmpAltitude;

        tmpAltitude = NULL;
   
		return true;


	}

	bool CGpsDataParse::ParseGSA(const vector<string>& _values)
	{
		char *tmp_SeriNum = new char[];
		char *tmp_PatternOne = new char[];

		//1 Pattern2: A = 自动, M = 手动

	//	GpsData.SetPatternTwo(_values[1] == "A"? 'A': 'M');

		//2 Pattern1 1 = 未定位  2 = 二维定位 3 = 三维定位
		int  tmpPatternOne;

		strcpy(tmp_PatternOne,_values[2].c_str());

		tmpPatternOne = atoi(tmp_PatternOne);

	//	GpsData.SetPatternOne(tmpPatternOne);


		//3 SatSerialNum  卫星编号

		int tmpSeriNum ;

		strcpy(tmp_SeriNum,_values[3].c_str());

		tmpSeriNum = atoi(tmp_SeriNum);

	//	GpsData.SetSatNum(tmpSeriNum);


		//4 PDOP

		//5 HDOP

		//6 VDOP

		return true;

	}

	/*
	函数名:ParseRMC

	输入参数:_values

	输出参数:GpsData

	函数功能:将子串用RMC方式解析*/



	bool CGpsDataParse::ParseRMC(const vector<string>& _values)
	{
		if (_values.size() <= 0)

			return false;

		int hour, minute, second;

		if(ParseTime(_values[1].c_str(), hour, minute, second))

		{
		//	GpsData.SetHour(hour);
//
	//		GpsData.SetMinute(minute);

		//	GpsData.SetSecond(second);
		}
		//2:state  定位状态 A=可用 V=警告(不可用)

	//	GpsData.SetState(_values[2] == "A"? 'A': 'V');

		//3:Latitude
		double dLat;

		if(ParseDegrees(_values[3].c_str(), dLat))
		{
			//将纬度传出
			if(_values[4] == "N")
			{
				weidu = dLat;
			}
			
		     else
			 {
				 weidu = -dLat;
			 }
		}

		//	GpsData.SetLat(_values[4] == "N"? dLat: -dLat);

		//4:Longitude
		double dLon;

		if(ParseDegrees(_values[5].c_str(),dLon))
		{
		  //将经度传出
			if( _values[6] == "E")
			{
			jingdu =dLon;

			}
			else
			{
			jingdu =-dLon;
			}
		}

		//	GpsData.SetLon( _values[6] == "E" ? dLon: -dLon);

		//5:Current speed

		double dSpeed = atof(_values[7].c_str());

	//	GpsData.SetSpeed((float)dSpeed * 1.069);

		// 6:Current track

		//7: Current date

		int year,month,day;

		if(ParseDate( _values[9].c_str(), year, month, day))

		{ // return false;

		//	GpsData.SetYear(year);

		//	GpsData.SetMonth(month);

		//	GpsData.SetDay(day);
//
		}





		return true;
	}



	void CGpsDataParse::ProcessSentence(vector<string>& _values)
	{
	}

	/*
	函数名称:ParseTime

	输入参数:timeString

	输出参数:hour ,minute,second

	函数功能:将时间串转化成具体的时间格式*/

	bool CGpsDataParse::ParseTime( const char* timeString, int& hour, int& minute, int& second )
	{
		if (strlen (timeString) != 0) 
		{
			long tempTime = atol(timeString);

			int tempHour, tempMinute, tempSecond;

			tempHour   = tempTime / 10000;

			tempMinute = (tempTime - ((tempTime / 10000) * 10000)) / 100;

			tempSecond = tempTime - ((tempTime / 100) * 100);
			// Check to see if the time is valid.
			if (   (tempHour   >= 0 && tempHour   <= 23)
				&& (tempMinute >= 0 && tempMinute <= 59)
				&& (tempSecond >= 0 && tempSecond <= 61)) 
			{ // leap seconds
				hour   = tempHour;

				minute = tempMinute;

				second = tempSecond;

				return true;
			} 

		} 

		return false;
	}

	/*
	函数名:Parse Degrees

	输入参数:degString

	输出参数:degrees

	函数功能:将经纬度子串转换成具体的经纬度
	*/

	bool CGpsDataParse::ParseDegrees (const char* degString, double& degrees ) 
	{
		if (strlen (degString) != 0) 
		{
			double tempMinutes;

			double tempDegrees;

			double tempPosition;

			tempPosition = atof (degString);

			tempDegrees  = (double)((int)(tempPosition / 100.0));

			tempMinutes  = (tempPosition - (tempDegrees * 100.0));

			tempPosition = tempDegrees + (tempMinutes / 60.0);

			if (tempPosition >= 0.0 || tempPosition <= 180.0) 

			{
				degrees = tempPosition;

				return true;
			} 

		} 

		return false;
	}

	/*
	函数名: Parse Date

	输入参数:dateString

	输出参数:year ,month ,day

	函数功能:将时间子串转换成具体的时间*/


	bool CGpsDataParse::ParseDate (const char* dateString, int& year, int& month, int& day )
	{
		if (strlen (dateString) >= 6 )//DATE_LEN) 

		{
			long tempDate = atol (dateString);

			int tempYear, tempMonth, tempDay;

			tempYear  = tempDate - ((tempDate / 100) * 100);

			tempMonth = (tempDate - ((tempDate / 10000) * 10000)) / 100;

			tempDay   = tempDate / 10000;
			// Check to see if the date is valid.  (This function will accept
			// Feb 31 as a valid date; no check is made for how many days are in
			// each month of our whacked calendar.)

			if (   (tempYear  >= 0 && tempYear  <= 99)
				&& (tempMonth >= 1 && tempMonth <= 12)
				&& (tempDay   >= 1 && tempDay   <= 31)) 

			{

				year = tempYear;

				month = tempMonth;

				day = tempDay;

				return true;
			} 

		} 

		return	false;
	}


⌨️ 快捷键说明

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