D
D
Denis Afonin2014-12-07 13:58:23
Android
Denis Afonin, 2014-12-07 13:58:23

How to parse NMEA string?

In an Android application, I receive data from a satellite in NMEA format. I'm trying to parse these lines, but somehow it doesn't work out very well.
Line example: $GPGSA,A,3,04,11,19,32,01,,,,,,,,8.16,6.83,4.47*0B
I need to extract satellite numbers from this line, in this example it is: 04, 11, 19,32,01. I do parsing like this:

nmea = "$GPGSA,A,3,04,11,19,32,01,,,,,,,,8.16,6.83,4.47*0B";
String[] subs = new String[10];
StringTokenizer st = new StringTokenizer(nmea, ",");
int i=0;
while (st.hasMoreElements()){
       Pattern p = Pattern.compile("\\d{2}");
       Matcher m = p.matcher(st.nextElement().toString());
       if(m.matches()){
             subs[i] = st.nextElement().toString();
             Log.d(LOG_TAG, "Элемент "+i+": " + subs[i]);
             i++;
        }
}

As a result, the following is displayed in the log:
Element 0: 11
Element 0: 32
Element 0: 8.16
Help me write the correct expression.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2014-12-07
@Rsa97

Reading the description of nmea$GPGSA

nmea = "$GPGSA,A,3,04,11,19,32,01,,,,,,,,8.16,6.83,4.47*0B";
String[] parts = nmea.split('[,*]');
String[] PRNs = new String[12];
for (int i = 0; i < 12 && parts[i+3] != ''; i++) {
    PRNs[i] = parts[i+3];
    Log.d(LOG_TAG, "Элемент "+Integer.toString(i)+": "+PRNs[i];
}

D
Denis Afonin, 2014-12-07
@Afdenis

Thanks, that's how it works! But still so interesting, why my condition does not work? After all, if we remove the \\d{2} match check, then all elements separated by commas, except for empty ones, are displayed in the log.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question