2016-04-14 75 views
1

嗨我正在使用GPS输出。为了更准确,我正在使用$ GPRMC输出。现在,我得到的输出为以下形式:使用C++输出GPS输出

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

此输出构成的时间,拉特,长材,在海里的速度,信息关于课程,日期,磁场变化和强制性检验。

The image that I have attached shows the current result I'm getting as I'm taking out the sub strings from the string.

现在我得到的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; 
} 
+1

请重新考虑你的东西往往被认为使用不好的做法:['using namespace std;'](http://stackoverflow.com/q/1452721/1171191)和['endl'](http://chris-sharpe.blogspot.co.uk/2016/02 /why-you-shouldnt-use-stdendl.html)(这些是解释的链接)。 – BoBTFish

回答

0

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

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

+0

有一个用于解析NMEA数据的Arduino的小型GPS库;你可以查看这个例子。 –

+0

@AhmetIpkin:看起来像作业;我不认为使用Arduino库会有所帮助。 – MSalters

+0

当然,他不能逐字使用图书馆;但可以用它作为解析GPS数据的方法,我认为。 NMEA格式毕竟是标准格式 –

1

首先,你的NMEA句子是错误的,在N和W之前应该有逗号',',所以你实际上必须解析“12311.12”而不是“12311.12 W”。你可以在这个网站上查到:http://aprs.gids.nl/nmea/#rmc,你也应该经常检查句子的校验和 - 对于网上支票使用:http://www.hhhh.org/wiml/proj/nmeaxor.html

要解析的经度和纬度,我建议正则表达式,我notsaying这是正则表达式是正确的 - 它只能通过解析您所提供的数据:

#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

+0

非常感谢。我会试一试。 –