courselog.gpsutils
Class SerialGPSConnection

java.lang.Object
  extended by courselog.gpsutils.SerialGPSConnection
All Implemented Interfaces:
java.lang.Runnable

public class SerialGPSConnection
extends java.lang.Object
implements java.lang.Runnable

Handles a GPS connected to a Serial port. Generates Measurements (MeasuredPoint) each time a new measurement is issued by the receiver. Handles a listener to the connection to receive all new points. Configuration of the device is planned but ot implemented : - enable differential measurements (WAAS or EGNOS) when available - initialize for fast re-start - power saving mode


Field Summary
protected static byte[] bGPGGA
          Identifier of a GGA NMEA sentence ("GPGGA").
protected static byte[] bGPGSV
          Identifier of a GSV NMEA sentence ("GPGSV").
protected static byte[] bGPRMC
          Identifier of a RMC NMEA sentence ("GPRMC").
static int BUFFMAX
          Maximum serial port buffer size (256 chars).
 MeasuredPoint currentPoint
          Current point received from the GPS.
 ewe.io.SerialPortOptions currentSPO
          Port options that were used to open the serial port to the GPS.
 int debugCounter
          Debugging counter to verify problems with connections
protected static java.lang.String fGPGGA
          Values sequence in a GGA sentence ("VVCVCIIVVCVCVI").
protected static java.lang.String fGPGSV
          Values sequence in a GSV sentence ("IIIIIIIIIIIIIIIIIII").
protected static java.lang.String fGPRMC
          Values sequence in a RMC sentence ("VCVCVCVVIVVC").
 ewe.io.Stream inGPS
          Input reader of the GPS serial port.
static boolean isEmulating
          Flag indicating that the receive should read from a "tmpGPSLog.txt" file in order to emulate the presence of a receiver.
static boolean isLogging
          Flag indicating that the receiver should log its data.
static java.lang.String logFileName
          For debugging, GPS data may be logged into a file.
protected  ewe.ui.EventListener myListener
          Listener to this receiver.
protected  ReceiverEvent myPointEvent
          Event passed to listeners when a new point is decoded.
protected  ReceiverEvent myStatusEvent
          Event passed to listeners when the status of the receiver changes.
static int NBLINESINBUFFER
          Number of lines to buffer before parsing NMEA.
static int NBSATMAX
          Maximum number of satellites status to record (20).
protected  ewe.io.FileOutputStream outLog
          Stream to output the logged GPS data to.
 ewe.sys.mThread reader
          Thread reading the GPS receiver values and issueing events when a new point is decoded.
 int status
          Status of the receiver (one of STATUS_XXX values).
static int STATUS_GPSLOST
          GPs is connected via the serial port but the GPS produces no valid fix.
static int STATUS_NOK
          GPS receiver does not transmit through the serial port.
static int STATUS_OK
          Receiver status is fine!
 char[] vNMEACar
          Array containing single char values decoded from NMEA (20 char).
 int[] vNMEAInt
          Array containing integer values decoded from NMEA (20 ints).
 double[] vNMEAVal
          Array containing double values decoded from NMEA (20 double).
 int[] vSatAz
          State of the GPS, azimtuhs of the satellites currently available.
 int[] vSatEl
          State of the GPS, elevations of the satellites currently available.
 int[] vSatPRN
          State of the GPS, idenfiers of the satellites currently available.
 int[] vSatSNR
          State of the GPS, signal to noise of the satellites currently available.
 
Constructor Summary
SerialGPSConnection(ewe.io.SerialPortOptions spo, ewe.ui.EventListener listener)
          Creates a new instance of SerialGPSConnection
 
Method Summary
 void analyseNMEAValues(byte[] b, int iSt, int iEnd)
          When a valid sentence is received, analyses it to update the current point values.
 void close()
          Stop the GPS reception.
static SerialGPSConnection getAnActiveSerialGPSConnection(ewe.io.SerialPortOptions spo, ewe.ui.EventListener listener)
          Factory for serial connection creating an active connection.
static ewe.io.SerialPortOptions getDefaultPortOptions()
          Provides a set of default port options.
static java.lang.String getPortOptionsAsString(ewe.io.SerialPortOptions forSPO)
          Format forSPO in a string that will be readable later.
