How to send a bit to the parallel port on Linux

It will be nice idea To do a network monitoring  project with LEDs, the fastest way is to use a Perl module called Device::ParallelPort, this module provides an API to all parallel ports. Device::ParallelPort has a number of drivers for multiple operating systems that includes a direct access module for Linux and the Linux ParPort driver. Work almost on  windows and FresBSD.

In python, there are a  module named pyparallel, and in c in userspace we may use Unix Standard and System Input/Output routines.

Here a simple example that make all LEDS connected to parallel port blinking 5 times.

  #include <stdio.h>
  #include <unistd.h>     /* For sleep(), ioperm(), inb() and outb(). */
  #include <sys/io.h>     /* Perhaps asm/io.h on other systems. */

  /* Address of the first parallel port. found in BIOS settings. */
  #define kDATA_REG (0x0378)          /* Base address = data register. */
  #define kSTAT_REG (DATA_REG + 1)    /* Status register. */
  #define kCONT_REG (DATA_REG + 2)    /* Control register. */

  int main()
  {
      int i;

      if (ioperm(kDATA_REG, 1, 1))    /* Get permission to access this port. */
          {
          printf("ioperm(%x) failed.\nYou must be root to execute!\n", kDATA_REG);
          return 1;
          }

      /* Assume port is already in output mode (bit 5 in control register). */
      for (i = 0; i < 5; i++)        /* Let the LED(s) blink. */
          {
          outb(255, kDATA_REG);       /* All 8 datalines high. */
          sleep(1);
          outb(0, kDATA_REG);         /* All 8 datalines low.  */
          sleep(1);
          }

      return 0;
  }
How to send a bit to the parallel port on Linux

2 thoughts on “How to send a bit to the parallel port on Linux

  1. I really miss that parallel port, my computer doesn’t have one🙂
    Reminds me of some routines i wrote under windows for it. ( can be easly ported under linux, if only i could find the source files😀 )
    Here is what it looked like: http://www.youtube.com/watch?v=50oaM-Ol4Gk

    Maybe you could clear the permission when you are done, and why not fix that warning message:
    ali.c:20: warning: don’t make assumptions !

    =D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s