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

FAQs

Brainboxes provides a .NET API which allows easy integration of Brainboxes Remote IO modules into your Windows software applications.

Requirements

VB Code Samples

Example 1: Variable Light Intensity Controller

This sample code will demonstrate a simple variable brightness bulb controlled by a dial. The dials are connected to ED-549 (analog input device) and the variable brightness bulbs are connected to ED-560 (analog output device).

Imports Brainboxes.IO

Module Module1
    Dim lights 'declared here to enable using lights object outside the main sub routine'

    'This sample code will demonstrate a simple variable brightness bulb controlled by a dial'
    'The dial is connected to ED-549 analog input device'
    'The bulbs are connected to ED-560 analog output device'
    Sub Main()
        Using edIn As EDDevice = EDDevice.Create("192.168.0.77") 'ip address of ED-549'

            Using edOut As EDDevice = EDDevice.Create("192.168.0.89") 'ip address of ED-560'

                edOut.Label = "Lights" 'give a label useful for debugging'

                'first 3 outputs are connected to variable outputs (lights with intensity)'
                'use the default output full scale range of 0-10V (note: can be configured to 0-20mA or 4-20mA also)'
                'use default data format of Engineering units'

                Dim lights As IOList(Of IOLine)
                lights = edOut.AOutputs.Take(3).AsIOList()

                'initialise lights off'
                lights.AValues = 0

                edIn.Label = "Light intensity controllers" 'give a label useful for debugging'

                'first 3 inputs are connected to dials'
                'use the default full scale range -10V -> +10V (note: can be configured to many other ranges)'
                'use the default output format of Engineering Units'
                Dim dials As IOList(Of IOLine)
                dials = edIn.AInputs.Take(3).AsIOList

                Console.WriteLine("Dial 0 current value " & dials(0).AValue)
                Console.WriteLine("Dial 1 current value " & dials(1).AValue)
                Console.WriteLine("Dial 2 current value " & dials(2).AValue)

                'attaching function to event handler'
                Dim DialTurnedEventHandler As AIOLineChangedEventHandler
                DialTurnedEventHandler = AddressOf DialTurnedEventHandlerFunction
                Dim LightsTooBrightEventHandler As AIOLineChangedEventHandler
                LightsTooBrightEventHandler = AddressOf LightTooBrightEventHandlerFunction
                Dim OptimumBrightnessEventHandler As AIOLineChangedEventHandler
                OptimumBrightnessEventHandler = AddressOf OptimumBrightnessLevelEventHandlerFunction



                'subscribe to event handler on dials'
                dials.SubscribeToDeltaEvent(DialTurnedEventHandler, 1) 'event triggered when the delta value changes by 1'

                'subscribe to event handler on lights'
                lights.SubscribeToTargetEvent(LightsTooBrightEventHandler, 6.5) 'event triggered when the light output touches 6.5'
                'event triggered when the light output touches 3 or 5'
                lights.SubscribeToTargetRangeEvent(OptimumBrightnessEventHandler, 4, 1)

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

    End Sub

    'Event handler functions'
    'when the dial is turned by greater than the delta value update the corresponding light output'
    Sub DialTurnedEventHandlerFunction(line As IOLine, device As EDDevice, value As Double, changetype As AIOChangeTypes)
        Console.WriteLine("Analog input " & line.IONumber & " changed by greater than the delta of " & value)
        'turn lights to new value for the corresponding light input'
        lights(line.IONumber).AValue = line.AValue * 0.25 '0.25 is a conversion factor'
    End Sub

    'when the light intensity is above a target level, prompt the user'
    Sub LightTooBrightEventHandlerFunction(line As IOLine, device As EDDevice, value As Double, changetype As AIOChangeTypes)
        'previous sampled value was below the target value and '
        'current sample value was above the target value'
        If changetype = AIOChangeTypes.Above Then
            Console.WriteLine("Light too bright on input " & line.IONumber)
        End If
    End Sub
    'when the light output enters or exits the optimum brightness level, prompt the user'
    Sub OptimumBrightnessLevelEventHandlerFunction(line As IOLine, device As EDDevice, value As Double, changetype As AIOChangeTypes)

        'previous sample value was outside the delta range of the target value and '
        'current sample value is inside the delta range of the target value'
        If changetype = AIOChangeTypes.Enter Then
            Console.WriteLine("Entering optimum brightness level on output " & line.IONumber)

            'previous sample value was inside the delta range of the target value and '
            'current sample value is outside the delta range of the target value'
        ElseIf changetype = AIOChangeTypes.Exit Then
            Console.WriteLine("Exiting optimum brightness level on output " & line.IONumber)
        End If
    End Sub


