read/write to a character device

Discussion in 'Perl Misc' started by Stoyan Stoyanov, Feb 15, 2004.

  1. 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
     
    Stoyan Stoyanov, Feb 15, 2004
    #1
    1. Advertising

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

    (Stoyan Stoyanov) wrote in message news:<>...
    > 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
     
    Stoyan Stoyanov, Feb 16, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nishadixit
    Replies:
    5
    Views:
    1,260
    Joe Discar
    Jun 1, 2005
  2. Tito
    Replies:
    1
    Views:
    374
  3. Replies:
    1
    Views:
    431
    santosh
    Nov 27, 2006
  4. AMT2K5
    Replies:
    3
    Views:
    367
    Bob Hairgrove
    Dec 10, 2005
  5. Matt
    Replies:
    0
    Views:
    477
Loading...

Share This Page