Re: Serial Ports and Perl
Andy Smith <andy@strugglers.net> writes:
> Hi Martin,
>
> I have been using it successfully for a long time, but all I do is
> read whole lines from the serial device like:
>
> my $dev = '/dev/ttyUSB0';
> my $port = Device::SerialPort->new($dev);
>
> $port->baudrate(57600);
> $port->write_settings;
>
> open my $fh, '<', $dev or die "Can't open $dev: $!";
>
> while (<$fh>) {
> print "READ: $_\n";
> }
>
> So, I am only using Device::SerialPort to configure the device,
> while all reading is done by treating it as a normal file.
>
> I see you are using lookfor(), which I have never used before. From
> a brief look at:
>
>
> http://search.cpan.org/~cook/Device-SerialPort-1.002/SerialPort.pm#Methods_for_I/O_Processing
>
> it seems you should be setting are_match() if you want lookfor() to
> match anything.
You are correct. After a good night's sleep and another
read in to perldoc Device::SerialPort, I found a directive one
can use as follows:
if ($string_in = $PortObj->input) { PortObj->write($string_in); }
# simple echo with no control character processing
If one writes an infinite loop such as while (1) {
if (my $c = $port->input)
{
print ("$c\n");
}
}
all data being received by the device are echoed so you know it
is good so far. That in itself saves a lot of trouble-shooting time.
Thanks for helping me think a bit.
Martin McCormick
Reply to: