How to Parse NMEA Sentences Using the nmea4j Library

Written by

in

The nmea4j library is an open-source Java framework hosted on SourceForge designed to fully implement the NMEA 0183 protocol (version 3.0) without proprietary extensions. It provides a clean API to parse raw text strings from GPS receivers into typed Java objects, handle event-driven data streaming, and even simulate GPS hardware. Core Architecture

The library relies on three main components to capture and process data:

NmeaParser: The core component that accepts data streams or individual lines, verifies the NMEA checksum, and breaks strings into individual fields.

Sentence Object (and subclasses): Represent the data structures mapping to specific NMEA types (e.g., GGASentence, RMCSentence).

SentenceListener: An interface used to bind an event-driven mechanism to input data streams, executing custom logic whenever a specific sentence is successfully parsed. Step 1: Parsing Individual NMEA Strings

For simple applications where you already have string data (e.g., from a database or UDP packet), you manually pass strings into the parser.

import net.sf.nmea4j.NmeaParser; import net.sf.nmea4j.data.Sentence; import net.sf.nmea4j.data.GGASentence; // Example sentence type public class ManualParseExample { public static void main(String[] args) { // Instantiate the core nmea4j parser NmeaParser parser = new NmeaParser(); // A raw NMEA 0183 string String rawSentence = “$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47”; try { // Parse raw text into a structured Sentence object Sentence sentence = parser.parse(rawSentence); // Check the exact type of sentence received if (sentence instanceof GGASentence) { GGASentence gga = (GGASentence) sentence; // Extract typed data fields directly System.out.println(“Latitude: ” + gga.getLatitude()); System.out.println(“Longitude: ” + gga.getLongitude()); System.out.println(“Satellites in view: ” + gga.getSatelliteCount()); System.out.println(“Altitude: ” + gga.getAltitude() + “ ” + gga.getAltitudeUnits()); } } catch (Exception e) { System.err.println(“Failed to parse sentence or invalid checksum: ” + e.getMessage()); } } } Use code with caution. Step 2: Streaming Data with Event Listeners

When dealing with a continuous, real-time data stream (such as a serial port connection or network socket to a GPS receiver), the event-listener pattern is highly recommended.

import net.sf.nmea4j.NmeaParser; import net.sf.nmea4j.data.RMCSentence; import net.sf.nmea4j.event.SentenceListener; import java.io.InputStream; import java.io.FileInputStream; public class StreamParseExample { public static void main(String[] args) { try { NmeaParser parser = new NmeaParser(); // Register an anonymous listener targeting the parser parser.addSentenceListener(new SentenceListener() { @Override public void sentenceRead(net.sf.nmea4j.event.SentenceEvent event) { // Check if the freshly parsed stream item is a Recommended Minimum (RMC) sentence if (event.getSentence() instanceof RMCSentence) { RMCSentence rmc = (RMCSentence) event.getSentence(); System.out.println(“Speed over ground (knots): ” + rmc.getSpeed()); System.out.println(“True course: ” + rmc.getCourse()); } } }); // Feed a data stream (e.g., tracking data file, serial port, or network stream) to the parser InputStream inputStream = new FileInputStream(“gps_data_log.txt”); parser.parse(inputStream); } catch (Exception e) { e.printStackTrace(); } } } Use code with caution. Supported Sentence Formats

The core implementation maps data fields for the standard NMEA 0183 version 3.0 types: GGASentence: Time, position, and 3D fix accuracy data.

RMCSentence: Time, date, position, speed, and course (Recommended Minimum Navigation Data).

GSASentence: GPS receiver operating mode and active satellites.

GSVSentence: Detailed information about satellites currently visible in the sky. GLLSentences: Latitude and longitude positioning.

Parsing NMEA strings received via UDP – Howto – Arduino Forum

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *