Parallel Port Programming With C/C++
Parallel port programming is easy with C or C++. This article presents a simple way to interface electronic hardware with normal PCs. The algorithm will allow us to talk with the hardware via LPT port. The keyboard signals (or keystrokes) will be captured and propagated to the parallel port.
Parallel ports can be very useful in connect our own designed circuit but cautions should be taken as this port can get damaged very easily.
Parallel port is a 25 pin female connector, normally used to connect printer to the PC.. There are 3 sub categories of parallel port pins.
a. Data registers : Responsible for data input/output.
b. Status registers : Input only port, responsible for notifying the current status
c. Control registers : used to send control signals.
The anode needs to be connected to any of the data registers (pin 2 to pin 9)and the cathode pin of LED should be connected to a 1k ohm resistor and can be grounded using any of the pins (18 to 25).
Windows NT and clones have a strict control over I/O ports. Windows NT/2000/XP raises an exception because they run on protected mode. Operating Systems on Intel’s 80286 onwards starts with real mode and then switched to protected mode or protected virtual address mode. In protected mode there are four rings or privilege modes ranging from 0 to 3 where 0 being most privilege and 3, the least. User mode programs will run in privilege level 3, while device drivers and the kernel will run in privilege level 0 or ring 0. The use of rings monitors the system software to restrict tasks from accessing data or executing privileged instructions. Here, the protected mode will restrict us to talk directly with hardware, so the library is acting as an interface or similar to a device driver.
There are two solutions to solving the problem of I/O access under Windows NT or its clones. The first solution is to write a device driver which runs in ring 0 (I/O privilege level 0) to access your I/O ports but an easier approach will be to use a library( inpout32.lib ) which can be downloaded free from http://logix4u.net with source code.
I have used MFC for the purpose. The idea is to capture the arrow keys event both on press and release and accordingly propagate the result to the data registers.
While using MFC with dialog based, we may require to send request to load the accelerators so that we can capture arrow keys event. Please add the following instruction inside OnInitDialog().
m_hAccel = LoadAccelerators(AfxGetResourceHandle(),
Define the parallel port data register as :
#define DATA 0x378
Now override the virtual function PreTranslateMessage for arrow keys event capture.
BOOL CKeyMapperDlg::PreTranslateMessage(MSG* pMsg)
//static text feild to update the key press status
int iDataOut = 0;
iDataOut = 1;
iDataOut = 2;
iDataOut = 4;
iDataOut = 8;
//Library function to write to the data port
Connect the circuit and pin no. 2 to pin no. 5 will glow on arrow key press and off on release.
Why Choose Us
Wide Portfolio of Services
Domain Intensive Value Proposition
Technological Excellence across platforms
Avail competitive pricing (Reduce 70% cost, ensure 100% quality)
Get 4 weeks complimentary Risk-Free Trial*
We Build Innovative Applications: Browse through our Intellectual Property