Very light weight GPS position
GrandFather
created: 2006-08-11 05:36:46

The following code reads data from a serial port connected GPS receiver generating NMEA GGA sentences and shows the current time and location.

Note that this is not a full NMEA sentence parser and it doesn't show a continuously updated time and position. It was written in a hurry to facilitate observing a lunar graze occultation.

use strict;
use warnings;
use Tk;

my $fh;

my $mw = MainWindow->new;

$mw->withdraw;
if (! open $fh, '<', "COM4:") {
    $mw->messageBox (
        -message => 'GPS unit not available',
        -title => 'Time and location', -type => 'Ok',
        -icon => 'info',
        );
    exit;
}

my $location;
while (<$fh>) {
    next if ! /^\$GPGGA,/;
    chomp;
    
    my ($hour, $min, $sec, $latDeg, $latMin, $latHemi, $longDeg, $longMin, $longHem) =
        /(\d\d)(\d\d)(\d\d),(\d\d)([\d.]+),(\S),(\d\d)([\d.]+),(\S),/;

    next if ! defined ($longHem);
    
    $latMin = toMS ($latMin);
    $longMin = toMS ($longMin);
    $location = sprintf "%s", "$hour:$min:$sec, $latDeg $latMin$latHemi, $longDeg $longMin$longHem";
    last;
}


$mw->messageBox (
    -message => $location, -title => 'Time and location', -type => 'Ok'
    );

sub toMS {
    my $x = shift;
    my $min = int ($x);
    $x = ($x - $min) * 60;
    return sprintf "%d %.2f", $min, $x;
}

DWIM is Perl's answer to Gödel
Re: Very light weight GPS position
created: 2006-08-29 22:44:40
Hello!
I tried the code from the original post on Windows XP, ActivePerl 5.8.7 Build 815 with a Garmin GPS 12CX attached to COM4 and it didn't work.
No worries though.
So... I put together a variation of the original post using [cpan://GPS::Garmin] which is a subset module to [cpan://perl::GPS]
I have posted the "new code" below.
++ to the original author for inspiring me to dig further and find this module to interface with my Garmin! Yet Another Cool Use For Perl!
Important Note: Garmin::GPS on Win32 wants to see installed [cpan://Win32::SerialPort], however, ActiveState does not appear to offer it for perl 5.8 and above. I did a little digging and installed it directly via the ppm prompt by issuing the command
ppm>install http://www.bribes.org/perl/ppm/Win32-SerialPort.ppd
With that little problem solved the script below works quite well.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use GPS::Garmin; 
use Win32::SerialPort;


my $mw = MainWindow->new;


$mw->withdraw;

my $location;

my $gps = new GPS::Garmin( 'Port' => 'COM4', 'Baud' => 9600, );
my ($latsign,$lat,$lonsign,$lon) = $gps->get_position;
my ($sec,$min,$hour,$mday,$mon,$year) = $gps->get_time;
my $time="$hour:$min:$sec";
$lat=sprintf("%.4f",$lat);
$lon=sprintf("%.4f",$lon);

$location="$lat$latsign $lon$lonsign";
my $locationandtime="$lat$latsign $lon$lonsign\n$time"." GMT";

$mw->messageBox (
    -message => $locationandtime, -title => 'Time and location', -type => 'Ok'
    );

I am thinking more possibilities for tinkering there will soon be! :)

perlmonks.org content © perlmonks.org and GrandFather, PerlBear

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03