Monday, August 18, 2008

How to program Read,Write parallel port in C#?

Introduction

The Parallel Port is the most commonly used port for interfacing homemade projects. Parallel ports are easy to program and faster compared to the serial ports. But main disadvantage is it needs more number of transmission lines. Because of this reason parallel ports are not used in long distance communications. This port allows the input of up to 9 bits or the output of 12 bits at any one given time, thus requiring minimal external circuitry to implement many simpler tasks. The port is composed of 4 control lines, 5 status lines and 8 data lines. It's found commonly on the back of your PC as a D-Type 25 Pin female connector.

What is a parallel port?
A port contains a set of signal lines that the CPU sends or receives data with other components. We use ports to communicate via modem, printer, keyboard, mouse and other such devices. In signaling, open signals are "1" and close signals are "0" so it is like binary system. A parallel port sends 8 bits and receives 5 bits at a time.

The parallel port comprises of 3 different ports the Data Port, the Status Port and the Control Port. These ports are distributed among the 25 PIN of the port. The data port ranges from PIN 2 to PIN 9 i.e. 8 PIN indicating a byte. The Status port ranges from PIN 10 to PIN 13 and the PIN 15 is also a part of the status port. The Control port comprises of PINs 1, 14, 16 and 17. The detail of these PINs and their functions is given below:
* Pins with * symbol in this table are hardware inverted. That means, If a pin has a 'low' ie. 0V, Corresponding bit in the register will have value 1.
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.

Create a Console application project using Visual Studio. Add a file PortAccessAPI.cs and add the following code to this file:
// References
using System;
using System.Runtime.InteropServices;

/// <summary>
/// Class to access the Port API's from the inpout32.dll.
/// </summary>
public class PortAccessAPI
{
/// <summary>
/// This method will be used to send the data out to the parallel port.
/// </summary>
/// <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>
[DllImport("inpout32.dll", EntryPoint="Out32")]
public static extern void Output(int address, int value);

/// <summary>
/// This method will be used to receive any data from the parallel port.
/// </summary>
/// <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);
}
The 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.

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;
PortAccessAPI.Output(adress, value);

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;
PortAccessAPI.Output(adress, 0);

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;
int value;
value = PortAccessAPI.Input(adress);

The variable “value” will contain the data that is read from the parallel port 0x378.

References
1. How Inpout32.dll works?
2. Inpout32.dll for Windows 98/NT/2000/XP.

Conclusion
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!

12 comments:

Anonymous said...

B@@!

Debashish said...

Thanks ..yaar...Keep it up.

Suresh Lasantha said...

Thanks.

I found some more information here,
http://dotnet-qs.blogspot.com/

Unknown said...

very helpful article, thanks alot

abhijeet said...

Good Boos :))

Anonymous said...

i have tried with C# 2008 express, under windows 7 Rc but it doesnt worked... :(

Anonymous said...

Hello,
Nice Explanation. But how can we use the the Pins 11-17
through inpout32.dll.

reply me at royael_2008@rediffmail.com

Unknown said...

who to read data from parallel..
i have some problem..
how to read data from LM35..
if it connect to PC..

i allready made hardware..

rply me at dikiaryanugraha89@gmail.com

Anonymous said...

Hi
I don't know how to read a bit stream from parallel port,I mean I want to read some kbits from parallel port and save them into a file

Would you please tell me what the proper code is in C#?
My Email: SalviaShadbakht@yahoo.com

Furby moderator said...

hello
i have a problem: i use impout32.dll to send data trogh paralel port to a driver board that controls 4 stepper motors.
problem is i need to send those bits with a 1ms delay.
wrote the C3 code for it but it's still very slow.
the code works great only if i start in background the program that came originally with the CNC machine(Foamworks).
with that program running in background my 1ms works great, if i turn off that program...the speed drops 5-6 times.
SO is that program loading any special driver that set's a higher pririty to the paralele port? or what?
if anyone can help me please EMAIL me at: brogdan@gmail.com

Busy Bee said...

I'm getting an exception when i try to call the functions of PortAccessAPI class...
An attempt to load a file of incorrect format,
this was the exception..
Plz tell how to correct this...

Unknown said...

Nice :) Good info.. Keep it up

Post a Comment