Click here to Skip to main content
Click here to Skip to main content
Go to top

I/O Ports Programming (Parallel port) Reading / Writing + Surveillance System using VB.NET

, 18 Sep 2007
Rate this:
Please Sign up or sign in to vote.
Input Output Port Accessing and Surveillance System using VB.NET and Introduction to Parallel Ports
Screenshot of the Sample Application

Contents at a Glance

Introduction

The parallel port, or printer port, is one of the easiest pieces of hardware available to users for communicating with external circuitry or homemade hardware devices. Most people think that interfacing with an external circuit is hard and never come up with a solution. However, in this article, I am trying to convince you that porting with another external device is very easy and it has only a few tricks to be remembered!

I will also be discussing a sample surveillance application that can be developed with few requirements. It is very simple and anyone who reads this article can use it at home. I will then be giving an introduction to Serial Port Programming, which will be my next article on The Code Project. In that section, I will illustrate some basics of Serial Data Transmission and how to use a more advanced serial port instead of a parallel port.

The application that I have given is developed in Visual Basic .NET and it uses inpout32.dll for direct port access. In Windows XP and NT versions, direct port accessing is prohibited, so we need to have a driver in order to communicate with ports directly. Therefore we are using a freeware driver, inpout32.dll, which can be found here.

WARNING

Parallel Ports that are built into your machine can be damaged very easily! So, use all of my sample code at your own risk. All of these sample programs and circuits are thoroughly tested and they will never lead you to damage your hardware or your computer. I have never experienced such a bad incident! However, use these programs with great care…

Communicating with the Parallel Port

The parallel port usually comes as a 25-pin female port and it is commonly used to connect printers to a computer. Many geeks also use it to connect their own devices to their PCs. There is a few more things to remember when using a PC's Parallel Port. It can load only 2.5mA and ~2.5 volts. It is better to use opto-couplers or ULN2803 when interfacing with an external device.

If you're just thinking of lighting up an LED through a Printer Port, try to connect a 330 ohm resistor in between. There is some more useful information to consider when programming a Printer Port: the usual Printer Port is a 25-pin female D-shaped port and it has several registers. A register is a place where you can write values and retrieve them. There are different types of registers called Data Register, Control Register and Status Register.

Data Register

This is the register that allows the user to write values into the port. In simple words, these pins can be used to output a specific value in a data register. You can also change voltages in specific pins. These are called output pins. There are altogether 8 output pins available, ranging from D0 to D7.

Status Register (Pins)

These are called input pins or status registers and can hold a value that the outside world gives to the Parallel Port. So, this port acts like a reader and it has 5 pins for inputs. The pin range is S4 to S7.

Control Register (Pins)

This register can be used in both ways: it enables a user to write values to the outside world, as well as read values from the outside world. However, you need to remember that most of the control pins work in an inverted manner. You can see them with a dash sign on the top of the pin. Pin range is C0 to C3. Ground pins are used as neutral; these pins are used as (-) in batteries. If you're connecting a device to a Parallel Port in order to read or write, you have to use one or more ground pins and a read/write pin to work. For example, if you're trying to light up an LED, then you have to connect the (-) of the LED to a ground pin and the (+) of the LED to an output pin. For reading purposes, use the same mechanism.

  · 8 Output  pins [D0 to D7]

  <!--[if !supportLists]-->
  · 5 Status  pins [S4 to S7 and S3]

  <!--[if !supportLists]-->
  · 4 Control  pins [C0 to C3]

  <!--[if !supportLists]-->
  · 8 ground  pins [18 to 25]
Parallel Port Layout

Addressing the Parallel Port and Registers

Port addressing controls a great deal in port programming, as it is the door that enables programs to connect to the external circuit or device. Therefore I would like to explain all the available port addresses.

In normal PCs, a parallel port address can be one of the addresses given below, but depending on the BIOS settings and some other issues, the parallel port address can vary. However, it always lies in one of these address ranges.

