read/write to a character device

S

Stoyan Stoyanov

I'd like to send some binary data directly to my USB printer and get
the response. The sending of the data seems to pass OK but I have
problems getting the response. What happens is that randomly I get: no
response at all, the correct response, or the correct response twice.
I tried several different ways of opening filehandles to the device
but in all cases I get the same results. I must be missing something.

Here is one of the script variants that I tried:

--------------------------
#!/usr/bin/perl -w
use strict;
use POSIX;

my $DEVICE = '/dev/usb/lp0';
my $buffer;
my $response;
my $byte;

my @line = split(/\s+/,'00 00 00 1B 01 40 45 4A 4C 20 31 32 38 34 2E
34 0A 40 45 4A 4C 0A 40 45 4A 4C 0A');

# converting the data
foreach (@line) {
$buffer .= sprintf ("%c", hex $_);
}

# debug: dumping to a file whatever is to be sent to the printer
open AA, "> buffer.txt";
print AA $buffer;
close AA;

sysopen(FH, $DEVICE, O_RDWR | O_EXCL) or die $!;
my $ofh = select(FH); $| = 1; select($ofh);

# sending to printer
syswrite (FH, $buffer, 27) or die $!;

# reading the response
while (sysread (FH, $byte, 1)==1) {
$response .= $byte;
}

# debug: dumping the response to a file
open AA, "> response.txt";
print AA $response;
close AA;

close (FH);
 
S

Stoyan Stoyanov

I figured it out. Just had to close/open the character device between
reading/writing attempts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top