X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=dialog.pl;h=d868ed93285e606b27e89bf2e75b46d323b4e2dd;hb=HEAD;hp=6d1a29a3833b6861a8977fb8fccdc6e09d633b0a;hpb=75e24000c67d1974d540ec88feb6c5b883e77f52;p=vrDialog diff --git a/dialog.pl b/dialog.pl index 6d1a29a..d868ed9 100755 --- a/dialog.pl +++ b/dialog.pl @@ -3,11 +3,14 @@ use warnings; use strict; use Data::Dump qw(dump); -use Device::SerialPort qw(:STAT); - my $debug = $ENV{DEBUG} || 0; +=for serial_port + +use Device::SerialPort qw(:STAT); + my $port = Device::SerialPort->new('/dev/ttyUSB0'); +die "can't open serial port" unless $port; $port->baudrate(9600); $port->databits(8); $port->parity('none'); @@ -17,6 +20,52 @@ $port->handshake('none'); $port->read_char_time(500); $port->read_const_time(1000); +sub read_serial { + my $len = shift; + return $port->read( $len ); +} + +sub write_serial { + my $bytes = shift; + return $port->write( $bytes ); +} + +=cut + +# TODO: implement remote serial port +# remserial -x 2 -p 23000 -s "9600 raw" /dev/ttyUSB0 + +use IO::Socket::INET; + +my $sock = IO::Socket::INET->new( + PeerAddr => '192.168.1.148', + PeerPort => '23000', + Proto => 'tcp' +); +die "can't connect" unless $sock; + +sub read_serial { + my $len = shift; + my $buffer; + eval { + local $SIG{ALRM} = sub { die "read timeout" }; + alarm 3; + + warn "## read_serial $len" if $debug; + $sock->read( $buffer, $len ); + warn "## read $len ",as_hex($buffer) if $debug; + }; + alarm 0; + warn "ERROR: $@" if $@; + return $buffer; +} + +sub write_serial { + my $bytes = shift; + return $sock->print( $bytes ); +} + + sub as_hex { my @out; foreach my $str ( @_ ) { @@ -62,14 +111,14 @@ sub v_bytes { retry: warn ">> ",as_hex( $bytes ); crc( $bytes ); - $port->write( $bytes ); + write_serial( $bytes ); - my $data = $port->read(1); + my $data = read_serial(1); warn "<< len: ",ord($data) if $debug; goto retry if ord($data) == 0; - $data .= $port->read(ord($data) - 1); + $data .= read_serial(ord($data) - 1); crc($data); warn "<< ",as_hex($data);