Home>
I want to achieve

Currently, I am writing a program that converts NMEA format received from a GPS receiver from a character string to a numerical value using c ++.
I would like to achieve this in a neat way using header files and classes.

Current problem

Currently, when data is received character by character from the receiver and the $mark is received, the next 5 characters are read to classify GPVTG or GPRMC, and then the number of times a comma is received is counted to obtain latitude and longitude information. I am.
However, I feel that this makes the process too complicated, but is this the only way?

  • Answer # 1

    I feel that this makes the process too complicated, but is this the only way?

    No, isn't it so complicated?

    Sample that uses $, * as the delimiter and separates the delimiter to the left and right ↓

    #include<string>
    #include<iostream>
    #include<iomanip>
    int main () {
        std :: string input = "$GPRMC, 085120.307, A, 3541.1493, N, 13945.3994, E, 000.0,240.3,181211, ,, A * 6A";
        std :: string :: size_type pos;
        while ((pos = input.find_first_of ("$, *"))! = std :: string :: npos) {
          char delim = input [pos];
          std :: string token = input.substr (0, pos);
          input = input.substr (pos + 1);
          std :: cout<<std :: setw (16)<<("[" + token + "]")<<delim<<"["<<input<<"] \ n";
        }
    }


    Execution result:

               [] $[GPRMC,085120.307,A,3541.1493,N,13945.3994,E,000.0,240.3,181211,,, A * 6A]
            [GPRMC], [085120.307, A, 3541.1493, N, 13945.3994, E, 000.0,240.3,181211, ,, A * 6A]
       [085120.307], [A,3541.1493,N,13945.3994,E,000.0,240.3,181211,,, A * 6A]
                [A], [3541.1493,N,13945.3994,E,000.0,240.3,181211,,, A * 6A]
        [3541.1493], [N,13945.3994,E,000.0,240.3,181211,,, A * 6A]
                [N], [13945.3994,E,000.0,240.3,181211,,, A * 6A]
       [13945.3994], [E, 000.0,240.3,181211, ,, A * 6A]
                [E], [000.0,240.3,181211, ,, A * 6A]
            [000.0], [240.3,181211, ,, A * 6A]
            [240.3], [181211, ,, A * 6A]
           [181211], [,, A * 6A]
                 [], [, A * 6A]
                 [], [A * 6A]
                [A] * [6A]

  • Answer # 2

    I feel that this makes the process too complicated.

    It is imagination.

    Currently, when data is received character by character from the receiver and the $mark is received, the next 5 characters are read to classify GPVTG or GPRMC, and then the number of times a comma is received is counted to obtain latitude and longitude information. I am.

    Is this the only way?

    It's not essentially different, but

    Instead of processing each time a character is received, one line up to the line feed code is stored in the buffer, the checksum is checked for the data for that one line, and if it is valid, the information is cut out from the line data. I think that there are more cases where you do it.