Saturday, July 18, 2009

/dev/port

This is something that I had known for years but never had a chance to try out. Recently I had to write a program to control parallel ports. I decided to do with iopl and in/outb. However, they did not work for me. Then I tried this. Just open the file, seek till where your parallel port is, write the data! It worked like a charm. Below is a small code snippet to do it.


#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    int fd1=-1;
    unsigned char data=0xaa;

    fd1=open("/dev/port",O_RDWR|O_NDELAY);
    if ( fd1 < 0 ) return fd1;

    lseek(fd1,(0x378+0x00),SEEK_SET);
    write(fd1,&data,1);


    return 0;
}



You will have to change 0x378 to your parallel port base address. Also data is at offset 0, status is at offset 1 and control is at offset 2.

No comments: