NMEA Java APIs

A Java Parser for NMEA Streams

Hosted by sourceforge

Check out the project page, where you can download sources and binaries from.
Check out the javadoc.

This package provides a smooth way to read the NMEA stream from a device.

Related to:

Content

Description

Table of Content
NMEA defines the structure of the flow of data that can be read from an NMEA compliant device. In short, the NMEA stream is:

Architecture

Table of Content
The package is built following the requirements of the Model-View-Controller (MVC) architecture.
Also, it can be considered as mimicking the behavior of an XML SAX Parser, as it is actually reading an NMEA stream, and sending event everytime a sentence you subscribed to is met.

Examples

Table of Content

The Model

We provide two samples: a simulator, and a real one, connected on the serial port.

The simulator

package ui.sampleclient;

import ocss.nmea.api.NMEAReader;
import ocss.nmea.api.NMEAEvent;
import java.util.ArrayList;
import java.io.FileInputStream;

/**
 * A Simulator, taking its inputs from a file
 */
public class CustomReader extends NMEAReader // Notice the extended class
{
  public CustomReader(ArrayList al)
  {
    super(al);
  }

  public void read() // The read() method must be overwritten
  {
    // Simulation
    super.enableReading(); // Then, canRead() will return true
    String fileName = "hydra2.nmea";
    try
    {
      FileInputStream fis = new FileInputStream(fileName);
      while (canRead())    // canRead() is a method of the super class
      {
        double size = Math.random();
        int dim = (int)(750 * size);
        byte[] ba = new byte[dim];
        int l = fis.read(ba);
        if (l != -1 && dim > 0)
        {
          fireDataRead(new NMEAEvent(this, new String(ba))); // Broadcast the event
          try { Thread.sleep(500); } catch (Exception ignore) {}
        }
        else
        {
          System.out.println("===== Reseting Reader =====");
          fis.close();
          fis = new FileInputStream(fileName);
        }
      }
    }
    catch (Exception e)
    {
     e.printStackTrace();
    }
  }
}
        

The real one

package ui.sampleclient;
import ocss.nmea.api.NMEAReader;
import ocss.nmea.api.NMEAEvent;
import java.util.ArrayList;

import javax.comm.*;
import java.util.*;
import java.io.*;

public class CustomSerialClient extends NMEAReader // Notice the extended class
{
  public CustomSerialClient(ArrayList al)
  {
    super(al);
  }

  public void read()
  {
    super.enableReading();
    // Opening Serial port COM1
    CommPortIdentifier com = null;
    try { com = CommPortIdentifier.getPortIdentifier("COM1"); }
    catch (NoSuchPortException nspe)
    {
      System.err.println("No Such Port");
      return;
    }
    CommPort thePort = null;
    try { thePort = com.open("PortOpener", 10); }
    catch (PortInUseException piue)
    {
      System.err.println("Port In Use");
      return;
    }
    int portType = com.getPortType();
    if (portType == com.PORT_PARALLEL)
      System.out.println("This is a parallel port");
    else if (portType == com.PORT_SERIAL)
      System.out.println("This is a serial port");
    else
      System.out.println("This is an unknown port:" + portType);
    if (portType == com.PORT_SERIAL)
    {
      SerialPort sp = (SerialPort)thePort;
      try
      {
        // Settings for B&G Hydra
        sp.setSerialPortParams(4800,
                               SerialPort.DATABITS_8,
                               SerialPort.STOPBITS_1,
                               SerialPort.PARITY_NONE);
      }
      catch (UnsupportedCommOperationException ucoe)
      {
        System.err.println("Unsupported Comm Operation");
        return;
      }
    }
    // Reading on Serial Port
    try
    {
      byte[] buffer = new byte[4096];
      InputStream theInput = thePort.getInputStream();
      System.out.println("Reading serial port...");
      while (canRead()) // Loop
      {
        int bytesRead = theInput.read(buffer);
        if (bytesRead == -1)
          break;
        System.out.println("Read " + bytesRead + " characters");
        // Count up to the first not null
        int nn = bytesRead;
        for (int i=0; i<Math.min(buffer.length, bytesRead); i++)
        {
          if (buffer[i] == 0)
          {
            nn = i;
            break;
          }
        }
        byte[] toPrint = new byte[nn];
        for (int i=0; i<nn; i++)
          toPrint[i] = buffer[i];
        // Broadcast event
        super.fireDataRead(new NMEAEvent(this, new String(toPrint))); // Broadcast the event
      }
      System.out.println("Stop Reading serial port.");
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}
        

The Controller

The Controller (ocss.nmea.api.NMEAParser) doesn't need any customization or modification.

The View

The simplest:
package ui.sampleclient;
import ocss.nmea.api.NMEAClient;
import ocss.nmea.api.NMEAEvent;

/**
 * The simplest you can write
 */
public class CustomClient extends NMEAClient
{
  public CustomClient(String s, String[] sa)
  {
    super(s, sa);
  }

  public void dataDetectedEvent(NMEAEvent e)
  {
    System.out.println("Received:" + e.getContent());
  }

  public static void main(String[] args)
  {
    String prefix = "II";
    String[] array = {"HDM", "GLL", "XTE", "MWV", "VHW"};
    CustomClient customClient = new CustomClient(prefix, array);
    customClient.initClient();
    customClient.setReader(new CustomReader(customClient.getListeners()));       // Simulator
//  customClient.setReader(new CustomSerialClient(customClient.getListeners())); // Serial Port reader
    customClient.startWorking();
  }
}
      


© 2002 and beyond, Oliv Cool Stuff Soft
Send your comments or questions to me.