D
D
Denis Afonin2014-12-09 23:13:35
Java
Denis Afonin, 2014-12-09 23:13:35

How to get correct NMEA data from external GPS?

In an Android program, I am writing a program that works with GPS. Since not all devices have built-in GPS, I decided to supplement the program with the ability to connect to external GPS receivers via Bluetooth and receive data from them in NMEA format. I coped with the first part of the task, I connect to an external receiver and receive data from it. But the data comes in chunks that need to be split into valid NMEA data lines. And this is the second part of the task, which caused me difficulty, I don’t even know where to start.
As I already wrote, the data comes in pieces of different lengths and always different. And from this heap you need to concoct the correct lines that start with the "$" character and end with "\n" . The length of an NMEA string cannot exceed 80 characters.
Help me compose the logic of such a parser, I have problems with this :((
Example of incoming chunks:
1 chunk:
N,05049.5692,E,0.06,232.87,091214,,*02\n
$GPGGA,200703.000,6140.6754,N,05049.5695,E ,1,07,1.1,131.6,M,6.7,M,,0000*6D\n
$GPGSA,A,3,15,13,26,28,17,18,04,,,,,,1.9,1.1 ,1.5*3D\n
$GPRMC,200703.000,A,6140.6754,N,05049.5695,E,0.05,199.27,091214,,*0F\n
$GPGGA,200704.000,6140.6755,N,05049.5698,E,1,07,1.1 ,131.7,M,6.7,M,,0000*67\n
$GPGS
2 chunk:
A,A,3,15,13,26,28,17,18,04,,,,,,1.9,1.1,1.5 *3D\n
$GPRMC,200704.000,A,6140.6755,N,05049.5698,E,0.02,81.10,091214,,*3F\n
$GPGGA,200705.000,6140.6755,N,05049.5701,E,1,07,1.1,131.8 ,M,6.7,M,,0000*68\n
$GPGSA,A,3,15,13,26,28,17,18,04,,,,,,1.9,1.1,1.5*3D\n
$GPRMC,200705.000,A,6140.6755,N,05049.5701,E, 0.02,209.70,091214,,*0B\n
3rd piece:
$GP
4th piece:
GGA,200706.000,6140.6756,N,05049.570
5th piece:
4,E,1,07,1.1,131.9,M,6.7,M
6th piece :
,,0000*6C
7 chunk:
,18.04,,,,,,1.9,1.1,1.5*3D\n$
8 chunk:
GPRMC,200706.000,A,6140.6756,N,05049.5704,E,0.02,181.59, 091214,,*06\n
Etc.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Power, 2014-12-09
@Power

What's the problem? Glue everything into one stream and read it line by line. If the line starts with $ - use it.

R
Rsa97, 2014-12-10
@Rsa97

I do not know Java, I will describe the general principle in C.

char nmea[81];
char buf[4096];
int nmeaIdx, bufIdx, bufLen;
    nmeaIdx = 0;
...
    while(1) {
        bufLen = read(buf, 4096); // Читаем пакет в буфер, получаем количество считанных байт.
        bufIdx = 0;
        while (bufIdx < bufLen) {
            nmea[nmeaIdx] = buf[bufIdx];
            if (buf[bufIdx] == '\n') {
                nmea[nmeaIdx] = 0;
                if (nmea[0] == '$') {
                    // здесь обрабатываем строку nmea
                }
                nmeaIdx = 0;
            } else
                nmeaIdx++;
            bufIdx++;
        }
    }

Such a cycle can be moved to a separate thread and the found lines can be placed in the processing queue of the main thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question