[grand-rapids-pm-list] The Perl - TCP/IP serial cable

Bill Ott bott at grpl.org
Fri Sep 30 09:52:37 PDT 2005


I had a request for another look at the code for the serial cable.  Here 
are the two pieces.

-Bill


com2sock.pl:

#!/usr/bin/perl
#use strict;
use IO::Socket;
use Device::SerialPort;

$com2 = Device::SerialPort->start ("/usr/local/work/cuaa0.cfg") or die "Can't start COM1\n";
my ($remote, $port, $iaddr, $paddr, $proto, $line, $buf);
$remote  = '123.123.123.123';
$port    = 12;  # random port
$iaddr   = inet_aton($remote)               || die "no host: $remote";
$paddr   = sockaddr_in($port, $iaddr);
$proto   = getprotobyname('tcp');
$data = '';

while(1) {
  if ($data = $com2->input) {
    socket(SOCK, PF_INET, SOCK_STREAM, $proto)  || die "socket: $!";
    connect(SOCK, $paddr) || die "no connecty...\n";
    $buf = $data;
    print SOCK $data;
    close SOCK;
    $data = "0";
  }
}


sock2com.pl:

#!/usr/bin/perl
#use strict;
use IO::Socket;
use Device::SerialPort;

$com2 = Device::SerialPort->start ("/usr/local/work/cuaa0.cfg") or die "Can't start COM1\n";
$sock = new IO::Socket::INET (LocalHost => '123.123.123.123',
                              LocalPort => 10,
                              Proto => 'tcp',
                              Listen => 5,
                              Reuse => 1 );
die "No Socket because: $! \n" unless $sock;

while ($new_sock = $sock->accept()) {
  while (defined ($buf = <$new_sock>)) {
#print "$buf\n";
    $com2->write($buf);
    print $new_sock "Thanx\n";

  }
}
close ($sock);


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/grand-rapids-pm-list/attachments/20050930/d239ab77/attachment.html


More information about the grand-rapids-pm-list mailing list