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

📄 nmeaparser.cpp

📁 对于GPS数据NMEA的解析
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	auxBuf[p2 - 2 - auxBuf] = '\0';
	latitude += atof(auxBuf);
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	if(p2 - p1 != 1)
		return;
	if(*p1 == 'S')
		latitude = -latitude;
	else if(*p1 != 'N')
		return;
	p1 = p2 + 1;

    // Longitude
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	if((p2 = strchr(auxBuf, '.')) == NULL)
		return;
	DOUBLE longitude = atof(p2 - 2) / 60.0;
	auxBuf[p2 - 2 - auxBuf] = '\0';
	longitude += atof(auxBuf);
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	if(p2 - p1 != 1)
		return;
	if(*p1 == 'W')
		longitude = -longitude;
	else if(*p1 != 'E')
		return;
	p1 = p2 + 1;

	// GPS quality
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	UINT quality = atoi(auxBuf);

	// Satellites in use
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	UINT satelitesInUse = atoi(auxBuf);
	
	// HDOP
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	DOUBLE hdop = atoi(auxBuf);
	
	// Altitude
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	DOUBLE altitude = atoi(auxBuf);
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	if(p2 - p1 != 1)
		return;
	if(*p1 != 'M')
		return;
	p1 = p2 + 1;

	// Height of geoid
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	DOUBLE heightGeoid = atoi(auxBuf);
	if((p2 = strchr(p1, ',')) == NULL)
		return;
//	if(p2 - p1 != 1)
//		return;
//	if(*p1 != 'M')
//		return;
	p1 = p2 + 1;

	// Empty field
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	p1 = p2 + 1;

	// Last Empty field
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) != NULL)
		return;
	if((p2 = strchr(p1, '*')) == NULL)
		return;

	// Set the values of m_GPSInfo
//	m_GPSInfo.m_latitude = latitude;
//	m_GPSInfo.m_longitude = longitude;
//	m_GPSInfo.m_altitude = altitude;
//	m_GPSInfo.m_nSentences++;
	m_GPSInfo.m_signalQuality = quality;
	m_GPSInfo.m_satelitesInUse = satelitesInUse;
}

void NMEAParser::ProcessGPGSA(const CHAR *buf, const UINT bufSize)
{

}

void NMEAParser::ProcessGPGSV(const CHAR *buf, const UINT bufSize)
{

}

void NMEAParser::ProcessGPRMB(const CHAR *buf, const UINT bufSize)
{

}

/*
Format

  $GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A
	 |	 |		| |			 |			 |	   |	 |		|	   |
	 |	 |		| |			 |			 |	   |	 |		|	   *6A Checksum data
	 |	 |		| |			 |			 |	   |	 |		|
	 |	 |		| |			 |			 |	   |	 |		003.1,W Magnetic Variation
	 |	 |		| |			 |			 |	   |	 |
	 |	 |		| |			 |			 |	   |	 230394 Date - 23rd of March 1994
	 |	 |		| |			 |			 |	   |
	 |	 |		| |			 |			 |	   084.4 Track angle in degrees
	 |	 |		| |			 |			 |	   
	 |	 |		| |			 |			 022.4 Speed over the ground in knots
	 |	 |		| |			 |
	 |	 |		| |			 01131.000,E Longitude 11 deg 31.000' E
	 |	 |		| |
	 |	 |		| 4807.038,N Latitude 48 deg 07.038' N
	 |	 |		|
	 |	 |		A Status A=active or V=Void
	 |	 |
	 |	 123519 Fix taken at 12:35:19 UTC
	 |
	 RMC Recommended Minimum sentence C

*/
void NMEAParser::ProcessGPRMC(const CHAR *buf, const UINT bufSize)
{
	CHAR auxBuf[15];
	const CHAR *p1 = buf, *p2;

	// GGA
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if(bufSize < 6)
		return;
	strncpy(auxBuf, buf, 5);
	auxBuf[5] = '\0';
	if(strcmp(auxBuf, "GPRMC") != 0 || buf[5] != ',')
		return;
	p1 += 6;

	// Time
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	UINT hour, min, sec;
	strncpy(auxBuf, p1, 2);
	auxBuf[2] = '\0';
	hour = atoi(auxBuf);
	p1 += 2;
	strncpy(auxBuf, p1, 2);
	auxBuf[2] = '\0';
	min = atoi(auxBuf);
	p1 += 2;
	strncpy(auxBuf, p1, 2);
	auxBuf[2] = '\0';
	sec = atoi(auxBuf);
	p1 = p2 + 1;

	
	// Status 
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	if(p2 == p1)
		return;
//	if(*p1 != 'A')
//		return;
	p1 = p2 + 1;

    // Latitude
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	if((p2 = strchr(auxBuf, '.')) == NULL)
		return;
	if(p2-auxBuf < 2)
		return;
	DOUBLE latitude = atof(p2 - 2) / 60.0;
	auxBuf[p2 - 2 - auxBuf] = '\0';
	latitude += atof(auxBuf);
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	if(p2 - p1 != 1)
		return;
	if(*p1 == 'S')
		latitude = -latitude;
	else if(*p1 != 'N')
		return;
	p1 = p2 + 1;

    // Longitude
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	if((p2 = strchr(auxBuf, '.')) == NULL)
		return;
	DOUBLE longitude = atof(p2 - 2) / 60.0;
	auxBuf[p2 - 2 - auxBuf] = '\0';
	longitude += atof(auxBuf);
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	if(p2 - p1 != 1)
		return;
	if(*p1 == 'W')
		longitude = -longitude;
	else if(*p1 != 'E')
		return;
	p1 = p2 + 1;

	// Ground speed
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	DOUBLE groundSpeed = atof(auxBuf);

	// Course over ground (degrees) 
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	DOUBLE courseOverGround = atof(auxBuf);

	// Date
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	UINT day, month, year;
	strncpy(auxBuf, p1, 2);
	auxBuf[2] = '\0';
	day = atoi(auxBuf);
	p1 += 2;
	strncpy(auxBuf, p1, 2);
	auxBuf[2] = '\0';
	month = atoi(auxBuf);
	p1 += 2;
	strncpy(auxBuf, p1, 2);
	auxBuf[2] = '\0';
	year = 2000 + atoi(auxBuf);
	p1 = p2 + 1;

	// Magnetic variation
	if((UINT)(p1 - buf) >= bufSize)
		return;
	if((p2 = strchr(p1, ',')) == NULL)
		return;
	strncpy(auxBuf, p1, p2 - p1);
	auxBuf[p2 - p1] = '\0';
	p1 = p2 + 1;
	DOUBLE magneticVariation = atof(auxBuf);
	if((p2 = strchr(p1, '*')) == NULL)
		return;
	if(p2 - p1 > 1)
		return;
	if(*p1 == 'W')
		latitude = -latitude;
	else if(*p1 != 'E' && *p1 != '*')
		return;

	// Set the values of m_GPSInfo
	m_GPSInfo.m_latitude = latitude;
	m_GPSInfo.m_longitude = longitude;
	m_GPSInfo.m_nSentences++;
}

void NMEAParser::ProcessGPZDA(const CHAR *buf, const UINT bufSize)
{

}

⌨️ 快捷键说明

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