(Update to previous GPS/GlassPanel emails) (script would typically be saved as GPSReader.tcl) #################################################################### # These lines with the '#' character are reader comments. # POSSIBLE PROBLEM --- If one tries to include the following script into an email ... # Some email programs will insert Line-Feed/Carriage-Retun characters into long lines. # If you want to run the TCL script you might have to first copy the script to a text editor to # remove those characters inserted by your email program. # Reads GPS unit connected to USB port and displays data in terminal window. set serial [open /dev/ttyUSB0 r] fconfigure $serial -mode "4800,n,8,1" fconfigure $serial -blocking 0 -buffering none -translation binary # NMEACode 'GPRMC' identifies the line of NMEA data we want ... "RECOMMENDED MINIMUM SENTENCE" set gpsstring "" while {1} { ;# False = 0; True = non-zero; therefore we have an infinite-loop # TCL allows nesting of commands within [square-brackets] set data [read $serial] ;# read ALL incoming bytes set size [string length $data] ;# number of received byte (may be 0) if { $size } { ;# ie ... any size > 0 evaluates as True append gpsstring $data ;# if something received, append data to line of NMEA data # puts "building: |$gpsstring|" ;# echo ... debugging - put temp-string on the screen # Parse the input data string & locate/index-positons of each data field using substring function # according to NMEA record layout. set AsteriskPos [string first * $gpsstring] if { $AsteriskPos != -1 } { ;# the '*' identifies the checksum field near end of line set NMEADollarPos [string first \$ $gpsstring] set GPRMCPos [string first GPRMC $gpsstring] ;# see if line represents RMC data set GMTPos [expr { $GPRMCPos + 6 }] set StatusAPos [string first A $gpsstring $GPRMCPos] set StatusVPos [string first V $gpsstring $GPRMCPos] set LatNPos [string first N $gpsstring $GPRMCPos] set LatSPos [string first S $gpsstring $GPRMCPos] set LongEPos [string first E $gpsstring $GPRMCPos] set LongWPos [string first W $gpsstring $GPRMCPos] set SpeedPos [expr { $LongEPos + 2 }] set TrackPos [expr { $SpeedPos + 6 }] set DatePos [expr { $SpeedPos + 12 }] set MagVarPos [expr { $SpeedPos + 19 }] if { $GPRMCPos > -1 } { ;# Line contains 'GPRMC' and data we want # puts "final string: |$gpsstring|" ;# for debugging # puts ************** set latitude [string range $gpsstring [expr {$LatNPos-10}] [expr {$LatNPos-2}]] set longitude [string range $gpsstring [expr {$LongEPos-10}] [expr {$LongEPos-2}]] set speed [string range $gpsstring $SpeedPos [expr {$SpeedPos + 4}]] set track [string range $gpsstring $TrackPos [expr {$TrackPos + 4}]] puts "Latitude: $latitude" puts "Longitude: $longitude" puts "Speed: $speed" puts "Track: $track" puts ************** } } set gpsstring [string range $gpsstring $AsteriskPos 200] ;# re-intialize input string ;# save characters read beyond '*' for next line update;# Display output, allow to close wish-window } ;# end found * near end of line. } ;# end size loop ... ie real non-null data read from USB port } ;# end while infinite loop