Tutorial for controlling parallel port in c#

Hi friends, in this post i'll tell you how to control parallel port using c#, paralle port is long 25 pins port used for controlling many external devices (toy car, leds, stepper motor etc..). Lets first understand how does parallel port works.

Some Basic of Parallel Port

Parallel port contains twenty five pins, in signaling, open signals are "1" and close signals are "0" so it is like binary system . To control the external devices we may use data pins (D0 to D7) as shown in picture below.


When data pin is set to logical "1" then it gets +5 v thus allowing us to run any appliance that works on 5V. For controlling LEDs we can't directly use 5v as they work on 3v so we will add resistance of 470 ohm, below is the diagram is the diagram of our circuit that we will be using to control LED.



Code to make the LEDs blink

Firts of all u need a to download inpout32.dll to control the paralell port data pin. Once u've dll copy it in debug and release folder of your program folder..

Now lets start the coding part ..

First add a class to import the refrence of the inpout32.dll so that it can used in the program .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
class PortInterop
{
[DllImport("inpout32.dll", EntryPoint = "Out32")]
public static extern void Output(int address, int value);
}
}
So you have to import inpout32.dll to your debug or release directory. By the way, the main thing in my Form1.cs is PortAccess.Output. It takes two variables which are address and value. If your data ports are set in "0x378" you will have to write "888" because "378" Hexadecimal is equal to "888" in decimal. (Default LPT1 is set to "378") If you are using LPT2 which is "0x278" you have to write for the address "632" . Dont get confused from these default config is LPT1 so use 888 in ur program if it does't work then replace 888 with 632 thats it!!

I've used pretty simple logic for giving blinking effect to leds...

Led 1 (glow) --> Time Delay ---> Led 2 (glow) --> Time Delay ---> Led 3 (glow) --> Time Delay ---> Led 4 (glow) --> Time Delay ---> Led 5 (glow) --> Time Delay ---> Led 6 (glow) --> Time Delay ---> Led 7 (glow) --> Time Delay ---> Led 8 (glow) ---> Time Delay and cycle continues ...

Below is the code form of above logic ..

while (true)// To keep

leds blinking forever...

{
PortInterop.Output(888, 1);//to set data pin 1 to logical one thus making
//first led to blink ...
System.Threading.Thread.Sleep(100);//to add time delay of 100 milli secs..
PortInterop.Output(888, 2);
System.Threading.Thread.Sleep(100);
PortInterop.Output(888, 2);
System.Threading.Thread.Sleep(100);
PortInterop.Output(888, 4);
System.Threading.Thread.Sleep(100);
PortInterop.Output(888, 8);
System.Threading.Thread.Sleep(100);
PortInterop.Output(888, 16);
System.Threading.Thread.Sleep(100);
PortInterop.Output(888, 32);
System.Threading.Thread.Sleep(100);
PortInterop.Output(888, 64);
System.Threading.Thread.Sleep(100);
PortInterop.Output(888, 128);
System.Threading.Thread.Sleep(100);
}
I've also uploaded the video of aboce tutorial check it out( for more clarity please watch it in full screen mode ) ..



Port can be used to control almost any electrical devices and its easy to program that why i've making use of it in my projects, i hope u enjoyed the tutorial for more c# tutorials u can subscribe this blog ....

We will Like to send you some more information related to blogger tricks, hacks, latest gadgets and many more

happy

0 comments:

Post a Comment