Friday, November 21, 2008

Parallel Port Programming

Parallel port is very common and popular tool which widely using in computer. Most of time we use this as our printer port. The main cause that makes it popular is 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...
In versions of Windows that did not use the Windows NT kernel (as well as MS-DOS and some other operating systems), programs could access the parallel port with simple outportb() and inportb() subroutine commands. Now I'm going to show a very simple Parallel port accessing program which will control LED On/Off.
First have a look in Parallel port pin configration. In 25 pin parallel port have following pin functions.


Port-----------Pin No.------------Mode
Data-----------2-9----------------Output (O/P)
Status---------10-13, 15----------Input (I/P)
Control--------1, 14, 16, 17--------Output (O/P)
GND-----------18-25--------------Single Ground

I have written a simple program in C++ , where port address is defined 0x378 as global variable. Now a array data is use where some data such 0,1,2......etc those are in decimal value. Next I just make decimal to binary conversion which just perform for display . Here I'm using 8 LED and my i/p will be 0,1,2,3......etc , so if press 1 then 1st LED will be on rest of will remain off and m program will give o/p 0-0-0-0-0-0-0-1 so 1 indicated LED is on and 0 for off. thats all. :)


A simple program which I wrote in C++


That Program will give such o/p





LED On (In this case I/P was 1)

Now this same parallel port program which I shown in C++ , if you try to run in XP OS then we don't get any result although that program is ok. Because not only in XP, also Windows NT/2000/Xp we cannot control parallel port by directly accessing port addresses. This is because the Windows NT kernel does not allow usermode programs to have direct access to I/O ports. we have to communicate with a driver that will access port addresses.

Those who are fimilar with programming may know the term API(Application Programming Interface) which is a set of functions / protocols contain a description of the way one piece of software asks another program to perform a service . So we can access ports by using the drivers included with Windows. So we need a device driver . Such Driver should already installed in the case of the standard PC parallel port, now the next question is how do you access it. Well I'm open the device like a file using a name such as "LPT1" (for the first parallel port device) using the likes of the Win32 API function CreateFile. Then read and write to it using the likes of ReadFile and WriteFile (or ReadFileEx and WriteFileEx) like usual programming language . For more information on these topics look in the MSDN library (online at http://www.msdn.microsoft.com/). There is a section on Device I/O in the Windows Development / Windows Base Services area. :)

1 comment:

Anonymous said...

Hi !!
Very Nice to see. I also tried run such type of LED control program on my windows Xp but im fail ..
thanxxx again.