TheByteWorks

    Quality ready made and affordable custom software

Home

Software

PHP Serial
rsterm
Easy Control
Rack Designer
EzCom2Web

Quick Texts
Wallpaper Slideshow

Articles

All articles
WS2300 weather station PHP project
Your own terminal in 17 lines
PC PIC Voltmeter
Network messenger in 22 lines
Phone Dialer

Download
Purchase

Support

Details
E-mail

Copyright 2001-2024
© Cosmin Buhu

  GPS Logger
Screenshot

GPS Logger

Capturing and decoding GPS NMEA sentences.

Almost every GPS receiver is capable to output position, velocity, time and other useful information on its serial port. A common specification for this is implemented in a National Marine Electronics Association standard called NMEA 0183. The basement of this specification is the method to pack data in standardized packets called sentences; each sentence type is transmiting specific information but their format is the same for all of them:
- all start with $ character
- all end with CRLF sequence (Carriage Return - ASCII 13 and Line Feed - ASCII 10)
- specific data fields are separated with , character
Details about NMEA 0183 specification can be read for example here, with detailed description of sentences.

In this project we aim to build a simple interface for a GPS connected to PC serial port. Easy Control is the programming tool while Rack Designer is used to design User Interface. For your convenience all project files can be downloaded as a zip archive below. For a quick start (assuming you have Easy Control installed):
- download and unzip the archive
- start Easy Control and load gpslogger.ecs script file
- tweak serial port setting if needed
- connect the GPS to the serial port, be sure is switched on, and the interface is set to output NMEA data
- run the script and watch GPS data on screen
- analyze the script to see how things are done; you can use it as a starting point for your own. The script is fairly well commented, and is listed below.

Download this project :
GPS Logger

For people that don't want to install Easy Control just to try this (even is a fast fine programming tool), here is the same script built as an executable using Easy Control building feature. This is built with Easy Control trial so is running 1 minute then exits, and serial settings are 4800/8/N/1.
GPS Logger executable

More ?
What can be done more? Well, a lot. Here only RMC and GGA sentence types are used, you can easily implememt more if you need. You can add data integrity checking, file logging, and, why not, data transmission over TCP/IP to remote locations. You are in command!

Below is the script listing, as is seen in Easy Control IDE. Enjoy !


############################################
#              GPS Logger                  #
#       Easy Control script program        #
#  Log NMEA 0183 data from compatible GPS  #
############################################

############################################
#        Program settings                  #
############################################
comport = "com1"
baudrate = 4800
databits = 8
parity = "n"
stopbits = 1
terminator = chr( 10 )

############################################
#        Here starts main program loop      #
############################################

# open serial port
openport( comport, baudrate, databits, parity, stopbits)
# uncomment this if you want to see raw received data in a separate window
# showterm( comport, 500, 300, 16)
# open GUI interface, use full path if is not in the executable folder
openrack( "gpslogger.res" )
# this rack has 2 buttons named power and pause
# and some labels used to print data named latitude, longitude, speed ...
# start already powered, activate/deactivate buttons
power = 1
pause = 0
finished = 0
flush( comport )

# endless loop to keep program running
while (1)
    # if Pause button is pressed loop here
    while ( pause == 1 )
        # check Power button, maybe we have to exit
        if ( power == 0 )
             cleanup
             break
        endif
        # get rid of data arrived meanwhile
        flush( comport )
    wend
    if ( finished == 1 )
        break
    endif

    # start getting data and looking for NMEA sentences
    # wait sentence start character which is $
    posof$ = 0
    while (posof$ == 0)
        rcv = receive( comport )
        posof$ = pos( "$", rcv )
        # check Power button, maybe we have to exit
        if ( power == 0)
             cleanup
             break
        endif
    wend
    if ( finished == 1 )
        break
    endif
    # got $, now get data until end character is received (ASCII 10 decimal)
    posofterminator = 0
    while ( posofterminator == 0 )
        rcv = receive( comport )
        posofterminator = pos( terminator, rcv )
        # check Power button, maybe we have to exit
        if ( power == 0)
             cleanup
             break
        endif
    wend
    if ( finished == 1 )
        break
    endif
    # extract NMEA sentence
    sentence = mid( rcv, posof$, posofterminator)
    # delete used data from the serial port buffer
    flush( comport, posofterminator )
    rcv = ""

    # we got a sentence, see what type is and call process procedure
    posofGPRMC = pos ( "GPRMC", sentence)
    if ( posofGPRMC <> 0 )
        # just type the procedure name to call it
        gprmc
    endif
    posofGGA = pos ( "GPGGA", sentence)
    if ( posofGGA <> 0 )
        # just type the procedure name to call it
        gga
    endif

    # loop forever
wend
############################################
#        Here ends main program loop      #
############################################

############################################
#        GPRMC sentence process            #
############################################
procedure gprmc
       # split NMEA sentence in tokens separated by comma char
       tk = split( sentence, "," )
       # extract and process latitude
       latitude = mid( extract( 4 ), 1, 2 ) & "d" & mid( extract( 4 ), 3, 6 ) & "m " & extract( 5 )
       # extract and process longitude
       longitude = mid( extract( 6 ), 1, 3 ) & "d" & mid( extract( 6 ), 4, 6 ) & "m " & extract( 7 )
       # extract and process course
       course = extract( 9 )
       # extract and process speed
       speed = extract( 8 )
       # extract and process date
       date = mid( extract( 10 ), 1, 2 ) & "/" & mid( extract( 10 ), 3, 2 ) & "/20" & mid( extract( 10 ), 5, 2 )
       # extract and process date
       time = mid( extract( 2 ), 1, 2 ) & ":" & mid( extract( 2 ), 3, 2 ) & ":" & mid( extract( 2 ), 5, 2 )
procend

############################################
#        GGA sentence process            #
############################################
procedure gga
       # split NMEA sentence in tokens separated by comma char
       tk = split( sentence, "," )
       # extract and process satellites number
       satellites = extract( 8 )
       # extract and process altitude
       altitude = extract( 10 )
procend

############################################
#        Clean-up procedure                #
############################################
procedure cleanup
    # first close serial port
    close( comport )
    finished = 1
    pause =0
procend