<!--<span class="code-comment">[if !supportLists]--></span>
· LPT1 03BC (hex)             956(decimal)

<!--<span class="code-comment">[if !supportLists]--></span>
· LPT2 0378(hex)              888(decimal)

<!--<span class="code-comment">[if !supportLists]--></span>
·
<!--<span class="code-comment">[endif]--></span>
LPT3 0278 (hex)               632(decimal)

As I mentioned above, those are the commonly used address ranges for parallel ports. Now you might be wondering how we communicate with each of them and how they work. It's very simple! Each of those above-mentioned address ranges has its own address for Data Register, Control Register and Status Register. So, if you want to access a Data register of port 888, then the Data register is also 888. However, if you want to access a status register, then add +1 to the 888 = 889 and this is the status register. To access a control register, what you have to do is add another +1 so that it is 890 and that is the Control register of the 888-based parallel port. If your parallel port address is different, please do this accordingly to find its corresponding registers.

There is another simple way to check the parallel port address of your computer. You can get to know the parallel port I/O addresses through the device manager. First, open the device manager (Start -> Settings -> Control Panel -> System -> Hardware -> Device Manager). Then select the parallel port you are interested in from the Ports (COM & LPT) section. With the mouse's right button, you can get the menu where you select Properties. When you go through steps, you will see a dialog similar to this. Locate the first item in the listbox and it carries the value 0378 – 037F so that this is the address range of the Printer Port. The data register address is 0378 in hex or 888 in dec.

Parallel Port Layout

Small Description of Inpout32.dll

Here in this article, we are using a third party tool called inpout32.dll to connect to ports available in your PC. We are using it because in Windows NT based versions, all the low-level hardware access is prohibited and a separate I/O driver is needed. When we read the status of the port, we have to take the support of that third party application.

In inpout32.dll, all of the system drivers and small tools needed to activate and run that system driver are included. So, we don't have to consider much or think about how this happens; it will do everything for us…

As far as we are using a DLL file to communicate with the hardware, we have to know the links that this particular file provides for the user, or the entry points for that particular file. Here, I will describe all the entry points one by one.

DLL Entry used to read

 Public Declare Function Inp Lib "inpout32.dll" Alias "Inp32" _
     (ByVal  PortAddress As Integer)  As Integer

DLL Entry used to output values

Public Declare Sub Out Lib "inpout32.dll" Alias "Out32" _
    (ByVal  PortAddress As Integer,  ByVal Value As Integer)

The Inp32 function can be used to read values from any address that is given through its PortAddress parameter. Also, the Out32 function is specifically used to write values to data ports. A long description on both functions can be found below.

Reading a Pin (Status Register) or External Value

Inp32 is a general-purpose function defined inside inpout32.dll to communicate with various hardware addresses in your computer. This function is included in a DLL, so this file can be called within any programming language that supports DLL import functions.

Here, for this sample application, I have used Visual Basic .NET because it can be used by any person who knows a little about the .NET Framework. What you have to do to read the status of a parallel port is very simple. I will explain everything in steps, but keep in mind that in the sample coding I assume that the Address of the status register is 889 or 0x379h in hex.

  1. Open Visual Studio .NET (I made this in the .NET 2005 version).
  2. Make a new Visual Basic .NET Windows application.
  3. Go to the Project menu and Click on "Add new Item." Then select the Module item in the listbox.
  4. Type any considerable name for the new module that you're going to create, for example, porting.vb.
  5. In the Module file, there should be some text like this:
    Module porting
        Public Declare Function Inp Lib "inpout32.dll" _
        Alias "Inp32" (ByVal PortAddress As Integer) As Integer
    End Module
  6. Go to your form and add a new text box. Then add a Command button and, in the click event of the command button, paste this code:
    'Declare variables to store read information and Address values
    Dim intAddress As Integer, intReadVal As Integer
    
    'Get the address and assign
    intAddress = 889
    
    'Read the corresponding value and store
    intReadVal = porting.Inp(intAddress)
    
    txtStatus.Text = intReadVal.ToString()

    This will work well only if the name of the text box you just made is txtStatus.

  7. Run your application and click on the button so that the text should be 120 or some similar value. Then that is the status of your parallel port.
  8. If you want to check whether this application runs properly, you just have to take a small wire (screen wire or circuit wire is ok) and short a Status Pin and a Ground Pin. For example, Status pins are 10,11,12,13,15 and if you short the 10th pin and 25th (Ground) pin, then the value in the textbox will be 104. If you short some other pins, like the 11th and 25th, then the value of the text box will be 88 or a similar value.

