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;
}
ppm>install http://www.bribes.org/perl/ppm/Win32-SerialPort.ppdWith 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