static ewe.io.SerialPortOptions getPortOptionsFromString(java.lang.String sO)
          Interprets a string with port name, rate, etc, into serial port options.
 java.lang.String getUTCFromGPS()
          Get UTC provided by the GPS receiver.
 boolean isCheckSumOK(int valChk, byte[] charChk, int idxChk)
          Verify checksum of an NMEA sentence.
 int parseNMEAContent(byte[] b, int len)
          Reads in the buffer to find NMEA sentences and analyses them in case there are.
 void run()
          Method reading the GPS actived in thread reader.
 void setListener(ewe.ui.EventListener list)
          Sets the listener of this receiver.
 boolean startLog()
          Start to log received data fom the serial port.
 boolean startReading()
          Starts the GPS reading thread myReader.
 void stopLog()
          Stops logging GPS data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATUS_OK

public static final int STATUS_OK
Receiver status is fine!

See Also:
Constant Field Values

STATUS_GPSLOST

public static final int STATUS_GPSLOST
GPs is connected via the serial port but the GPS produces no valid fix.

See Also:
Constant Field Values

STATUS_NOK

public static final int STATUS_NOK
GPS receiver does not transmit through the serial port.

See Also:
Constant Field Values

BUFFMAX

public static int BUFFMAX
Maximum serial port buffer size (256 chars). If bigger, data is blocked long before being exploited, if smaller, partial NMEA sentences are received and not usable. NMEA specifies that the sentences should not exceed 80 chars.


NBLINESINBUFFER

public static int NBLINESINBUFFER
Number of lines to buffer before parsing NMEA.


NBSATMAX

public static int NBSATMAX
Maximum number of satellites status to record (20).


bGPGGA

protected static final byte[] bGPGGA
Identifier of a GGA NMEA sentence ("GPGGA").


fGPGGA

protected static final java.lang.String fGPGGA
Values sequence in a GGA sentence ("VVCVCIIVVCVCVI").

See Also:
Constant Field Values

bGPRMC

protected static final byte[] bGPRMC
Identifier of a RMC NMEA sentence ("GPRMC").


fGPRMC

protected static final java.lang.String fGPRMC
Values sequence in a RMC sentence ("VCVCVCVVIVVC").

See Also:
Constant Field Values

bGPGSV

protected static final byte[] bGPGSV
Identifier of a GSV NMEA sentence ("GPGSV").


fGPGSV

protected static final java.lang.String fGPGSV
Values sequence in a GSV sentence ("IIIIIIIIIIIIIIIIIII").

See Also:
Constant Field Values

logFileName

public static java.lang.String logFileName
For debugging, GPS data may be logged into a file. ("tmpGPSLog.txt")


isLogging

public static boolean isLogging
Flag indicating that the receiver should log its data.


isEmulating

public static boolean isEmulating
Flag indicating that the receive should read from a "tmpGPSLog.txt" file in order to emulate the presence of a receiver.


debugCounter

public int debugCounter
Debugging counter to verify problems with connections


outLog

protected ewe.io.FileOutputStream outLog
Stream to output the logged GPS data to.


currentPoint

public MeasuredPoint currentPoint
Current point received from the GPS. Same object is used, fields are updated depending on the received data.


status

public int status
Status of the receiver (one of STATUS_XXX values).


reader

public ewe.sys.mThread reader
Thread reading the GPS receiver values and issueing events when a new point is decoded.


currentSPO

public ewe.io.SerialPortOptions currentSPO
Port options that were used to open the serial port to the GPS.


inGPS

public ewe.io.Stream inGPS
Input reader of the GPS serial port.


myPointEvent

protected ReceiverEvent myPointEvent
Event passed to listeners when a new point is decoded. Same object is reused.


myStatusEvent

protected ReceiverEvent myStatusEvent
Event passed to listeners when the status of the receiver changes. Same object is reused.


myListener

protected ewe.ui.EventListener myListener
Listener to this receiver.


vNMEAInt

public int[] vNMEAInt
Array containing integer values decoded from NMEA (20 ints).


vNMEAVal

public double[] vNMEAVal
Array containing double values decoded from NMEA (20 double).


vNMEACar

public char[] vNMEACar
Array containing single char values decoded from NMEA (20 char).


vSatPRN

public int[] vSatPRN
State of the GPS, idenfiers of the satellites currently available.


vSatAz

public int[] vSatAz
State of the GPS, azimtuhs of the satellites currently available.


vSatEl

