使用C++处理GPS输出

Working with GPS Output using C++

本文关键字:输出 GPS 处理 C++ 使用      更新时间:2023-10-16

嗨,我正在处理GPS输出。更准确地说,我使用$GPRMC输出。现在我得到的输出形式如下:

$GPRMC,225446,A,4916.45 N,12311.12 W,000.5,054.7,191194,020.3 E,*68"

该输出包括时间、lats、longs、以节为单位的速度、关于航向、日期、磁变化和强制性校验和的信息。

我所附的图像显示了我从字符串中取出子字符串时得到的当前结果。

现在我得到hhmmss格式的时间。我想要hh:mm:ss格式。另外,我得到的经度是4916.45 N。我想得到49度16'45"。纬度为123度11'12"。我是一个初学者,所以我真的不知道如何转换格式。我还在下面附上了我的代码。

#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;
}

首先,你的NMEA句子是错误的,N和W之前应该有逗号",",所以你实际上必须解析"12311.12",而不是"12311.12W"。你可以在这个网站上查看:http://aprs.gids.nl/nmea/#rmc,您还应该始终检查句子的校验和-对于在线检查,请使用:http://www.hhhh.org/wiml/proj/nmeaxor.html.

为了解析经度和纬度,我建议使用regexp,我并不是说regexp是正确的——它只解析您提供的数据:

#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
}

http://coliru.stacked-crooked.com/a/ce6bdb1e551df8b5

您必须自己解析;在标准C++中没有GPS解析。

您可能需要编写自己的Angle类,以便将49 degrees 16' 45"作为可能的输出。为此,您需要重载operator<<