You are on page 1of 5

PROGRAMMING THE PARALLEL PORT (Parallel port)

Programming the Parallel Port in Visual Basic

Home

Parallel Port Programming in Visual Basic

First off. This will not work in Windows NT/2000/XP without some extra work. This is
because these operating systems have security issues with programs directly accessing ports. I
found this link: http://www.zeecube.com/IOAccess/index.htm that has some info on how to get
around this but you may need to do some more searching. That said, since Visual Basic doesnt
provide direct access to the parallel port we will have to use an external control or DLL. I am
using the INPOUT32.DLL for this. I chose this since a post on PSC yesterday had some info on
programming in the parallel port but no code and they said to use this one. So thats why Im
using it.
To get the full effect of this it is helpful to have a small
amount of electronics knowledge. You could do this simply by sticking the
positive side of a LED into pins 2-9 on your parallel port and the negative
side of the LED into pins 18-25 if you dont have much electronics
knowledge. However, below is the schematic for a simple array of LEDs that
I made up last night. You could build this very easily if you know how to
solder or you could even go to the local Radio Shack and buy a breadboard,
some wire, and 8 LEDs.

With this circuit you would simply run a wire from the positive
the LEDs to one of the pins on the parallel port, pin 2-9, and
other end of the LED to pin 18-25 of the parallel port. All of
wires can be put into 1 of the parallel ports ground pins, not
individual wires but 1 wire that they all hook up to.

end of all of
then the
the ground
all 8

pins do.

The diagram below shows the parallel port and what all of the
For our current purposes, we only need pins 2-9 and pins 18-25.

Next we will discuss programming the parallel port. Attached is a sample


program that allows you to turn on one of the data pins at a time. This
would be used to light up a LED that was attached to that pin. However, I
will walk you through the process of making that program right now.

Ok, as I stated above, Visual Basic doesnt provide any direct


access to the parallel port. So, we will have to declare the functions that
we will need to use from the INPOUT32.dll. Here are the declarations that I
placed into the Declarations section of Form1:

Private Declare Function Inp Lib "inpout32.dll" _


Alias "Inp32" (ByVal PortAddress As Integer) As Integer

Private Declare Sub Out Lib "inpout32.dll" _


Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)

These declarations allow us to read data from the parallel port and also to
write data to the parallel port. We would use Inp to read and Out to write.
For anyone familiar with QBASIC, you will recognize these as the commands
used to read and write to the parallel port. In these declarations,
PortAddress is the hexidecimal address for the parallel port and Value is the
value that you want to write to the parallel port.

Next, I declared a variable to hold the value of the port address


so that we dont have to put it in every time:

Dim PortAddress As String

Next, we place code into the Form_Load Sub:

PortAddress = &H378

This is the default HEX address to the parallel port. This might be
different on your computer so you would go to the device manager and click on
the Resources tab under your parallel port.

Next, I added 9 command buttons to my form and labeled the first 8 of them
Pin 1, Pin 2, etc. The last one is labeled Off and is used to turn off all
of the lights. Here is the code for the command buttons:

Private Sub Command1_Click()


Out PortAddress, 1
End Sub

Private Sub Command2_Click()


Out PortAddress, 2
End Sub

Private Sub Command3_Click()


Out PortAddress, 4
End Sub

Private Sub Command4_Click()


Out PortAddress, 8
End Sub

Private Sub Command5_Click()


Out PortAddress, 16
End Sub

Private Sub Command6_Click()


Out PortAddress, 32
End Sub

Private Sub Command7_Click()


Out PortAddress, 64
End Sub

Private Sub Command8_Click()


Out PortAddress, 128
End Sub

Private Sub Command9_Click()

Out PortAddress, 0
End Sub

As you can see, the code for command buttons 1 through 8 is almost identical
except for the numbers. Those numbers simply state which of the data ports
to send the data to. You might think that you would put in 3 to output to
data port 3 and 6 to output to data port 6. However, if you were to put in 3
it would output to data ports 2 and 3, and if you were to put in 6 it would
output to data ports 3 and 4. This is because the data ports on the parallel
port go by powers of 2. The first port is 1, the second is 2, the third is 4
and so on. All of the numbers in between are used to light up different
combinations of the pins. All numbers between say 8 and 16 will light up all
of the lights on the first data port through the sixth data port. I will
leave you to figure out those combinations though. I will, however, tell you
that the values for the data ports go from 1 to 255. If you put in a number
above 255 you will get an overflow error.

The code for command9 sends to port 0. This will turn off all of the data
ports and will send the data through pin 1 on the parallel port.

As you can see, this is only a very basic tutorial on using the parallel
port. Using this code you would be able to turn off any device that will run
off of +5 volts DC. So, you would be able to run motors or basically any
other device that uses 5 volts DC. If you know enough about electronics you
could even make a device to turn on and off lights or other electrical
appliances in your home. I will be writing more advanced tutorials on the
parallel port at a later date but I think that this should get you started
with programming the parallel port in Visual Basic.
Source: Joel Weirauch

Home

You might also like