Parallel Port Guide
by David
Register DB-25 I/O Signal Name Bit Pin Direction =========== ======== ===== ========= -Strobe C0 1 Output +Data Bit 0 D0 2 In/Out +Data Bit 1 D1 3 In/Out +Data Bit 2 D2 4 In/Out +Data Bit 3 D3 5 In/Out +Data Bit 4 D4 6 In/Out +Data Bit 5 D5 7 In/Out +Data Bit 6 D6 8 In/Out +Data Bit 7 D7 9 In/Out -Acknowledge S6 10 Input +Busy S7 11 Input +Paper End S5 12 Input +Select In S4 13 Input -Auto Feed C1 14 Output -Error S3 15 Input -Initialize C2 16 Output -Select C3 17 Output Ground - 18-25 - Interrupt C4 - Input Input Enable C5 - Input
______________________________________________________ / \ \ 13 12 11 10 9 8 7 6 5 4 3 2 1 / \ / \ 25 24 23 22 21 20 19 18 17 16 15 14 / \________________________________________________/
Above is a pin diagram of a parallel port. Our primary concern will be with the Data Bits (Register Bit D0-D7, Pin 2-9) and Control Bits (Register Bit C0-C5).
Before interfacing with the Parallel Port we will need to do 3 things.
1 - In order for VB to write/read data from the port we will need a DLL to help us out. The one we will be using is call "inpout32.dll".
- The DLL will need to be downloaded, since it isn't part of window's standard library.
2 - We will need the parallel port I/O address. The address is usually 0x378, but you will need to check the address for each individual computer.
To locate the address, just head to "Device Manager", click on under Ports "(COM & LPT)", right click on printer port and click on properties.
Now a new window will pop up. Click on Resources tab and look at the number next the topmost "I/O Range" it should be something like "0378-037F".
Make note of the first number in the range (0378, in this case). This is call the Base Address and you will need this number to read/write to the parallel port.
IMPORTANT The number Windows gives you is in HEX! You must either convert it to 10base or use VB Hex format when coding!
Other common addresses are 0x3BC and 0x278.
3 - Lastly we must set the parallel port to PS/2 mode. This isn't a problem for most older computers, but new computers come default to different modes.
To change parallel port to PS/2 mode, reboot the computer and enter bios. From there just look for a setting to change the parallel port to PS/2 mode.
inpout32.dll in detail
Once you have inpout32.dll downloaded, you can copy the inpout32.dll to the Windows System32 folder and make the library file global to every program.
Or keep a copy inpout32.dll in the same folder where the program that uses it reside. This is the idea option because when experiments are transport between
computers, the inpout32.dll will be transported along with it.
inpout32.dll has basically two functions, one to read from the parallel port and one to write to it. You will need to declare these two functions in a VB Module.
Public Declare Function Inp
Lib "inpout32.dll" Alias "Inp32" (ByVal PortAddress As Integer) As Integer
Public Declare Sub Out Lib "inpout32.dll" Alias "Out32" (ByVal PortAddress As
Integer, ByVal Value As Integer)
The "Value" is the byte to write.
Inp returns the byte of the port you read.
The PortAddress is the I/O address (Base Address) you found earlier.
To parallel port is divided into 3 groups, Data Register (D0-D1), Feedback Register (S3-S7), and Control Register (C0-C5).
You will need to add an offset to the Base Address to access the group of registers you want.
Base Address + 0 = Data Registers
Base Address + 1 = Feedback Registers
Base Address + 2 = Control Registers
Our primary concern will be with the Data Registers (D0-D7) and the Control Register 5 (C5 - Input Enable).
We will need to enable register C5 if we are to change the Data Registers to input mode (which will most likely be the case).
To enable register C5 we just have to write the Byte XX1X XXXX (X = 0 or 1) to Base Address + 2.
As you can see for C#, the # is the bit location of the register bit, counting from 0 going from right to left.
So to enable all the control register (C0-C5) you would write 0011 1111 to Base Address + 2.
To disable all control register, you would write 0000 0000 to Base Address + 2.
Reading and writing to the Feedback and Data Registers are done in the same manner.
Remember, the numbers here are written in binary you will need to convert when coding!
So the code to enable input for the Data Registers would be something like:
Call Out(&H378 + 2, 32) 'Turn only C5 on and C0-C4 off
Again my base address was 0x378, yours might be different. Make sure you use the correct address!
Once input mode has been enable for the Data Bits, you may notice that the Data Bits will be stuck at 0xFF or 1111 1111.
No matter what you write to the data registers this will not change. The Data Bits are now in Input mode, so
the Data Bits can only be change by the device the parallel port is connected to. This is expected and should not be a cause
of concern.
Lastly to read the Data Registers, you will need to use a loop since interrupts are not used. For example, if a two-button box
is connected to parallel port where a press of a button sets D0 = 0 and the other button sets D1 = 0 we would use a code like
this to read the input:
Dim iInput
As Integer
Dim bLoop As Boolean
bLoop = True
While (bLoop)
iInput = Inp(&H378)
'0xFE = 1111 1110
0xFD= 1111 1101
If (iInput = &HFE or iInput = &HFD) Then
'Do whatever
bLoop = false 'End
loop
End If
DoEvents
Wend
This may all sound very complicated but once you get the hang of it, the parallel port is surprisingly very easy to program for.