Introduction
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.
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);
}
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:
B@@!
Thanks ..yaar...Keep it up.
Thanks.
I found some more information here,
http://dotnet-qs.blogspot.com/
very helpful article, thanks alot
Good Boos :))
i have tried with C# 2008 express, under windows 7 Rc but it doesnt worked... :(
Hello,
Nice Explanation. But how can we use the the Pins 11-17
through inpout32.dll.
reply me at royael_2008@rediffmail.com
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
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
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
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...
Nice :) Good info.. Keep it up
Post a Comment