#!/usr/bin/perl # # A simple TCP daemon written in Perl. The script listens for connections on TCP port 7070, # and passes text to and from the serial port. This allows multiple local or remote programs # to interface with the I/O controller. # use IO::Select; use IO::File; use IO::Socket; sub open_port { my($portdevice, $portspeed) = @_; system("/bin/stty -F $portdevice speed $portspeed raw > /dev/null 2>&1"); my $porthandle = new IO::File("+<$portdevice"); if ($porthandle) { $porthandle->autoflush(1); } return $porthandle; } sub command { my @ready; my $s, $buf; my($porthandle, $command) = @_; my $read_set = new IO::Select(); $read_set->add($porthandle); print $porthandle $command; if (@ready = $read_set->can_read(2)) { foreach $s (@ready) { $buf = <$s>; return $buf; } } return 0; } $port = open_port("/dev/ttyUSB2", "2400"); $socket = new IO::Socket::INET (LocalHost => '127.0.0.1', LocalPort => '7070', Proto => 'tcp', Listen => 16, Reuse => 1,); die "Could not create socket: $!\n" unless $socket; $sock_set = new IO::Select($socket); while (1) { @rh_set = $sock_set->can_read(); foreach $rh (@rh_set) { if ($rh == $socket) { $ns = $rh->accept(); $ns->autoflush(1); $sock_set->add($ns); } else { $buf = <$rh>; if ($buf) { $out = command($port, $buf); print $rh $out; } else { $sock_set->remove($rh); close($rh); } } } }