Re: [PARPORT] Example Code

From: Peter Ryser (
Date: Thu Mar 29 2001 - 19:17:59 EST

  • Next message: Richard Polton: "Re: [PARPORT] Freecom CD/RW problems"

    Hi Andrew,

    following is a piece of code that might help you with what you are
    planning to do. Save it as lpttest.c and compile with:
    gcc -o lpttest lpttest.c

    Using ioctl's to access the parallel port is not very efficient since
    for every single character you have to go from user space to kernel
    space and back. A more efficient method would be to use ioperm(), inb()
    and outb(). But then you have to run the program as root or setuid root.

    I am wondering whether through ppdev it is possible to configure a
    parallel port into "stupid mode" that allows fast write access to the
    data port without waiting for any handshake signals and timeouts. I
    imagine something like:

    write(fd, buffer, 100); /* no handshaking, no timeouts, just write
    data as fast as possible to the data port with only one context switch */

    Does anybody have a solution for that?

    - Peter

    #include <sys/time.h>
    #include <sys/types.h>
    #include <sys/ioctl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <getopt.h>

    #include "parport.h"
    #include "ppdev.h"

    int main (int argc, char *argv[])
    int fd;
    char hbuffer[200];
    int res;

    fd = open ("/dev/parport0", O_RDWR);

    if (fd == -1)
    perror ("/dev/parport0");
    return fd;

    if (ioctl (fd, PPCLAIM))
    fprintf (stderr, "deviceid: cannot claim port\n");
    close (fd);
    return 1;

      /* write character to data port */
      hbuffer[0] = 0x40;
      res=ioctl(fd, PPWDATA,hbuffer);

      /* read character from status port */
      printf("%d: %x\n", res, hbuffer[0]);

      ioctl (fd, PPRELEASE);

      close (fd);
      return 0;

    -- To unsubscribe, send mail to: --
    -- with the single word "unsubscribe" in the body of the message. --

    This archive was generated by hypermail 2b29 : Thu Mar 29 2001 - 19:33:52 EST