Now you can clearly see that all the power is in your hands. If you're a creative person with a sound knowledge of electronics, then the possibilities are endless…

Note!

Before you run and compile this application, download inpout32.dll from this page and copy it to the Bin folder of your project directory, the place where the executable file is made, so that the DLL should be in the same directory to work. Alternatively, you can just copy and paste the DLL file to your Windows System directory and then you don't want to have that file in your application path. You only have to follow one guide.

Writing to a Pin (Data Register)

When writing data to the Data Register, you have to follow the same rules. There is no difference in basics! As you learned in the above context, do the same thing here with a few differences. I will explain to you what they are, but keep in mind that in the sample coding I assume that the address of the data register is 888 or 0x378h in hex.

  1. Go to the Project menu and Click on "Add New Item." Then select the Module item in the list box, the same as above.
  2. Type any considerable name for the new module that you're going to create, for example, porting.vb.
  3. In the Module file, there should be some text like this:
    Module porting
       Public Declare Sub Out Lib "inpout32.dll" _
       Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)
    End Module
  4. Go to your form and add a Command button. In the Click event of the Command button, paste this code:
    intVal2Write As Integer
    
    'Read the corresponding value and store
    
    intVal2Write = Convert.ToString(txtWriteVal.Text)
    
    'porting is the name of the module
    porting.Out(888, intVal2Write)

    This will work properly only if the name of the text box you just made is txtWriteVal.

  5. Run your application and click on the Button so that the parallel port will output voltages in pins according to the value you entered in the text box.

How to Light up LEDs

Now to test the value that you output. Assume that you wrote value 2 to the data register and you want to know which pin outputs +5V. It's really simple to check. The data register starts from pin number 2 and ends in pin number 9, so there are 8 pins for output. In other words, it's an 8-bit register. So, when you write 2 to its data register, +5 voltage will be there in the 3rd pin. You have to take a scientific calculator and convert Decimal 2 to Binary; then the value is 2(DEC) = 1 0(BIN), so 1 0 means 0 0 0 0 0 0 1 0 is the status at the data register.

Example 2

If you write value 9 to a data register, then the binary of 9 is 0 0 0 0 1 0 0 1. So, +5V can be taken from the 2nd pin and the 5th pin.

Binary Value

0

0

0

0

1

0

0

1

Data Register

D7

D6

D5

D4

D3

D2

D1

D0

Actual Pin number

9

8

7

6

5

4

3

2

Example 3

Assume you're writing 127 to a data register. Then the binary of 127 is 0 1 1 1 1 1 1 1 and therefore all the pins will be +5V, excluding the 9th pin.

Binary Value

0

1

1

1

1

1

1

1

Data Register

D7

D6

D5

D4

D3

D2

D1

D0

Actual Pin number

9

8

7

6

5

4

3

2

When you want to light up an LED, there is a special way to connect them to a data register. So, you need some tools to connect. As a tutorial, I will explain how to connect only 1 LED.

How the LED should be connected to Printer Port

Here you have to connect a ground pin to any pin starting from the 18th to 25th. All are ground pins, so there is no difference. The output pin or positive of the LED should be connected to a pin in the data register. When you write a value which enables that particular data register pin, then the LED will light up.

Note!

