VB 2005 (express edition) Digital Input and Output Control

If you had difficulty getting code for VB2005 did this help?

  • yes
  • no
  • I dont use vb2005

0 voters

I’ve written a program which allows one to simply edit a text file which uses an extremely simple scripting language.

This allows for a digital input to trigger digital outputs in a serial fashion for different time intervals defined by a millisecond value

Ex. Here is the contents of my default.vel file:
I1
O1;1000.0
O2;200.0
O3;2000.0
I2
O4;1800.1
O5;200.0
O6;1000.1
I3
O1;2000.1
O2;2000.1
O3;2000.1
I4
O4;2000.1
O5;4000.1
O6;3000.0
I5
O1;2000.0
O2;2555.0
O3;555.1

I have discovered midway through that I need to learn how to program multithreaded to really make the program useful… However, I’m not overly interested in releasing the final code, so much as the compiled program… as there will be many more features (Webcam control, Vonage Dialing… etc.)

So beware this code is buggy and does no error detection and can only be used with a single 8055 board.

I’m posting it because I had essentially very few examples of code which would work with VB2005 express edition, and I hope the headaches for others can be lessened.

[code]

Option Strict Off
Option Explicit On
Imports System.Text
Imports System.Collections

Friend Class Form1
Inherits System.Windows.Forms.Form
Dim DoNothing As Boolean
Dim n As Short
Dim TotalCommands As Integer
Dim ScriptCommands As New Hashtable
Dim StopScript As Integer
Dim ValidInputs(0 To 100, 0 To 1) As String
Dim InputID As Integer
Dim CurrentLocation As Integer = 1
Dim InputLabels As String
Dim TotalValidInputs As Integer
Dim InputDetermine As String = “”
Dim OutputDetermine As String
Dim OutputPresent As String
Dim DigitalInputState As String

Private Declare Sub Version Lib "c:\8055\k8055d.dll" ()
Private Declare Function SearchDevices Lib "c:\8055\k8055d.dll" () As Integer
Private Declare Function SetCurrentDevice Lib "c:\8055\k8055d.dll" (ByVal CardAddress As Integer) As Integer
Private Declare Function OpenDevice Lib "c:\8055\k8055d.dll" (ByVal CardAddress As Integer) As Integer
Private Declare Sub CloseDevice Lib "c:\8055\k8055d.dll" ()
Private Declare Function ReadAnalogChannel Lib "c:\8055\k8055d.dll" (ByVal Channel As Integer) As Integer
Private Declare Sub ReadAllAnalog Lib "c:\8055\k8055d.dll" (ByRef Data1 As Integer, ByRef Data2 As Integer)
Private Declare Sub OutputAnalogChannel Lib "c:\8055\k8055d.dll" (ByVal Channel As Integer, ByVal Data As Integer)
Private Declare Sub OutputAllAnalog Lib "c:\8055\k8055d.dll" (ByVal Data1 As Integer, ByVal Data2 As Integer)
Private Declare Sub ClearAnalogChannel Lib "c:\8055\k8055d.dll" (ByVal Channel As Integer)
Private Declare Sub SetAllAnalog Lib "c:\8055\k8055d.dll" ()
Private Declare Sub ClearAllAnalog Lib "c:\8055\k8055d.dll" ()
Private Declare Sub SetAnalogChannel Lib "c:\8055\k8055d.dll" (ByVal Channel As Integer)
Private Declare Sub WriteAllDigital Lib "c:\8055\k8055d.dll" (ByVal Data As Integer)
Private Declare Sub ClearDigitalChannel Lib "c:\8055\k8055d.dll" (ByVal Channel As Integer)
Private Declare Sub ClearAllDigital Lib "c:\8055\k8055d.dll" ()
Private Declare Sub SetDigitalChannel Lib "c:\8055\k8055d.dll" (ByVal Channel As Integer)
Private Declare Sub SetAllDigital Lib "c:\8055\k8055d.dll" ()
Private Declare Function ReadDigitalChannel Lib "c:\8055\k8055d.dll" (ByVal Channel As Integer) As Boolean
Private Declare Function ReadAllDigital Lib "c:\8055\k8055d.dll" () As Integer
Private Declare Function ReadCounter Lib "c:\8055\k8055d.dll" (ByVal CounterNr As Integer) As Integer
Private Declare Sub ResetCounter Lib "c:\8055\k8055d.dll" (ByVal CounterNr As Integer)
Private Declare Sub SetCounterDebounceTime Lib "c:\8055\k8055d.dll" (ByVal CounterNr As Integer, ByVal DebounceTime As Integer)



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    

    ClearAllDigital()
    Dim CurrentLine As String = "1"

    Dim Counter As Integer
    'Dim TotalCommands As Integer

    Counter = 1


    Dim readerVar As IO.StreamReader
    readerVar = IO.File.OpenText("c:\8055\default.vel")
    While CurrentLine <> ""
        CurrentLine = readerVar.ReadLine
        ScriptCommands.Add(Counter, CurrentLine)
        Counter = Counter + 1

    End While

    TotalCommands = Counter - 1

    readerVar.Close()

    ScriptBox.Text = ""

    For Counter = 1 To TotalCommands
        ScriptBox.Text = ScriptBox.Text + ScriptCommands(Counter) + ControlChars.CrLf
    Next

    'Determine the Line Numbers where the Inputs
    'or Time Events Occur. Then the intervening lines are 
    'evaluated if they begin with the letter o for
    'digital output, or d for attached devices
    'These could be reused for multiple inputs, so they will be identified as
    'belonging to a specific input
    'Once the total Number of Inputs is determined, and the outputs assigned specifically
    'the scan for activation can occur. In the case of Digital Inputs, the Velleman
    'Board will be polled, for Timed events, the Current Time will be
    'Compared to The Timed events.
    For Counter = 1 To TotalCommands
        InputDetermine = ScriptCommands(Counter)
        OutputPresent = ScriptCommands(Counter + 1)

        If Microsoft.VisualBasic.Left(InputDetermine, 1) = "I" And Microsoft.VisualBasic.Left(OutputPresent, 1) = "O" Then ValidInputs(CurrentLocation, 0) = (Counter)
        If Microsoft.VisualBasic.Left(InputDetermine, 1) = "I" And Microsoft.VisualBasic.Left(OutputPresent, 1) = "O" Then ValidInputs(CurrentLocation, 1) = ScriptCommands(Counter)
        If Microsoft.VisualBasic.Left(InputDetermine, 1) = "I" And Microsoft.VisualBasic.Left(OutputPresent, 1) = "O" Then TotalValidInputs = TotalValidInputs + 1
        If Microsoft.VisualBasic.Left(InputDetermine, 1) = "I" Then CurrentLocation = CurrentLocation + 1
    Next




    'a list containing the Numbered Inputs that are valid exist
    'ie. The inputs have at least one output or device indicated after the input


    'The preceeding sequence loads the default script into the textbox
    'but provides no error checking or exception handling. Make sure 
    'default.vel is where it says it is.

    Dim i As Long
    Dim Checked As Boolean
    Dim Devices As Long
    Checked = False
    Devices = SearchDevices
    If Devices Then


        For i = 0 To 3
            If (Devices And 2 ^ i) Then
                If Not Checked Then
                    SetCurrentDevice(i)

                    Checked = True
                    CardStatus.Text = "Card " + Str(i) + " connected"

                End If
            Else
            End If
        Next i
    End If

    If Devices = 0 Then CardStatus.Text = "No Cards Connected"
    'This preceeding sequence detects connected card, and displays
    'the card address in the status bar otherwise no card is connected and
    'the execute command will be disabled through checking Checked = false

End Sub




Private Sub Form_Terminate()
    CloseDevice()  'Closes open card when the application is terminated
End Sub

'Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'    Connect_Click()
'End Sub

Private Sub OpenScriptButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenScriptButton.Click
    
End Sub
Private Sub ExecuteScript()
    'ClearAllDigital() For some reason this command does not work.. go figure
    WriteAllDigital(0) 'Ironically the writealldigital controls the outputs as a binary array where 255 is all 8 inputs turned on and 0 is all off
    Dim Counter As Integer = 1
    





    'Now we need to scan for an occurrence of each of the valid inputs 
    'The values returned by ReadAllDigital are as follows
    'Input 1 = 1
    'Input 2 = 2
    'Input 3 = 4
    'Input 4 = 8
    'Input 5 = 16
    'Therefore one statement can be used for checking if 
    'We recognize the squared value

    Dim i As Integer = 0
    DigitalInputState = ReadAllDigital

    For i = 1 To 5
        If DigitalInputState = 1 Then InputID = 1
        If (DigitalInputState And 2 ^ i) Then InputID = i + 1

    Next i
    If InputID = 0 Then Return
    'compare the pressed input to the valid inputs, then execute outputs or skips it over
    Dim OutputStartLine As Integer
    OutputDetermine = ""
    Dim InputIDStr As String
    Dim ParsedID As String
    InputIDStr = InputID
    ParsedID = "I" + InputIDStr
    For i = 1 To TotalValidInputs
        If ParsedID = Microsoft.VisualBasic.Left(ValidInputs(i, 1), 2) Then OutputStartLine = ValidInputs(i, 0)

        
    Next i
    Dim OutputID As String
    Dim OutputNumber As Integer
    Dim EndTimeChar As String = ""
    OutputDetermine = ScriptCommands(OutputStartLine + 1)
    'Process the Output instructions one at a time, making timers for events which
    'will allow continued execution and scanning on the other inputs.
    While Microsoft.VisualBasic.Left(OutputDetermine, 1) <> "I"

        OutputID = Microsoft.VisualBasic.Left(Microsoft.VisualBasic.Mid(OutputDetermine, 2), 1)
        OutputNumber = Convert.ToInt32(OutputID)
        SetDigitalChannel(OutputNumber)
        Dim TimeLeft As String
        Dim Time As String
        Dim TimeRemaining As Integer
        Dim StripValue As Integer
        TimeLeft = Mid(OutputDetermine, 4)
        StripValue = Len(TimeLeft)
        Time = Microsoft.VisualBasic.Left(TimeLeft, StripValue - 2)
        TimeRemaining = Convert.ToInt32(Time)
        System.Threading.Thread.Sleep(TimeRemaining)
        ClearDigitalChannel(OutputNumber)
        OutputStartLine = OutputStartLine + 1
        OutputDetermine = ScriptCommands(OutputStartLine + 1)
    End While


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ExecuteScript()


End Sub

End Class[/code]