Friday, July 15, 2011

Parallel port programming in Windows XP or Windows Vista

Hello friends, this is very much exciting to control the parallel port, because using parallel port we can control many types of robot from small to very large, there is no limitation we can think beyond the expectation, all the programming can be done on today's fastest computer or laptop and we can place laptop or a CPU in any robot if it is capable of and we can provide vision by camera using vision processing, so lots of ideas can come in picture, here I am giving one simple example that how to control parallel port in windows.

We have two solutions that I know to control parallel port in windows operating system

  1. First method

    Using Turbo C for coding and in a C program include dos.h header file as shown in program below.
  • This program is for blinking a led at all the data pins of parallel port
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define PORTID 0x378

void main()
{
long int i;
while(1){
   outportb(PORTID,0xFF); //0xFF will enable all the eight data pin of parallel port
   for(i=0;i<999999;i++); // for delay
   outportb(PORTID,ox00); //0x00 will disable all the data pins of port
   for(i=0;i<999999;i++); // for delay
}
}

  • Similarly we can take input from  a parallel port  with the help of inportb(PORTID) function.
  • First declare an integer variable
         For Example:
                             int a;
                             a=inportb(PORTID);
  • Using above statement, status of data pins of parallel port will be stored in variable a, and then we can use this variable for comparison or any other use.
  1. Second method for controlling the parallel port:

  • If you want to integrate your parallel port controlling part in vision processing then you need a good IDE for development like Dev-C++, things those are difficult to implement in Turbo C can be easily done in IDE like Dev-C++. Using inpout32.dll library we can achieve parallel port controlling in Dev-C++
  • I have done this in Dev-C++ while I was using OpenCV2.2 for real time vision processing, it works very well
  • You can download inpout32.dll file
  • Unzip the downloaded folder 
  • Then go to binaries->DLL and copy inpout32.dll file
  • Paste it in 'C:/window/system32/' where all the dll files has been placed
  • After pasting this file now you can use this library in your C program in Dev-C++ IDE
  • Example is shown below:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define p 0x378

typedef short _stdcall (*inpfuncPtr)(short portaddr);
typedef void _stdcall (*oupfuncPtr)(short portaddr, short datum);

int main(void)
{
HINSTANCE hLib;
inpfuncPtr in;
oupfuncPtr out;
long int i;
char c;
hLib = LoadLibrary("inpout32.dll");
if (hLib == NULL) {
printf("LoadLibrary Failed.\n");
getch();
return -1;
}
in = (inpfuncPtr) GetProcAddress(hLib, "Inp32");
if (in == NULL) {
printf("GetProcAddress for Inp32 Failed.\n");
getch();
return -1;
}
out = (oupfuncPtr) GetProcAddress(hLib, "Out32");
if (out == NULL) {
printf("GetProcAddress for Oup32 Failed.\n");
getch();
return -1;
}
out(p,0x00);
printf("OFF\n");
while(1){

while(kbhit()){
c=getch();
if(c=='p'){
printf("ON\n");
out(p,0xFF);
}
else if(c=='o'){
printf("OFF\n");

out(p,0x00);
}

}
FreeLibrary(hLib);
out(p,0x00);
return 0;
}

To know how to control Parallel Port in Linux, see my blog: Parallel port programming in Linux

..........Enjoy...:)