您好我正在使用GPS输出。为了更准确,我使用$ GPRMC输出。我得到的输出是以下形式:
$GPRMC,225446,A,4916.45 N,12311.12 W,000.5,054.7,191194,020.3 E,*68"
此输出包括时间,拉特,长度,结节速度,课程信息,日期,磁变化和强制校验和。
现在我以hhmmss格式获得时间。我希望它是......:mm:ss格式。 另外,我的经度为4916.45 N.我希望得到它为49度16' 45&#34 ;. 而纬度为123度11' 12&#34 ;.我是初学者,所以我真的不知道如何转换格式。我还附上了我的代码。
#include<iostream>
#include<string>
#include<sstream>
#include<stdio.h>
#include<conio.h>
using namespace std;
int main()
{
std::string input = "$GPRMC,225446,A,4916.45 N,12311.12 W,000.5,054.7,191194,020.3 E,*68";
std::istringstream ss(input);
std::string token;
string a[10];
int n = 0;
while (std::getline(ss, token, ','))
{
//std::cout << token << '\n';
a[n] = token;
n++;
}
cout << a[0] << endl << endl;
cout << "Time=" << a[1] << endl << endl;
cout << "Navigation receiver status:" << a[2] << endl << endl;
cout << "Latitude=" << a[3] << endl << endl;
cout << "Longitude=" << a[4] << endl << endl;
cout << "Speed over ground knots:" << a[5] << endl << endl;
cout << "Course made good,True:" << a[6] << endl << endl;
cout << "Date of Fix:" << a[7] << endl << endl;
cout << "Magnetic variation:" << a[8] << endl << endl;
cout << "Mandatory Checksum:" << a[9] << endl << endl;
_getch();
return 0;
}
答案 0 :(得分:2)
首先,你的NMEA句子是错误的,应该有逗号&#39;,&#39;在N和W之前,所以你实际上必须解析&#34; 12311.12&#34;而不是&#34; 12311.12 W&#34;。你可以在这个网站上查看它:http://aprs.gids.nl/nmea/#rmc,你也应该经常检查句子的校验和 - 在线检查使用:http://www.hhhh.org/wiml/proj/nmeaxor.html。
要解析经度和纬度,我建议使用正则表达式,我不是说这是正则表达式是正确的 - 它只解析你提供的数据:
#include <iostream>
#include <string>
#include <regex>
#include <iostream>
std::tuple<int,int,int> parseLonLat(const std::string& s) {
std::regex pattern("(\\d{2,3})(\\d+{2})\\.(\\d+{2})" );
// Matching single string
std::smatch sm;
if (std::regex_match(s, sm, pattern)) {
return std::make_tuple(std::stoi(sm[1]), std::stoi(sm[2]), std::stoi(sm[3]));
}
return std::make_tuple(-1,-1,-1);
}
int main (int argc, char** argv) {
auto loc1 = parseLonLat("4916.45");
std::cout << std::get<0>(loc1) << ", " << std::get<1>(loc1) << ", " << std::get<2>(loc1) << "\n";
// output: 49, 16, 45
auto loc2 = parseLonLat("12311.12");
std::cout << std::get<0>(loc2) << ", " << std::get<1>(loc2) << ", " << std::get<2>(loc2) << "\n";
// output: 123, 11, 12
}
答案 1 :(得分:0)
你必须自己解析;标准C ++中没有GPS解析。
您可能希望编写自己的Angle
课程,以便49 degrees 16' 45"
作为可能的输出。您需要为此重载operator<<
。