What is a parallel port?
Signals with prefix 'n' are active low. Normally these pins will have low value. When it needs to send some indication, it will become high. For example, normally nStrobe will be high, when the data is placed in the port, computer makes that pin low.
Normally, data, control and status registers will have following addresses. We need these addresses in programming later.
By default, data port is output port. To enable the bidirectional property of the port, we need to set the bit 5 of control register.
To know the details of parallel ports available in your computer, follow this procedure:
1. Right click on My Computer, go to "Properties".
2. Select the tab Hardware, Click Device manager.
3. You will get a tree structure of devices; in that Expand "Ports (Com1 &LPT)".
4. Double Click on the ECP Printer Port (LPT1) or any other LPT port if available.
5. You will get details of LPT port. Make sure that "Use this Port (enable)" is selected.
6. Select tab recourses. In that you will get the address range of port.
Parallel Port Interface Projects
1. Running LED lights.
2. Controlling a remote Car using the computer.
3. Controlling home appliances such as lights, fan, TV etc.
4. Any other gadget that you want to control.
Programming Parallel Port in C#
Till today we use to control the Parallel port using the “Turbo C”, which is easy to code and at the same time powerful to control the parallel port. C lacks the fancy user interface that most of the users are attracted towards. Even in this article we will be using the Console application, you can use the code and create a Windows Forms application for a fancy UI.
I have used an assembly inpout32.dll for interfacing with parallel port. This assembly provides easy access to the interfacing API’s. You can download the assembly from here.
If you are coding in Turbo C, you will end up using the functions outportb() and inportb() from the Stdio.h and IO.h library files.
// ReferencesThe Output function used in the above class will send the data out to the required port. The first parameter i.e. address, indicates the address of the port on which the data needs to be sent. The second parameter i.e. value, indicated the value (as integer) that needs to be sent to the port. In actual the value will be sent as binary on the port and hence an equivalent integer value needs to be computed before invoking this method.
/// Class to access the Port API's from the inpout32.dll.
public class PortAccessAPI
/// This method will be used to send the data out to the parallel port.
/// <param name="adress">Address of the port to which the data needs to be sent.</param>
/// <param name="value">Data that need to send out.</param>
public static extern void Output(int address, int value);
/// This method will be used to receive any data from the parallel port.
/// <param name="address">Address of the port from which the data should be received.</param>
/// <returns>Returns Integer read from the given port.</returns>
[DllImport("inpout32.dll", EntryPoint = "Inp32")]
public static extern int Input(int address);
Now in the Program.cs which is the entry point for the Console application, the following code will go in the Main method:
int address = 888;
int value = 24;
Here the address 888 as int is actually 0x378 as Hex, which is the data port of the parallel port.
To reset the data that is sent on the data port, you need to invoke the Output method with a value 0x00 i.e.0 as shown below:
int address = 888;
This was all about writing data to the parallel port; now let’s see how we can read data from the parallel port. In the file PortAccessAPI.cs we have declared a function “Input”, this will be used to read the parallel port. This function takes a parameter “address”, this is the address of the parallel port that we want to read. This method will return an integer as the data that is read from the requested port. The code will look like:
int address = 888;
value = PortAccessAPI.Input(adress);
The variable “value” will contain the data that is read from the parallel port 0x378.
1. How Inpout32.dll works?
2. Inpout32.dll for Windows 98/NT/2000/XP.
Using this simple technique of read/write we can create innovative gadgets that can be controlled using the computer...
Hope this helps you !
Your comments are always welcome!