Web lists-archives.com

Re: Serial Ports and Perl




On Thu, Oct 26, 2017 at 07:37:07PM -0500, 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
> $port->databits(8);
> $port->parity("none");
> $port->stopbits(1);
>    $port->handshake("none");
>        $port->write_settings;
> #This is supposed to flush the buffers.
> $port->lookclear;
> #This causes an infinite loop so it should hang and receive
> #characters.
> while (1) {
>        my $char = $port->lookfor;
> #When there is nothing, go back and keep retrying.
>     if ($char) {
>        print "$char\n";
> }
>        }
> return;
> }    #serial port
> 
> #Call the subroutine.
> 
> comm;
> 
> 	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
>

This is a good starting point:
http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/
It is not perl centric (c in fact). But it tells you the general basics for
serial communication.

-H 

-- 
Henning Follmann           | hfollmann@xxxxxxxxxxxxxxx