Visual Basic 6.0
Sample Parallel Port I/O
Program Listing


 

'Parallel port I/O program
'David A. Ward March 2000

'The flashing LED show uses 8 LED's with their anodes connected
'to D0 through D7 (pins 2 through 9) of the parallel printer port with
'430 ohm series resistors.

'The read from port routine reads 8 bits connected through
'a DIP switch using 8-4.7K pull-down resistors.  Switches
'open reads in as a

'inpout32.dll must be in the Windows\Systems directory
'for the INP and OUT commands to work.

'the following code must also be in a separate 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

Option Explicit
Dim X, Y, T, Z, PortAddress, LowNib, HighNib, Number, Total As Integer

'Flashing LED show
Private Sub cmdShow_Click()
 MousePointer = 13  'change cursor to indicate the program is busy
 'shift LED's from LSB up through MSB
 Z = 0
  While Z < 10
   X = 0
    While X < 8
       Out PortAddress, 2 ^ X
       Call Delay
       X = X + 1
    Wend
   'Shift LED's from MSB down through LSB
    X = 7
    While X > -1
       Out PortAddress, 2 ^ X
       Call Delay
       X = X - 1
     Wend
   Z = Z + 1
  Wend
Z = 0
While Z < 10
Out PortAddress, 0
    Out PortAddress, 170
    Call Delay
    Out PortAddress, 85
    Call Delay
    Z = Z + 1
Wend
    Out PortAddress, 0
    MousePointer = 0
End Sub

'Delay subroutine
Private Sub Delay()
   Dim PauseTime, Start
   PauseTime = 0.01 ' Set duration.
   Start = Timer    ' Set start time.
   Do While Timer < Start + PauseTime
      DoEvents      ' Yield to other processes.
   Loop
End Sub

'Write check box data to the parallel port
Private Sub cmdWriteToPort_Click()
    If Check1(0).Value = vbChecked Then Number = 1: Shape1(0).FillColor = &HFF& Else Shape1(0).FillColor = &H0&
    If Check1(1).Value = vbChecked Then Number = Number + 2: Shape1(1).FillColor = &HFF& Else Shape1(1).FillColor = &H0&
    If Check1(2).Value = vbChecked Then Number = Number + 4: Shape1(2).FillColor = &HFF& Else Shape1(2).FillColor = &H0&
    If Check1(3).Value = vbChecked Then Number = Number + 8: Shape1(3).FillColor = &HFF& Else Shape1(3).FillColor = &H0&
    If Check1(4).Value = vbChecked Then Number = Number + 16: Shape1(4).FillColor = &HFF& Else Shape1(4).FillColor = &H0&
    If Check1(5).Value = vbChecked Then Number = Number + 32: Shape1(5).FillColor = &HFF& Else Shape1(5).FillColor = &H0&
    If Check1(6).Value = vbChecked Then Number = Number + 64: Shape1(6).FillColor = &HFF& Else Shape1(6).FillColor = &H0&
    If Check1(7).Value = vbChecked Then Number = Number + 128: Shape1(7).FillColor = &HFF& Else Shape1(7).FillColor = &H0&
 
Out PortAddress, Number
Text1.Text = Number
Number = 0
End Sub

'Read binary data from the parallel port
Private Sub CmdRead_Click()
    Out PortAddress + 2, 15
    Out PortAddress + 1, 128
    LowNib = (Inp(PortAddress + 2) And 15)
    HighNib = (Inp(PortAddress + 1) And 240)
    Total = LowNib + HighNib
    Text2.Text = Total
 
    If Total And 1 Then Text3(0).Text = "1" Else Text3(0) = "0"
    If Total And 2 Then Text3(1).Text = "1" Else Text3(1) = "0"
    If Total And 4 Then Text3(2).Text = "1" Else Text3(2) = "0"
    If Total And 8 Then Text3(3).Text = "1" Else Text3(3) = "0"
    If Total And 16 Then Text3(4).Text = "1" Else Text3(4) = "0"
    If Total And 32 Then Text3(5).Text = "1" Else Text3(5) = "0"
    If Total And 64 Then Text3(6).Text = "1" Else Text3(6) = "0"
    If Total And 128 Then Text3(7).Text = "1" Else Text3(7) = "0"
    Out PortAddress, Total
End Sub

'Change port address
Private Sub Port_Click()
   PortAddress = Port.Text
End Sub

'Clear out bits on form and set all port bits to 0
Private Sub CmdClear_Click()
    For X = 0 To 7
    Check1(X).Value = False
    Shape1(X).FillColor = &H0&
    Text3(X) = ""
    Next X
    Number = 0
Out PortAddress, Number
Text1.Text = Number
Text2.Text = Number
End Sub

Private Sub Form_Load()
'Test program for inpout32.dll
'Change PortAddress to match the port address to write to:
'Usual parallel-port addresses are:
'&h378 (888), &h278 (632), &h3BC (956)
PortAddress = 888
Out PortAddress + 2, 15
Port.Text = PortAddress
End Sub

Back