How do I use Visual Basic (VB) to communicate with my Remote IO Module?

FAQs

Brainboxes provides a .NET API – called Brainboxes.IO – this allows easy integration of Brainboxes Remote Ethernet IO modules with your Windows software applications.

Requirements

Visual Basic .NET Code Samples

VB for .net 2.0 and above

The following code works in VB targeted at .net version 2.0 and above

Imports Brainboxes.IO

Module VBConsoleModule

    Sub Main()
        'tcp connection
        Dim connection As IConnection = New TCPConnection("192.168.0.158")
        'or serial connection on COM3
        'connection = new SerialConnection(3)

        'ED588 is the model number of the device could be e.g. ED038, ED204 etc.
        Dim ed As EDDevice = New ED588(connection)
        ed.Label = "Pump Control in Cabinet 3" 'optionally assign a label, useful for debugging

        'open connection
        ed.Connect()

        'set all outputs open/off/0
        ed.Outputs.Values = 0

        'set Dout0 closed/on/1
        ed.Outputs(0).Value = 1

        'read Din0: 1 = high, 0 = low
        Console.WriteLine("Din0 is "& ed.Inputs(0).Value)

        'optionally label a line for ease when debugging
        ed.Inputs(3).Label = "Pump enabled Monitor"

        'add event handler for rising edge on Din3
        AddHandler ed.Inputs(3).IOLineRisingEdge, AddressOf DIN3IOLineRisingEdge

        'add event handler for falling edge on any of the inputs
        AddHandler ed.Inputs.IOLineFallingEdge, AddressOf Inputs_IOLineFallingEdge

        'add event handler for any change on any of the inputs or outputs of the device
        AddHandler ed.IOLineChanged, AddressOf ed_IOLineChanged


        Console.WriteLine("Press enter to exit...")
        Console.ReadKey()

        'close connections after a key press
        ed.Disconnect()
    End Sub


    'called when DIN3 goes from 0 -> 1
    Public Sub DIN3IOLineRisingEdge(line As IOLine, device As EDDevice, changeType As IOChangeTypes)
        Console.WriteLine(line.ToString() & " rising edge!")
        'or for full debug data about line:
        Console.WriteLine(line.Describe())
    End Sub

    'called when any of the inputs goes from 1 -> 0
    Public Sub Inputs_IOLineFallingEdge(line As IOLine, device As EDDevice, changeType As IOChangeTypes)
        Console.WriteLine(line.ToString() & " falling edge!")
    End Sub

    'called when any change occurs on any of the inputs or output
    Public Sub ed_IOLineChanged(line As IOLine, device As EDDevice, changeType As IOChangeTypes)
        Console.WriteLine(line.ToString() & " of device " & device.ToString())

        'changeType can be one of:
        ' IOChangeTypes.FallingEdge
        ' IOChangeTypes.RisingEdge
        ' IOChangeTypes.Latched -- within the sample time the line has transitioned 2 times

        'or for full debug data about device:
        Console.WriteLine(device.Describe())
    End Sub

End Module

VB for .net 3.5 and above

The following code works in VB targeted at .net version 3.5 and above

Imports Brainboxes.IO

Module VBConsoleLinqModule

    Sub Main()
        Try
            'alternative use a serial connection on e.g. new SerialConnection(3)
            'ED588 is the model number of the device could be e.g. ED038, ED204 etc.
            Using ed As EDDevice = New ED588(New TCPConnection("192.168.0.158"))
                ed.Label = "Pump Control in Cabinet 3" 'optionally give it a label, useful for debugging

                'first 3 inputs are connected to button switches
                Dim buttons As IOList(Of IOLine) = ed.Inputs.Take(3).AsIOList()
                buttons.Label = "Console Buttons" 'optionally label group, useful for debugging

                'first 3 outputs are connected to lights
                Dim lights As IOList(Of IOLine) = ed.Outputs.Take(3).AsIOList()

                'attach anonymous event handler to falling edge of all buttons
                'when the button is turned on the corresponding light will come on
                AddHandler buttons.IOLineFallingEdge, Sub(line, device, changeType)
                                                          Console.WriteLine("Button " & line.IONumber & " Pressed")
                                                          lights(line.IONumber).Value = 1 'turn on light
                                                      End Sub

                'attach anonymous event handler to rising edge of all buttons
                'when the button is released the corresponding light will turn off
                AddHandler buttons.IOLineRisingEdge, Sub(line, device, changeType)
                                                         Console.WriteLine("Button " + line.IONumber + " Released")
                                                         lights(line.IONumber).Value = 0 'turn off light
                                                     End Sub

                'open connection, can be opened before or after events are attached
                ed.Connect()

                'initialise all lights off
                lights.Values = 0

                Console.WriteLine("Press any key to exit...")
                Console.ReadKey()

                'close connections after a key press, automatic disconnect and dispose outside of using block
            End Using
        Catch ex As Exception
            Console.WriteLine("Exception")
            Console.WriteLine(ex.ToString())
            Console.WriteLine("Press any key to exit...")
            Console.ReadKey()
        End Try
    End Sub

End Module
FAQs