2016-04-14 25 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

非常感謝。我會試一試。 –