Web lists-archives.com

Re: Serial Ports and Perl

On 10/26/2017 05:37 PM, Martin McCormick wrote:
The perl list I subscribe to seems to be on the fritz or I would
take the question there.  I want to write code that receives from
a RS-232 port and I just can't seem to get it to do anything.

	The port I am reading is connected to a scanner radio and
produces generally short lines of text such as CD13, - or +, each
followed by a carriage return so these data should be very easy
to read.

	If I use the kermit program, I do see the data when the
radio receives a signal but if I run the following script which
should hold and wait for some data, it holds and waits forever
even when data are present

#!/usr/bin/perl -w
use strict;
use Device::SerialPort;

sub comm {    #serialport

my $port = Device::SerialPort->new("/dev/ttyUSB0");
$port->baudrate(9600); # Configure this to match your device
#This is supposed to flush the buffers.
#This causes an infinite loop so it should hang and receive
while (1) {
        my $char = $port->lookfor;
#When there is nothing, go back and keep retrying.
     if ($char) {
        print "$char\n";
}    #serial port

#Call the subroutine.


	If this was working, it would show a column of all the
ASCII characters being received.  /dev/ttyUSB0 is a valid device
and works when used with kermit or even a C program I wrote.

	If anybody has gotten the perl Device::SerialPort to
work, I am interested to know what I am doing or not doing.

	Thank you for any constructive ideas.

Martin McCormick  WB5AGZ

Some years ago I used Perl for driving a pen plotter and I believe I used it for some input function also. Unix considers devices to be more or less the same as files so the serial port was accessed the same as a file would be.

$port = "/dev/sts/ttyp02";
open (PLOTTER,">$port")  || die "something or other:$!\n";
This would open it for writing. You are using "sane" parameters that serial ports default to so you wouldn't need to specify them.
Best regards,
Fred Boatwright