Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have NMEA text traffic coming in a USB port which I would like to capture & massage. I can view this traffic through cu(1). I realize script(1) can capture serial output, but I don't want to spool all this data to disk; I would rather intercept the traffic to compute various parameters & throw it away. Is there a way I can split off this traffic for analysis in Perl?

Thanks for any candor shared.

Replies are listed 'Best First'.
Re: trapping output of cu(1)?
by superfrink (Curate) on Oct 03, 2007 at 16:31 UTC
    I have used Device::SerialPort with a USB device that was presented as a serial device in /dev .
      I suspected there would a simple way to redirect, and it appears that piping works:
      #!/usr/bin/perl -w use strict; open(IN, 'cu -l cuaU0 -s 4800 |') or die 'unable to connect'; while (<IN>) { print; } close IN;
      Is there anything glaringly wrong with this approach?

      Thanks.

        When you open a process like that you can only read from it. That might be fine but if you need to read and write check into Expect like bruceb3 mentioned.

        Update: I don't see why you couldn't do some forking and create a child process with STDIN and STDOUT pointing to file handles the parent shares and then have the child exec the cu command. Try looking in perlfaq for "dup". How do I dup a filehandle in Perl

      I'm using one of the *BSD's. Do you have any knowledge if this module works there too?

      Is there not a way to redirect or dup the stdout filehandle used by another process?

      Thanks.

Re: trapping output of cu(1)?
by bruceb3 (Pilgrim) on Oct 03, 2007 at 17:09 UTC
    You could always run the cu command via the Expect module and read the output that way.