wrapper about serial port read/write
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 18 Jan 2015 18:30:35 +0000 (19:30 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 18 Jan 2015 18:30:35 +0000 (19:30 +0100)
dialog.pl

index 6d1a29a..6e3445e 100755 (executable)
--- a/dialog.pl
+++ b/dialog.pl
@@ -8,6 +8,7 @@ use Device::SerialPort qw(:STAT);
 my $debug = $ENV{DEBUG} || 0;
 
 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 +18,19 @@ $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 );
+}
+
+# TODO: implement remote serial port
+# remserial -x 2 -p 23000 -s "9600 raw" /dev/ttyUSB0
+
 sub as_hex {
        my @out;
        foreach my $str ( @_ ) {
@@ -62,14 +76,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);