HOW TO PROGRAM THE PARALLEL PORT ON A COMPUTER USING C/C++

New versions of windows operating system does not support direct access to I/O ports.
In order to communicate with the parallel port of your computer in windows operating system you will need a driver called inpout32.dll which you can dowload from here http://logix4u.net/parallel-port/inpout32_source_and_bins.zip

Extract the zip file , open the binaries directory , copy the inpout32.dll and add it to you projects directory.
you can also save it at "%systemroot%\system32\drivers"


The address of the parallel data port is 
0x378-0x37F for LPT1
0X278-0X27F for LPT2
Parallel port has 8 data input

fig.1

DATA PORT PIN NO.
SIGNAL NAME
BYTE TO ACTIVATE PIN
2
D0
0x01
3
D1
0x02
4
D2
0x04
5
D3
0x08
6
D4
0x10
7
D5
0x20
8
D6
0x40
9
D7
0x80
18-25
Ground(GND)

All the data port are latching which means it keeps it state once it is activated.
To send one byte to the parallel port  
 send 0xFF to 0x378
1 bit will be on each data port which means all the data port will have 5V across them
To send 1 bit to Data port 
If you send 0x01 to data port 0x378 will send binary 00000001(5V) to pin 1
and so on as shown on the table.

Now to start create a new cpp file  and note the inpout32.dll should be in the same folder or you can place in this directory %systemroot%\system32\drivers.

This is how your code should start

#include <iostream>
#include <windows.h>
using namespace std;
typedef short _stdcall (*inpfuncPtr)(short portaddr);
typedef void _stdcall (*oupfuncPtr)(short portaddr, short datum);
int main()
{
HINSTANCE hLib;
inpfuncPtr in;
oupfuncPtr out;
hLib = LoadLibrary("inpout32.dll"); //load the inpout32.dll
if (hLib == NULL) {
cout<<"LoadLibrary Failed.\n";
cin.get();
return -1;
}
in = (inpfuncPtr) GetProcAddress(hLib, "Inp32"); //define in function
if (in == NULL) {
cout<<"GetProcAddress for Inp32 Failed.\n";
cin.get();
return -1;
}
out = (oupfuncPtr) GetProcAddress(hLib, "Out32"); //define out function
if (out == NULL) {
cout<<"GetProcAddress for Oup32 Failed.\n";
cin.get();
return -1;
/* place your codes here
use in(address) to read data from parallel port
use out(address,data) to send data from the parallel port
*/
}
view raw main.c hosted with ❤ by GitHub

To send 1 bit to data port pin 1( D0 ) out(0x378, 0x01) and the rest can be found on the table 

Here is a simple example to demonstrate how to switch the data port of the paraport on and off .
setup a circuit as shown below
fig.2



And the source code is here:
#include <iostream>
#include <windows.h>
using namespace std;
typedef short _stdcall (*inpfuncPtr)(short portaddr);
typedef void _stdcall (*oupfuncPtr)(short portaddr, short datum);
int main()
{
HINSTANCE hLib;
inpfuncPtr in;
oupfuncPtr out;
hLib = LoadLibrary("inpout32.dll");
if (hLib == NULL) {
cout<<"LoadLibrary Failed.\n";
cin.get();
return -1;
}
in = (inpfuncPtr) GetProcAddress(hLib, "Inp32");
if (in == NULL) {
cout<<"GetProcAddress for Inp32 Failed.\n";
cin.get();
return -1;
}
out = (oupfuncPtr) GetProcAddress(hLib, "Out32");
if (out == NULL) {
cout<<"GetProcAddress for Oup32 Failed.\n";
cin.get();
return -1;
}
int x;
do
{
cout<<"\n select(0 to quit) \n";
cout<<"1. data port ON \n";
cout<<"2. data port OFF\n";
cin>>x;
if (x==1)
{
out(0x378,0xFF); //turn all data port high
cout<<"port is ON\n";
}
else if(x==2)
{
out(0x378,0); //send 0 to data port pin
cout<<"port is OFF\n";
}
}
while (x != 0);
return 0;
}
view raw main.c hosted with ❤ by GitHub

Output display below
fig. 3


With this you can control many appliance and device with the help of a relay.
 The next blog is about How to use the Parallel Port to control a 240 V light bulb

HOW TO PROGRAM THE PARALLEL PORT ON A COMPUTER USING C/C++ HOW TO PROGRAM THE PARALLEL PORT ON A COMPUTER USING C/C++ Reviewed by Zakaria Mohammed on July 29, 2015 Rating: 5

Post Comments

1 comment :

Powered by Blogger.