Never connect devices that consume more voltage than LEDs, as this would burn your parallel port or entire motherboard. So, use them at your own risk...! I recommend that you refer to the method written below if you're going through more advanced methods. If you want to switch electric appliances on and off, then you will obviously have to use relays or high voltage Triacs. I recommend using relays and connecting them through the method I have illustrated below.

More Advanced Way to Connect Devices to Parallel Port
(Compact 8 Channel Output Driver)

If you're not satisfied with lighting up LEDs and you're hungry to go further, then use this method to connect all your advanced or more complex devices to the Parallel Port. I would recommend that you consider using ULN2803 IC. This is an 8-channel Darlington Driver and it is possible to power up devices with 500mA and +9V with it.

Here in this diagram, you can see how to connect a set of LEDs to this IC. Also remember that you can replace these LEDs with any devices such as relays or Solenoids that support up to 500mA and they will run smoothly.

How the LED should be connected to Printer Port

How Do the Surveillance System and Sample Application Run?

The surveillance system that I have written is very small and can only be used at home or for personal use. However, you can make it more advanced if you know how more complex security systems work. Here, the application has a timer and it always checks for status register changes. If something changes, then the application's status will be changed and a sound will be played accordingly.

This can be used to check whether your room's door is closed. What you have to do is place a push to turn on the switch on the edge of the door and you have to place it in a manner which activates when the door is opened. If you're not using a push to turn on the switch, then just place two layers of thin metal that can be bent and place them on the door. However, those two layers of metal should be able to make a short circuit when the door is closed. One piece of metal layering should be soldered to a screen wire (circuit wire) and that should be connected to the ground pin. The other layer of metal piece should be connected to a status pin of the parallel port. Status pins are 10, 11,12,13,15.

References

I have learned to program hardware ports since the year 2000. As far as I can remember, most of the time I used to get resources from Google.com, and I have used the links I found there... However, keep in mind that creativity cannot be searched from the web. It should be in your mind... So be creative and support the community...

  1. So Do It Yourself
  2. Inpout32.dll for Windows 98/2000/NT/XP

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Purinda Gunasekara
Web Developer
Australia Australia
Purinda Gunasekara is a researcher in Firmware developments, embedded systems and very much interested in low level programming and 3D Graphics programming. Some of his published researches are Infrasoft Earth Invasion which was the Gold Award winner at the National Best Quality Software awards in Sri Lanka, as well as at the International competition called "APICTA" in the year of 2005, it stands for "Asia Pacific ICT Awards" - http://www.apicta.com
 
In terms of programming interests, he first started writing programs in a PII machine using GWBasic and at the age of 14 he learned Delphi(Pascal) and lived with it for few years after he realized the power of C/C++ he newer looked back. Currently he is interested in php, C# and in embedded devices development using Microcontrollers.
 
Blog http://theredblacktree.wordpress.com/

