poniedziałek, 17 maja 2010

LPT port access with C

I found this useful snippet on another blog.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/types.h>
#include <fcntl.h>

#define BASEPORT 0x378 /* lp1 */

int main()
{
  unsigned char c = 0;
  int n, tem;

   printf("Hit enter to stop\n");

  if (ioperm(BASEPORT, 3, 1)) 
  {
   perror("ioperm");
   exit(1);
  }

  tem = fcntl(0, F_GETFL, 0);
  fcntl (0, F_SETFL, (tem | O_NDELAY));
  
  while (1) {
    n = read(0, &c, 1);
    if (n > 0) break;

     outb(c, BASEPORT);
     usleep(c * 1000);
     c++;
  }

  fcntl(0, F_SETFL, tem);
  outb(0, BASEPORT);

  if (ioperm(BASEPORT, 3, 0)) 
  {
   perror("ioperm");
   exit(1);
  }
  exit(0);
}

Brak komentarzy: