Wednesday, May 11, 2011

Parallel port interfacing

Parallel port interfacing



Introduction
           Parallel port is a simple and inexpensive tool for building computer controlled devices and projects. The simplicity and ease of programming makes parallel port popular in electronics hobbyist world. The parallel port is often used in Computer controlled robots, Atmel/PIC programmers, home automation, ...etc... Here a simple tutorial on parallel port interfacing and programming with some examples.

Hardware
The pin outs of DB25 connector is shown in the picture below


 
       
Sample program in VC++
           Writing a parallel port interfacing program in VC++ is very easy. Here is the steps to write your first parallel port interfacing application in VC++.

           Start VC++ IDE , Select 'New' from File menu.Then select “Win32 Console Application” from “Projects” tab(picture-3). enter project name as “partest1” , then click OK button.


Now you can see a dialog box with caption “Win32 Console Application - step 1 of 1” (picture-4).

Select “a simple Application” and click Finish. Now open example1.cpp from “fileview” and replace the existing code with the code given below.

#include "stdafx.h"
#include "conio.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
 short data;
 if(argc<2)
 {
  printf("Usage\n\n");
  printf("partest1.exe ,,\n\n\n");
  return 0;
 }

 if(!strcmp(argv[1],"read"))
 {
  data = _inp(atoi(argv[2]));
  printf("Data read from parallel port is  ");
  printf("%d\n\n\n\n",data);
 }

 if(!strcmp(argv[1],"write"))
 {
  _outp(atoi(argv[2]),atoi(argv[3]));
  printf("Data written to parallel port is  ");
  printf("%s\n\n\n\n\n",argv[3]);
 }
 return 0;
}


Build the project and copy partest1.exe to "c:\".
 
How to Test The Program ?
           Connect The assembled hardware shown above to your PC's parallel port. Open DOS command window Move to "C:\" and type "partest1 write 888 255" and press enter. If everything is correct , LED1 to LED8 in the hardware will glow. You may be doubtful about the command line parameters passed to the program. Here 888(0x378) is the address of the parallel port data register and 255 is the data to be written to parallel port data register. if you enter "partest1 read 888" to command line , the program will read parallel port data register and display it. This will blindly read the contents of parallel port data register , but not the data present on data lines. To read the data from the data lines , we will have to enable the bidirectional data transfer first. To enable Bidirectional data transfer just set the "Bidirectional" bit (bit 5) in control register. This is done by writing 32 to control register. The command "partest1 write 890 32" will do this. After entering this command you can read the status of switches in the hardware using the command "partest1 read 888"
NOTE: This sample program will not work on Windows NT/2000 or XP if you run the program on these machines , it will show an error. use new Inpout32.dll on NT/2000/XP machines

No comments:

Post a Comment