End Module


Example 2: Water flow feedback control system

This more complicated example shows a feedback loop on a flow rate pipe. A pipe has water flow through it, which needs to be maintained at 3.0 m/s (meters/second). The pipe has a flow rate meter and a valve to control the flow rate. When the valve is fully opened the flow rate is at the pipes current flow rate (normally 5.0 m/s). When the valve is fully closed the flow rate is 0.0 m/s. The flow meter outputs 4-20mA into a brainboxes ED-549 analog input device. The flow rate meter outputs 4-20mA, for flows between 0.0 m/s and 5.0 m/s. The valve is controlled using a -10V to +10V range connected to a brainboxes ED-560 analog output device. This example shows a method to maintain the flow rate at 3.0 m/s. The flow valve is a -10V to + 10V input device, where -10V closes the value completely and +10V opens the valve completely.

When valve is fully closed:

Flow rate when the valve is fully closed: 0 m/s

Flow meter output when the valve closed: 4 mA

Input voltage to close the valve completely: -10V

When valve is fully open:

Flow rate when the valve is fully open: 5 m/s

Flow meter output when the valve fully open: 20 mA

Input voltage to open the valve completely: +10V

Valve at optimum speed:

Required optimum flow rate required: 3 m/s

Imports Brainboxes.IO

Module Module1


    Dim flowRateConversionFactor As Double = 5.0 / (20 - 4)
    Dim flowRateOffset As Double = -1.25
    Dim edOut As EDDevice

    Sub Main()
        Using edIn As EDDevice = EDDevice.Create("192.168.0.98") 'ip addresss of ED-549'
            Using edOut = EDDevice.Create("192.168.0.101") 'ip addresss of ED-560'


                Dim flowMeter As IOLine
                flowMeter = edIn.AInputs(0)
                'optionally provide a label, useful for debugging'
                flowMeter.Label = "Flow meter"

                Dim flowValve As IOLine
                flowValve = edIn.AInputs(0
                'optionally provide a label, useful for debugging'
                flowValve.Label = "Flow Valve"

                'attaching function to event handlers'
                Dim FlowrateChangedEvent As AIOLineChangedEventHandler
                FlowrateChangedEvent = AddressOf FlowrateChangedEventFunction
                Dim MaintainFlowRateEventHandler As AIOLineChangedEventHandler
                MaintainFlowRateEventHandler = AddressOf MaintainFlowRateEventHandlerFunction

                'for every change of 0.1 m/s update the console to display the current flow rate in m/s'
                flowMeter.SubscribeToDeltaEvent(FlowrateChangedEvent, ConvertoMilliAmps(0.1))

                'the aim of the system is to maintain a flow rate of 3m/s'
                'monitor the flow rate and alter the valve whenever the flow falls outside the target value by 0.2 m/s'
                'when the flow hits 2.8 m/s its going too slow, open the valve (a bit!!)'
                'when the flow hits 3.2 m/s its going too fast, close the valve (a bit!!!)'
                flowMeter.SubscribeToTargetRangeEvent(MaintainFlowRateEventHandler,
                                                 ConvertoMilliAmps(3),
                                                 ConvertoMilliAmps(0.2))

            End Using
        End Using

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

    'to find the flow meter input(in mA) for a given flow rate (in m/s)'
    Function ConvertoMilliAmps(flowRate As Double)
        Return (flowRate - flowRateOffset) / flowRateConversionFactor

    End Function

    'for every change in 0.1 m/s update the console to display the current flow rate in m/s'
    Sub FlowrateChangedEventFunction(line As IOLine, device As EDDevice, value As Double, changeTypes As AIOChangeTypes)
        Console.WriteLine("The flow rate is: " & (line.AValue * flowRateConversionFactor + flowRateOffset) & " m/s")
    End Sub

    'as the flow touches the delta range of the target value, adjust the valve to maintain the flow rate at 3 m/s'
    Sub MaintainFlowRateEventHandlerFunction(line As IOLine, device As EDDevice, value As Double, changeTypes As AIOChangeTypes)
        'difference between current input and optimum value in mA'
        'multiply difference by conversion factor and add it to the existing output value '
        edOut.AOutputs(0).AValue = (ConvertoMilliAmps(3) - line.AValue) * (20 / 16) + edOut.AOutputs(0).Value
        Console.WriteLine(edOut.AOutputs(0).AValue)
    End Sub

End Module
FAQs