2016-04-14 8 views
1

こんにちはGPS出力を使用しています。私は$ GPRMCの出力を使用してより正確に私は働いている。今、私が手出力は次のような形式である:C++を使用したGPS出力の操作

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

この出力は、時間、ラッツ、long型、ノットの高速、もちろんに関する情報、日付、磁気変動と必須のチェックサムを構成しています。今、私はHHMMSS形式で時刻を取得してい

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

。私は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

ことが多いと考えられるものの使用を再考してください: -

は経度を解析し、私は正規表現を示唆して緯度するには、私は、これは正規表現が正しいですnotsayingています、それはあなたが提供したデータを解析し、悪い習慣:['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解析がありません。

可能であれば49 degrees 16' 45"を出力するには、Angleクラスを作成することをおすすめします。そのためにはoperator<<をオーバーロードする必要があります。

+0

NMEAデータの解析を行うArduino用のtinyGPSライブラリがありました。その例を調べることができます。 –

+0

@AhmetIpkin:宿題のようです。私はArduinoライブラリを使用すると助けになるとは思わない。 – MSalters

+0

もちろん、彼はライブラリをそのまま使用することはできません。私は思うのGPSデータを解析するための方法としてそれを使用することができます。 NMEA形式は標準フォーマットのあとであります。 –

1

最初にNMEA文が間違っていて、NとWの前にカンマ '、'があるので、実際に "12311.12 W"ではなく "12311.12"を解析する必要があります。あなたはこのサイトでそれを確認することができます: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 
} 

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

+0

ありがとうございます。私はそれを試してみましょう。 –

関連する問題