Option Explicit
'Declare use of the DLL
Private Declare Function Out8255 Lib "8255.dll" (ByVal PortAddress As Integer, ByVal PortData As Integer) As Integer
Private Declare Function In8255 Lib "8255.dll" (ByVal PortAddress As Integer) As Integer
'Declare variables
Dim BaseAddress As Integer: ' 8255 Base Address
Dim Dummy As Integer: ' Dummy variable used with DLL
Dim PortA As Integer: ' 8255 Port A address
Dim PortB As Integer: ' 8255 Port B address
Dim PortC As Integer: ' 8255 Port C address
Dim Cntrl As Integer: ' 8255 Control Address
Dim PortValue As Integer: ' decimal value read at port
Dim Start As Integer: ' Start flag
Dim Msg As String
Dim Style As Integer
Dim Response As Integer
Dim PortSelected As Integer

Private Sub cmdEnd_Click()
Beep
'txtOutputWindow.Text = "Stopped"
' quit program
End
End Sub

Private Sub cmdGo_Click()
If Start = 0 Then
    ' user clicked GO button first time
    If txt8255Address.Text = "" Then
        ' Base address was not defined
        Msg = "Enter a Base Address! e.g. 608"   ' Define message.
        Style = vbOK + vbExclamation ' Define buttons.
        Response = MsgBox(Msg, Style)
        Exit Sub
    End If
    
    Start = 1: ' Go button enabled; start counting
        
    cmdGo.Caption = "Pause"
    ' Assign values for all addresses
    BaseAddress = Val(txt8255Address.Text)
    PortA = BaseAddress
    PortB = BaseAddress + 1
    PortC = BaseAddress + 2
    Cntrl = BaseAddress + 3

    ' determine which port to output to
    ' default is Port A
    If optPortA.Value = True Then
        PortSelected = PortA
    End If
    If optPortB.Value = True Then
        PortSelected = PortB
    End If
    If optPortC.Value = True Then
        PortSelected = PortC
    End If
    
    ' configure all ports for input
    Dummy = Out8255(Cntrl, 155)
    ' initialize all Ports to 0
Else
    Start = 0: ' user clicked GO button again
    cmdGo.Caption = "Go!"
End If
End Sub


Private Sub Form_Load()
' Program is loaded with these values
    txtOutputWindow.Text = "Enter Base Address"
    Start = 0: 'Counting action not started
    optPortA.Value = True ' Default port is A
End Sub

Private Sub tmrTimer_Timer()
If Start = 1 Then
    PortValue = In8255(PortSelected)
    txtOutputWindow.Text = "Value = " + Str(PortValue)
End If

End Sub

