a8b38906ef3e661e926d4d5c2d7a24ce21309436
[Printer-EVOLIS.git] / scripts / evolis-command.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use POSIX;
7 use Data::Dump qw(dump);
8 use Time::HiRes;
9 use Getopt::Long;
10
11 my $port = '/dev/usb/lp0';
12 my $debug = 0;
13
14 GetOptions(
15         'debug+' => \$debug,
16         'port=s' => \$port,
17 ) || die $!;
18
19 warn "# port $port debug $debug\n";
20
21 my $parallel;
22
23 $|=1;
24 print "command> ";
25 while(<STDIN>) {
26         chomp;
27
28         my $send = "\e$_\r";
29
30         # XXX we need to reopen parallel port for each command
31         sysopen( $parallel, $port, O_RDWR | O_EXCL) || die "$port: $!";
32
33         foreach my $byte ( split(//,$send) ) {
34                 warn "#>> ",dump($byte),$/ if $debug;
35                 syswrite $parallel, $byte, 1;
36         }
37
38         close($parallel);
39         # XXX and between send and receive
40         sysopen( $parallel, $port, O_RDWR | O_EXCL) || die "$port: $!";
41
42         my $response;
43         while ( ! sysread $parallel, $response, 1 ) { sleep 0.1 }; # XXX wait for 1st char
44         my $byte;
45         while( sysread $parallel, $byte, 1 ) {
46                 warn "#<< ",dump($byte),$/ if $debug;
47                 last if $byte eq "\x00";
48                 $response .= $byte;
49         }
50         close($parallel);
51
52         print "<answer ",dump($response),"\ncommand> ";
53
54 }
55