seng a character to printer port... - Hardware

This is a discussion on seng a character to printer port... - Hardware ; Very strange... even though I include all header files... related open() function... ( #include ,#include ,#include ) I met " `O_DIRECT' undeclared (first use in this function) " so I remove it for compiling.. And write() function returned ...always -1.. ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: seng a character to printer port...

  1. seng a character to printer port...

    Very strange... even though I include all header files... related open()
    function...
    ( #include ,#include ,#include )
    I met " `O_DIRECT' undeclared (first use in this function) "


    so I remove it for compiling.. And write() function returned
    ...always -1..


    when I run this short example, I can see following messages by runing
    "dmesg" command.

    parport0: PC-style at 0x378 [PCSPP,EPP]
    parport0: faking semi-colon
    parport0: Printer, Hewlett-Packard HP LaserJet 1100
    lp0: using parport0 (polling).
    lp0: console ready

    Why I can't write character to /dev/lp0?


    I 'd like to know How they are diffrect between /dev/lp0 and /dev.par0 and
    /dev/parport0 ?


    ========
    ================================================== ======
    #include
    #include
    #include
    #include
    #include
    #include

    int main(void)
    {
    int fd;
    int nbytes, total_written = 0;
    char buffer[3] = {0x65, 0x0D , 0x0a };

    fd = open("/dev/lp0",O_RDONLY| O_SYNC ) ;
    if ( fd > 0 )
    {

    nbytes = write(fd, buffer , 3 );
    printf ( "nbytes = ... %d\n", nbytes );
    }
    else

    close(fd);
    return 0;
    }



    Have a day....


    #include
    #include
    #include
    #include
    #include
    #include
    int main(void)
    {
    int ParallelPort;
    char buffer[1] = "A";

    if ((ParallelPort = open("/dev/lp0",O_RDONLY|O_DIRECT|O_SYNC)) >= 0)
    {
    int nbytes, total_written = 0;
    while (bytes_written < sizeof(buffer)
    {
    nbytes = write(ParallelPort, buffer + total_written,
    sizeof(buffer) - total_written);
    total_written += nbytes;
    }

    close(ParallelPort);
    }
    else return EXIT_ERROR;
    return EXIT_SUCCESS;
    }






  2. Re: seng a character to printer port...

    "google-rambo88" writes:

    > Very strange... even though I include all header files... related open()
    > function...
    > ( #include ,#include ,#include )
    > I met " `O_DIRECT' undeclared (first use in this function) "
    >


    1. You aren't writing to a disk, so there's no reason to be
    specifying direct disk access.

    2. Regardless, you need to

    #define _USE_GNU

    before you do your #includes for that symbol to exist.

    3. There's also no real reason to be using O_SYNC...

    > Why I can't write character to /dev/lp0?


    1. You can get the system to tell you why things don't work by
    calling perror() if a system call fails

    2. You opened it RD_ONLY. Why do you expect to be able to write?

    > I 'd like to know How they are diffrect between /dev/lp0 and /dev.par0 and
    > /dev/parport0 ?


    Take a look at the parport.txt and parport-lowlevel.txt files in the
    kernel documentation. The short form is that printers are referred to
    as lp.

    Now... what are you really trying to accomplish? There's really no
    reason anybody needs to send data straight to a printer unless they're
    doing something *really* esoteric. It doesn't seem likely from you
    questions that whatever you're doing really needs that direct access.

  3. Re: seng a character to printer port...

    Almost forgot -- when your open() returns -1, it's telling you the
    open() failed. There's no need to close() a file that wasn't open in
    the first place.

  4. Re: seng a character to printer port...

    google-rambo88 wrote:
    > Very strange... even though I include all header files... related open()
    > function...
    > ( #include ,#include ,#include )
    > I met " `O_DIRECT' undeclared (first use in this function) "
    >
    >
    > so I remove it for compiling.. And write() function returned
    > ...always -1..
    >
    >
    > when I run this short example, I can see following messages by runing
    > "dmesg" command.
    >
    > parport0: PC-style at 0x378 [PCSPP,EPP]
    > parport0: faking semi-colon
    > parport0: Printer, Hewlett-Packard HP LaserJet 1100
    > lp0: using parport0 (polling).
    > lp0: console ready
    >
    > Why I can't write character to /dev/lp0?
    >
    >
    > I 'd like to know How they are diffrect between /dev/lp0 and /dev.par0 and
    > /dev/parport0 ?
    >
    >
    > ========
    > ================================================== ======
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    >
    > int main(void)
    > {
    > int fd;
    > int nbytes, total_written = 0;
    > char buffer[3] = {0x65, 0x0D , 0x0a };
    >
    > fd = open("/dev/lp0",O_RDONLY| O_SYNC ) ;

    =========================^^^^^^

    Why are you opening it for read if you're planning on _writing_ to it?

    Jerry

    > if ( fd > 0 )
    > {
    >
    > nbytes = write(fd, buffer , 3 );
    > printf ( "nbytes = ... %d\n", nbytes );
    > }
    > else
    >
    > close(fd);
    > return 0;
    > }
    >
    >
    >
    > Have a day....
    >
    >
    > #include
    > #include
    > #include
    > #include
    > #include
    > #include
    > int main(void)
    > {
    > int ParallelPort;
    > char buffer[1] = "A";
    >
    > if ((ParallelPort = open("/dev/lp0",O_RDONLY|O_DIRECT|O_SYNC)) >= 0)
    > {
    > int nbytes, total_written = 0;
    > while (bytes_written < sizeof(buffer)
    > {
    > nbytes = write(ParallelPort, buffer + total_written,
    > sizeof(buffer) - total_written);
    > total_written += nbytes;
    > }
    >
    > close(ParallelPort);
    > }
    > else return EXIT_ERROR;
    > return EXIT_SUCCESS;
    > }
    >
    >
    >
    >
    >


+ Reply to Thread