Wednesday, December 23, 2009

Parallel port programming using C/C++

Parallel port programming is easy with C or C++. Even other languages integrate C/C++ codes to their codes in order to program parallel ports. If you are a beginner in parallel port programming and know nothing about it, just go through this intro page. Nevertheless the efficiency of code, the code may be different for different platforms and need to be compiled with some change in the code.Here we see how to program parallel port(printer port or LPT) in both GNU/Linux and MS Windows platform.
Parallel port programming in GNU/Linux
I am not intended to make a wide description. I am directly give an example and this is what you want. If you are experienced the taste of C, you can easily catch what the program code mean.(We use LEDs to check whether our program run. You can see a simple circuit in the intro page). Connect the circuit described in intro page. to the parallel port properly. Then make new file parport.c and edit the file as shown:

#include stdio.h
#include stdlib.h
#include asm/io.h
#include unistd.h

#define base 0x378 /* printer port base address */
#define value 255 /* numeric value to send to printer port */

main(int argc, char **argv)
{
if (ioperm(base,1,1))
fprintf(stderr, "Couldn't get the port at %x\n", base), exit(1);

outb(value, base);
}



The above program is copied from epanorama. You should compile the program as su (super user) since ioperm() function needs root access. But in my system the above program didn't work. Many of my friends got error while compiling. The solution is to change the 4th line
#include asm/io.h
to
#include sys/io.h

You change the line and recompile only if you get a linking error. Sometimes you get an error like "Couldn't get the port at 378". But this is not a problem due to the port address. You probably may not be su.

Parallel port programming in MS windows
Converse the subtitle suggests, this program is not working in the windows environment, but in the MS DOS environment. I think you have basic knowledge of how to compile a C program.

#include stdio.h
#include conio.h
#include dos.h

/********************************************/
/*This program set the parallel port outputs*/
/********************************************/

void main (void)
{
clrscr(); /* clear screen */
outportb(0x378,0xff); /* output the data to parallel port */
getch(); /* wait for keypress before exiting */
}


The above program is also from epanorama. If you try to compare those programs, you can see a very little difference between those codes. Parallel port is a hardware and surely it is depended upon the OS you are using. So you can't write a cross-platform code for the parallel/printer port control. Please note that you can read inputs as well as write as output however, the example only shows the output control. Sometimes your port address may be different, however in most cases it will be 0x378. If it fails try 0x278 and 0x3BC. Read more here.
You can check your program by simply inserting an LED connected in serial with 1K ohm resistor.

Read my post on parallel port control on python

No comments: