This little perl program uses GPS::NMEA to read data from a generic USB GPS dongle and continuously print out the results. It works great, when it works, which is most of the time, and I've never seen it stop. But sometimes when the program is first run:
Anyway I need this thing to run no matter what and was wondering what's the best technique to achieve total fault tolerance. I tried to catch errors by checking the object and using eval on the hanging method (get_position has this until loop waiting to parse) to no avail. Would it be wise to fork the program and somehow monitor it for activity from the parent so a hanging child process could be restarted or something? How would that be done? Thank you!
#!/usr/bin/perl use strict; use warnings; use GPS::NMEA; use Time::Piece; my $gps = GPS::NMEA->new( Port => '/dev/tty.usbserial-110', # ls -l /dev | grep usb | grep tty Baud => 4800 # can't connect this old unit at 9600 ); STDOUT->autoflush(1); while () { # https://metacpan.org/dist/perl-GPS/source/NMEA.pm#L70 if (my ($ns, $lat, $ew, $lon) = $gps->get_position) { # hangs right +here my $ddmmyy = $gps->{NMEADATA}->{ddmmyy} || '???'; my $time_utc = $gps->{NMEADATA}->{time_utc} || '???'; my $alt_meters = $gps->{NMEADATA}->{alt_meters} || '???'; $time_utc =~ s/\.\d+$//; if (my $t = Time::Piece->strptime("$ddmmyy $time_utc","%d%m%y %H:% +M:%S")){ $time_utc = $t->strftime } print join ' ', $ns, $lat, $ew, $lon, 'A', $alt_meters, $time_utc, + "\n" } } __END__ Output, once per second: N 12.345678 W 123.456789 A 123.4 Mon, 29 Apr 2024 12:34:01 UTC N 12.345678 W 123.456789 A 123.4 Mon, 29 Apr 2024 12:34:02 UTC N 12.345678 W 123.456789 A 123.4 Mon, 29 Apr 2024 12:34:03 UTC
In reply to Total fault tolerance for perl GPS program by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |