Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > read/write to a character device

Reply
Thread Tools

read/write to a character device

 
 
Stoyan Stoyanov
Guest
Posts: n/a
 
      02-15-2004
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);
--------------------

Thanks!
Stoyan
 
Reply With Quote
 
 
 
 
Stoyan Stoyanov
Guest
Posts: n/a
 
      02-16-2004
I figured it out. Just had to close/open the character device between
reading/writing attempts.

http://www.velocityreviews.com/forums/(E-Mail Removed) (Stoyan Stoyanov) wrote in message news:<(E-Mail Removed). com>...
> 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);
> --------------------
>
> Thanks!
> Stoyan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cisco 1750 Router Cisco QoS Device Manager Cisco VPN Device Manager Rene Kuhn Cisco 0 12-28-2005 08:45 PM
877W - cannot talk wireless device to wireless device Nick Ersdown Cisco 7 10-31-2005 04:20 PM
Will application J2ME MIDP 2.0 based of one device run another J2ME MIDP 2.0 device? nishadixit Java 5 06-01-2005 05:40 AM
Determine the device is a router or switch given the Device IP kiranreddyd@gmail.com Cisco 14 12-26-2004 04:11 PM
802.11g router / 1 x 802.11b device / 1 x 802.11g device Oli Wireless Networking 3 09-27-2004 11:56 PM



Advertisments