Dear Monks,

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:

  1. nothing happens, nothing. rerun program and it's fine.
  2. one line output (may or may not have altitude). then, more nothing.
The altitude is sometimes missing from the first or second line of output, but once (in many of test runs) I saw it spit this thing out as the altitude: x???x??x???f3Y??c??fF? which I think may be undecoded GPS data from my experiments with Serial Communication in Perl.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.