extract chat with parallel port
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 16 Aug 2010 11:55:29 +0000 (13:55 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 16 Aug 2010 11:55:29 +0000 (13:55 +0200)
lib/Printer/EVOLIS/Parallel.pm [new file with mode: 0644]

diff --git a/lib/Printer/EVOLIS/Parallel.pm b/lib/Printer/EVOLIS/Parallel.pm
new file mode 100644 (file)
index 0000000..7a71114
--- /dev/null
@@ -0,0 +1,69 @@
+package Printer::EVOLIS::Parallel;
+
+use warnings;
+use strict;
+
+use POSIX;
+use Data::Dump qw(dump);
+
+my $debug = 0;
+
+=head1 NAME
+
+Printer::EVOLIS::Parallel - chat with parallel port printer
+
+=head1 METHODS
+
+=head2 new
+
+  my $p = Printer::EVOLIS::Parallel->new( '/dev/usb/lp0' );
+
+=cut
+
+sub new {
+       my ( $class, $port ) = @_;
+       my $self = { port => $port };
+       bless $self, $class;
+       return $self;
+}
+
+=head2 command
+
+  my $response = $p->command( 'Rsn' );
+
+=cut
+
+sub command {
+       my ( $self, $send ) = @_;
+
+       my $port = $self->{port};
+       die "no port $port" unless -e $port;
+
+       my $parallel;
+
+       # XXX we need to reopen parallel port for each command
+       sysopen( $parallel, $port, O_RDWR | O_EXCL) || die "$port: $!";
+
+       foreach my $byte ( split(//,$send) ) {
+               warn "#>> ",dump($byte),$/ if $debug;
+               syswrite $parallel, $byte, 1;
+       }
+
+       close($parallel);
+       # XXX and between send and receive
+       sysopen( $parallel, $port, O_RDWR | O_EXCL) || die "$port: $!";
+
+       my $response;
+       while ( ! sysread $parallel, $response, 1 ) { sleep 0.1 }; # XXX wait for 1st char
+       my $byte;
+       while( sysread $parallel, $byte, 1 ) {
+               warn "#<< ",dump($byte),$/ if $debug;
+               last if $byte eq "\x00";
+               $response .= $byte;
+       }
+       close($parallel);
+
+       return $response;
+}
+
+1;