Click here to get back home

Loopback with Device::SerialPort?

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Loopback with Device::SerialPort? Eric Schwartz 03-18-2005
Posted by Eric Schwartz on March 18, 2005, 1:24 pm
Please log in for more thread options


This is cross-posted to .misc and modules, as I'm not certain where it
belongs. Feel free to redirect the conversation to whichever group
you feel is more appropriate.

I'm trying to verify that I understand how to use Device::SerialPort
on a Linux machine correctly by connecting two serial ports on the
same machine with a crossover serial cable and sending data across the
connection.

The problem is that when the child process in the example below tries
to issue its first read() call, I this this error message:

Error #0 in Device::SerialPort::read at ./testserial line 42

I have no idea what this means, and I can't find any information on
what could cause this sort of error message anywhere in the
Device::SerialPort docs. I'm appending my test program below-- it's
not as small as I'd like, but most of that is the serial port setup
code, which has to be in there (AFAIK).

I've tried changing the code to use lookfor() in the child, with
$port2->are_match($STOP_STRING), but that results in a read of 0 bytes
and no error message.

My code:

#!/usr/bin/perl
use warnings;
use strict;
use Device::SerialPort;
use Digest::MD5 qw(md5_hex);

my $port1 = Device::SerialPort->new("/dev/ttyS1");
my $port2 = Device::SerialPort->new("/dev/ttyS2");

for my $port ($port1, $port2) {
$port->user_msg('ON');
$port->baudrate(115200);
$port->parity("none");
$port->databits(8);
$port->stopbits(1);
$port->handshake('none');
$port->write_settings || undef $port;
}

my $STOP_STRING = "Seriously dude, stop this, like now, evenn";

chomp(my $uname = `uname -r`);
open my $fh, "<", "/boot/vmlinuz-$uname" or die "Can't open kernel file: $!";
if (my $pid = fork) {
$port2->close();
binmode($fh);
my $accum;
while(read($fh, my $buf, 255)) {
$accum .= $buf;
$port1->write($buf);
}
$port1->write($STOP_STRING);
print "In parent, md5sum is: ", md5_hex($accum), "n";
$port1->close();
waitpid($pid,0);
} else {
die "cannot fork: $!" unless defined $pid;
close($fh);
$port1->close();
my $buf;
while (1) {
my ($count, $line) = $port2->read(255);
next unless $count;
last if $line eq $STOP_STRING;
$buf .= $line;
print "Child read $count bytes, total: ", length $buf , "n";
}
$port2->close();
print "In child, md5sum is: ", md5_hex($buf), "n";
exit;
}
__END__

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
                -- Blair Houghton.


Similar ThreadsPosted
problem with Device::SerialPort November 30, 2005, 4:19 pm
Problems with Device-SerialPort May 4, 2006, 2:15 pm
Help compiling Device::SerialPort May 9, 2006, 9:50 am
tie and tie with IO::Multiplex and Device:SerialPort? March 11, 2007, 10:55 am
device::serial October 2, 2007, 1:46 pm
Device::Gsm v1.34 released to CPAN August 18, 2004, 8:53 am
registering name Device::Cdio February 8, 2006, 6:30 pm
Device::Gsm v1.43 released to CPAN July 23, 2006, 11:54 am
Device::Modem v1.39 released to CPAN August 18, 2004, 8:49 am
How to control SCSI device in win32?? November 11, 2004, 4:25 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap