📄 gps.cpp
字号:
#include "StdAfx.h"
#include "GPS.h"
const string GPS::m_sPrefix = "GPGGA";
GPS::GPS(void)
{
m_sInputBuffer.reserve(GPS_STRINIG_BUFFER_SIZE);
m_bNewLine = false;
m_bValid = false;
m_nFlag = -1;
}
GPS::~GPS(void)
{
}
void GPS::OnReceiveChar(const char ch)
{
switch (ch)
{
case '$':
m_bNewLine = true;
m_nFlag = 0;
break;
case '*':
if (m_bValid)
{
AnalyseGPGGA(m_sInputBuffer);
m_bValid = false;
}
m_sInputBuffer = "";
break;
case 13: //cr
break;
case 10: //lf
break;
default:
if (m_bNewLine)
{
if (m_bValid)
{
m_sInputBuffer += ch;
}
else
{
if (m_nFlag != -1)
{
m_sInputBuffer += ch;
++m_nFlag;
if (m_nFlag == m_sPrefix.size())
{
if (m_sPrefix == m_sInputBuffer)
{
m_bValid = true;
}
else
{
m_nFlag = -1;
}
}
}
}
}
break;
}
}
void GPS::AnalyseGPGGA(const string& str)
{
assert(!str.empty());
GPGGADATA data = {0};
const char comma = ',';
size_t start = 0, end = 0;
//$GPGGA,(1)Time (UTC),(2)Latitude,(3)N or S,(4)Longitude,(5)E or W,(6)GPS Quality Indicator,
//(7)Number of satellites in view,0-12,(8)Horizontal Dilution of precision,(9)Altitude,M,
//(10),M,(11),(12)*hh(CR)(LF)
//解析的帧格式
//GPGGA
end = str.find_first_of(comma, start);
start = end + 1;
//Time int nHours int nMinutes int nSeconds
end = str.find_first_of(comma, start);
long temp = atoi(str.substr(start, end - start).c_str());
data.nHours = (temp / 10000 + 8) % 24;
data.nMinutes = (temp / 100) % 100;
data.nSeconds = temp % 100;
start = end + 1;
//float fLatitude
end = str.find_first_of(comma, start);
data.fLatitude = (float)atof(str.substr(start, end - start).c_str());
start = end + 1;
//N or S
end = str.find_first_of(comma, start);
assert((end - start) == 1);
start = end + 1;
//float fLongitude
end = str.find_first_of(comma, start);
data.fLongitude = (float)atof(str.substr(start, end - start).c_str());
start = end + 1;
//E or W
end = str.find_first_of(comma, start);
assert((end - start) == 1);
start = end + 1;
//int nGPSQual;
end = str.find_first_of(comma, start);
assert((end - start) == 1);
data.nGPSQual = atoi(str.substr(start, end - start).c_str());
start = end + 1;
//int nNumofSate;
end = str.find_first_of(comma, start);
data.nNumofSate = atoi(str.substr(start, end - start).c_str());
start = end + 1;
// float fHdop
end = str.find_first_of(comma, start);
data.fHdop = (float)atof(str.substr(start, end - start).c_str());
start = end + 1;
//float fAltitude
end = str.find_first_of(comma, start);
data.fAltitude = (float)atof(str.substr(start, end - start).c_str());
start = end + 1;
//float fUndulation;
UpdateDataBuffer(data);
/*assert(!str.empty());
//14 ',' total
GPGGADATA data = {0};
const char comma = ',';
size_t start = 0, end = 0;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
end = str.find_first_of(comma, start);
cout << str.substr(start, end - start) << endl;
start = end + 1;
UpdateDataBuffer(data);*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -