# # # expriment to read from serial port. # Doug Rice # # To get input I found I needed to use: # `stty -F /dev/ttyUSB1 4800 time 50 ` ; # # Use Fcntl on Linux to provide a read() with timeout. # # # use Fcntl; $usb ="ttyUSB0"; my $cnt = 0; print "Setting up terminal ....\n"; print `ls /dev/ttyU*` ; sleep(1); print `stty -F /dev/$usb 4800 -icanon raw -echo -parity cs8 min 0 time 0 ` ; sleep(1); print `stty -F /dev/$usb -a`; $rin = $win = $ein=''; while(1) { print "Opening terminal......."; open( COM, "+ 0 ) { print "\n\ncmd: $op \n\n" ; #syswrite COM ,$op , 1, length( $op ); print COM $op ; $op = "" ; } $timeoutFlag = 1; # we use select to see if any IO is ready. Only block for a short while ($nfound, $timeleft) = select( $rout=$rin, undef , undef , 0.1 ); #print "\nfound: t=" .$timeleft." ". $nfound . "/" . unpack("b32",$rout) . "/" . unpack("b32",$stdin_bit) . "/" . unpack("b32",$com_bit) . "/" . unpack("b8",$eout); #print "\n==============++++++=======================\n"; #print vec( $rout, fileno( STDIN ), 1 ); #print vec( $rout, fileno( COM ), 1 ); if ( vec( $rout, fileno( COM ), 1 ) == 1 ) { $timeoutFlag = 0; # # blocks until a line is received. # we need a non blocking read # http://perldoc.perl.org/functions/readline.html # #readline EXPR #readline # #while ( ! eof($fh) ) { #defined( $_ = <$fh> ) or die "readline failed: $!"; #... #} # #$temp =select(COM); #$/ = undef; #select( $temp); # print "tell COM:".tell( COM ); $ip = ""; $len = sysread COM, $ip, 4000, 0 ; $all = $all.$ip; #read COM ,$ip,100 ; #$ip = ; print $all; } if ( vec( $rout, fileno( STDIN ), 1 ) == 1 ) { $timeoutFlag = 0; #$len = sysread STDIN,$ip,4000 ; $ip = ; $cmd = $ip; if ( $len > 0) { print "\nkey:" . $ip .":" ; if ( $cmd eq "go" ) { print "="; print COM "p07;\r" ; } if ( $cmd =~ /du/ ) { print "="; $op = "d;\n"; } if ( $cmd =~ /poll/ ) { print "="; $op = "p07;\n"; } if ( $cmd =~ /a/ ) { print "="; $op = "w07ff;\n"; } if ( $cmd =~ /b/ ) { print "="; $op = "w0700;\n"; } if ( $cmd =~ /S/ ) { print "="; $op = "S;\n"; } if ( $cmd =~ /s/ ) { print "="; $op = "s;\n"; } } #$op = ""; } if ($timeoutFlag == 0 ) { $all=""; } } close(COM); print "sleep"; sleep(1); }