public int[] vSatEl
State of the GPS, elevations of the satellites currently available.


vSatSNR

public int[] vSatSNR
State of the GPS, signal to noise of the satellites currently available.

Constructor Detail

SerialGPSConnection

public SerialGPSConnection(ewe.io.SerialPortOptions spo,
                           ewe.ui.EventListener listener)
Creates a new instance of SerialGPSConnection

Parameters:
spo - Serial port options to use for connection to the GPS.
listener - Listener which will receive the state and points events.
Method Detail

getAnActiveSerialGPSConnection

public static SerialGPSConnection getAnActiveSerialGPSConnection(ewe.io.SerialPortOptions spo,
                                                                 ewe.ui.EventListener listener)
Factory for serial connection creating an active connection. Call this method instead of the constructor and get a connection generating data. If this method is called within an mThread, it tries to reconnect every 2s an only returns if interrupted. Otherwise (not in an mThread) returns a new connection even if connection fails.

Parameters:
spo - Serial ort options to use for the connection
listener - Listener for GPS events
Returns:
A new instance of a SerialGPSConnection with an active connection to a GPS device. null if the connection process is interrupted.

startReading

public boolean startReading()
Starts the GPS reading thread myReader.

Returns:
true is the thread started, false otherwise.

run

public void run()
Method reading the GPS actived in thread reader. This method also parses NMEA values and activates the listener in the reader thread. May only be stopped by an interruption on reader thread.

Specified by:
run in interface java.lang.Runnable

parseNMEAContent

public int parseNMEAContent(byte[] b,
                            int len)
Reads in the buffer to find NMEA sentences and analyses them in case there are. Also checks the checksum of each sentence.

Parameters:
b - Buffer to read in.
len - Length of usable chars in the buffer, starting at 0.
Returns:
Number of chars consummed by the analysis of NMEA sentences.

analyseNMEAValues

public void analyseNMEAValues(byte[] b,
                              int iSt,
                              int iEnd)
When a valid sentence is received, analyses it to update the current point values.
 GGA : get UTC, lat, lon, nbsats, alt, hdop
 RMC : get date, speed, heading
 GSV : updates satellite status (no event is generated
      but data is available for polling).
When a point is fully analysed, generate a ReceiverEvent. If fix is not valid, modify status accordingly.

Parameters:
b - Buffer to read in.
iSt - Index of starting location of the NMEA sentence.
iEnd - Index of ending location of the sentence.

close

public void close()
Stop the GPS reception. Stops the thread and close the log if any.


startLog

public boolean startLog()
Start to log received data fom the serial port. Data is appended to the file which only grows...

Returns:
true if correct, false otherwise.

stopLog

public void stopLog()
Stops logging GPS data.


isCheckSumOK

public boolean isCheckSumOK(int valChk,
                            byte[] charChk,
                            int idxChk)
Verify checksum of an NMEA sentence. Checksum is a XOR of all characters after the '$' and before the '*'.

Parameters:
valChk - Computed checksum of the sentence.
charChk - Hexadecimal value of the checksum read from the port.
idxChk - Index of the checksum value in charChk.
Returns:
True if valChk equals interpretation of charChk.

getPortOptionsFromString

public static ewe.io.SerialPortOptions getPortOptionsFromString(java.lang.String sO)
Interprets a string with port name, rate, etc, into serial port options. Example : "{name=COM6:,rate=4800,bits=8,parity=0,stop=1}"

Parameters:
sO - String containing the description of the options.
Returns:
Initialised new Serial port options.

getDefaultPortOptions

public static ewe.io.SerialPortOptions getDefaultPortOptions()
Provides a set of default port options. Corresponds to what works with my configuration : {name=COM6:,rate=9600,bits=8,parity=0,stop=1}

Returns:
Default port options.

getPortOptionsAsString

public static java.lang.String getPortOptionsAsString(ewe.io.SerialPortOptions forSPO)
Format forSPO in a string that will be readable later.

Parameters:
forSPO - Options to format.
Returns:
String that may be parsed later to reconfigure options.

getUTCFromGPS

public java.lang.String getUTCFromGPS()
Get UTC provided by the GPS receiver.

Returns:
Time from receiver as a string "HH:MM:SS"

setListener

public void setListener(ewe.ui.EventListener list)
Sets the listener of this receiver. Should not be null if an event occurs.

Parameters:
list - Listener to dispatch events to.