extract chat with parallel port
[Printer-EVOLIS.git] / lib / Printer / EVOLIS / Parallel.pm
1 package Printer::EVOLIS::Parallel;
2
3 use warnings;
4 use strict;
5
6 use POSIX;
7 use Data::Dump qw(dump);
8
9 my $debug = 0;
10
11 =head1 NAME
12
13 Printer::EVOLIS::Parallel - chat with parallel port printer
14
15 =head1 METHODS
16
17 =head2 new
18
19   my $p = Printer::EVOLIS::Parallel->new( '/dev/usb/lp0' );
20
21 =cut
22
23 sub new {
24         my ( $class, $port ) = @_;
25         my $self = { port => $port };
26         bless $self, $class;
27         return $self;
28 }
29
30 =head2 command
31
32   my $response = $p->command( 'Rsn' );
33
34 =cut
35
36 sub command {
37         my ( $self, $send ) = @_;
38
39         my $port = $self->{port};
40         die "no port $port" unless -e $port;
41
42         my $parallel;
43
44         # XXX we need to reopen parallel port for each command
45         sysopen( $parallel, $port, O_RDWR | O_EXCL) || die "$port: $!";
46
47         foreach my $byte ( split(//,$send) ) {
48                 warn "#>> ",dump($byte),$/ if $debug;
49                 syswrite $parallel, $byte, 1;
50         }
51
52         close($parallel);
53         # XXX and between send and receive
54         sysopen( $parallel, $port, O_RDWR | O_EXCL) || die "$port: $!";
55
56         my $response;
57         while ( ! sysread $parallel, $response, 1 ) { sleep 0.1 }; # XXX wait for 1st char
58         my $byte;
59         while( sysread $parallel, $byte, 1 ) {
60                 warn "#<< ",dump($byte),$/ if $debug;
61                 last if $byte eq "\x00";
62                 $response .= $byte;
63         }
64         close($parallel);
65
66         return $response;
67 }
68
69 1;