Cheap Linux Hardware - seng a character to printer port...

This is Interesting: Free IT Magazines  
Home > Archive > Cheap Linux Hardware > July 2007 > seng a character to printer port...





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author seng a character to printer port...
google-rambo88

2007-07-22, 7:14 pm

Very strange... even though I include all header files... related open()
function...
( #include <sys/types.h> ,#include <sys/stat.h>,#include <fcntl.h> )
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 <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

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 <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
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;
}





Joe Pfeiffer

2007-07-22, 7:14 pm

"google-rambo88" <rambo88@gmail.com> writes:

> Very strange... even though I include all header files... related open()
> function...
> ( #include <sys/types.h> ,#include <sys/stat.h>,#include <fcntl.h> )
> 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.
Joe Pfeiffer

2007-07-22, 7:14 pm

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.
Jerry Peters

2007-07-22, 7:14 pm

google-rambo88 <rambo88@gmail.com> wrote:
> Very strange... even though I include all header files... related open()
> function...
> ( #include <sys/types.h> ,#include <sys/stat.h>,#include <fcntl.h> )
> 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 <stdio.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> #include <unistd.h>
>
> 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 <stdio.h>
> #include <stdlib.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <fcntl.h>
> #include <unistd.h>
> 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;
> }
>
>
>
>
>

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com