Comments and Discussions

 
QuestionCreating Setup problems PingroupOsoJames2-Apr-14 22:50 
AnswerRe: Creating Setup problems PinmemberPurinda Gunasekara3-Apr-14 10:40 
QuestionParallel port PinmemberMember 1068912321-Mar-14 6:03 
AnswerRe: Parallel port PinmemberPurinda Gunasekara3-Apr-14 11:34 
Questionstatus pin problem PinmemberAYYANZ1113-Jan-13 23:27 
AnswerRe: status pin problem PinmemberPurinda Gunasekara3-Apr-14 11:37 
QuestionCnnot find whether i have got a control of parallel ports PinmemberUnmesh Bane29-Oct-12 18:23 
AnswerRe: Cnnot find whether i have got a control of parallel ports PinmemberPurinda Gunasekara29-Oct-12 23:15 
GeneralMy vote of 5 PinmemberMCSIASON30-Aug-12 23:36 
GeneralMy vote of 5 PinmemberNuwat Wh12-Jul-12 20:55 
QuestionHARDWARE interface PinmemberPRIYKANT8-Jun-12 19:16 
GeneralMy vote of 5 PinmemberPRIYKANT8-Jun-12 19:15 
GeneralMy vote of 5 Pinmemberoneo8-Mar-12 8:58 
QuestionChange Voltage on Pins PinmemberThayhor25-Jan-12 22:03 
QuestionControl registers PinmemberRddezh10-Dec-11 0:10 
GeneralRe: Control registers PinmemberPurinda Gunasekara10-Dec-11 16:50 
QuestionParallel Port Problem Need Urgent Help PinmemberSaddam Khan20-Sep-11 17:32 
GeneralRe: Parallel Port Problem Need Urgent Help PinmemberWeb_Hunter17-Nov-11 3:31 
AnswerRe: Parallel Port Problem Need Urgent Help Pinmemberhari51625-Mar-12 19:51 
Generalproblem inpout32.dll in visual stdio 2008 Pinmembersampath_MIT4-Sep-10 19:54 
GeneralRe: problem inpout32.dll in visual stdio 2008 Pinmembersamantha.b17-Sep-10 23:56 
This Dll is not compatible with Windows 7
SuggestionRe: problem inpout32.dll in visual stdio 2008 PinmemberGary Peters13-Feb-14 2:30 
Generalrecording to database Pinmemberdualshock0317-Jun-10 3:27 
QuestionSEHException external component has thrown an exception PinmemberBisratie1-Jun-10 11:48 
AnswerRe: SEHException external component has thrown an exception Pinmemberhari51625-Mar-12 19:44 
NewsParallel Port Digital Oszilloscope and Logic Analyzer PinmvpElmue24-Apr-10 3:47 
GeneralExternal component has thrown an exception. Pinmembergsuspradeep9-Nov-09 5:11 
GeneralThanks a lot PinmemberPooya200723-Sep-09 11:00 
GeneralLPT1 port address is &CCB8 PinmemberBern Squiggley27-May-09 7:37 
Questiondot matrix printer interfacing with PIC microcontroller [p24FJ256GB110]?? Pinmembermadhav_mahuli6-Apr-09 7:40 
Generalparallel port Pinmembervaish_kv28-Dec-08 18:47 
QuestionHow do I readback status message (PJL) from the parallel port? Pinmemberidosh22-Dec-08 10:04 
GeneralLPT address PinmemberMember 91403520-Jun-08 21:34 
GeneralReading GPS time from GPS Card Pinmembernajeeb shah19-May-08 18:26 
NewsProblem of "External component has thrown an exception." solved Pinmemberrednerus26-Apr-08 9:58 
GeneralRe: Problem of "External component has thrown an exception." solved Pinmembergsuspradeep9-Nov-09 5:59 
Questionwhat about serial port? Pinmembertervon11-Mar-08 3:43 
AnswerRe: what about serial port? PinmemberLaleh Asadi24-Aug-08 18:04 
AnswerRe: what about serial port? PinmemberMichał Zalewski29-May-10 21:54 
QuestionExternal component has thrown an exception PinmemberAruna Dissanayaka27-Feb-08 4:43 
GeneralUSB ports... Pinmemberdishandayarathna22-Feb-08 20:11 
GeneralThanks so much! PinmemberGary Reese17-Feb-08 3:31 
Generalproblem in second nibble PinmemberROBOTICAR26-Nov-07 6:38 
QuestionTeach me Pinmembersneakerslaw28-Oct-07 11:25 
GeneralI/ O ports PinmemberUche Nwoko16-Oct-07 7:19 
Generalinpout32.dll Pinmemberfwsouthern18-Sep-07 14:39 
AnswerRe: inpout32.dll PinmemberPurinda Gunasekara18-Sep-07 17:10 
GeneralRe: inpout32.dll PinmemberPurinda Gunasekara18-Sep-07 17:13 
GeneralRe: inpout32.dll Pinmemberxolisa26-Oct-08 3:32 
GeneralHelp needed Pinmembermike82200518-Sep-07 9:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 18 Sep 2007
Article Copyright 2007 by Purinda Gunasekara
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid