2

Hi I'm working with GPS output. To be more accurate I'm working using the $GPRMC output. Now the output that I get is in the following form:

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

This output constitutes of time, lats, longs, speed in knots, info about course, date, magnetic variation and mandatory checksum.

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

Now I'm getting the time in the hhmmss format. I want it in hh:mm:ss format. Plus I'm getting the longitude as 4916.45 N. I want to get it as 49 degrees 16' 45". And the latitude as 123 degrees 11' 12". I'm a beginner so I really don't know how to convert the format. I have also attached my code below.

#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;
}
marcinj
  • 48,511
  • 9
  • 79
  • 100
Nauman Nawaz
  • 21
  • 1
  • 4
  • 1
    Please reconsider your use of what are often considered bad practices: [`using namespace std;`](http://stackoverflow.com/q/1452721/1171191) and [`endl`](http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html) (those are links to explanations). – BoBTFish Apr 14 '16 at 07:30

2 Answers2

3

First thing is that your NMEA sentence is wrong, there should be commas ',' before N and W, so you will actually have to parse "12311.12" and not "12311.12 W". You can check it on this site: http://aprs.gids.nl/nmea/#rmc, you should also always check checksum of sentence - for online checks use: http://www.hhhh.org/wiml/proj/nmeaxor.html.

To parse longitude and latitude I suggest regexps, I am notsaying this is regexp is correct - it only parses data you have provided:

#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

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

You'll have to parse that yourself; there's no GPS parsing in standard C++.

You may want to write your own Angle class in order to have 49 degrees 16' 45" as possible output. You'll want to overload operator<< for that.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • There was a tinyGPS library for Arduino that does parsing of NMEA data; you can examine that for examples. – Ahmet Ipkin Apr 14 '16 at 07:21
  • @AhmetIpkin: It looks like homework; I don't think using an Arduino library will help. – MSalters Apr 14 '16 at 07:22
  • Of course, he cannot use the library verbatim; but can use it as a how-to for parsing GPS data I think. NMEA format is a standard format after all – Ahmet Ipkin Apr 14 '16 at 07:25