How do I use C# 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

C# 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).

using System.Linq;
using Brainboxes.IO;
namespace Analog_io_example_application
{

    //This sample code will demonstrate a simple variable brightness bulb controlled by a dial
    //The dials are connected to ED-549 analog input device
    //The variable brightness bulbs are connected to ED-560 analog output device
    class Program
    {
        static IOList lights; //declared here to enable using 'lights' object outside the main program
        static void Main(string[] args)
        {
            using (EDDevice edIn = new ED549(new TCPConnection("192.168.0.77")))//ip address of ED-549
            {
                using (EDDevice edOut = new ED560(new TCPConnection("192.168.0.89")))//ip address of ED-560
                {
                    edIn.Connect();
                    edOut.Connect();
                    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 the default output format of 'Engineering Units'
                     lights= edOut.AOutputs.Take(3).AsIOList();
                    //initialise lights off
                    lights.AValues = 0;
                    edIn.Label = "Light intensity controller";//give a label useful for debugging
                    //first 3 inputs are connected to dials
                    //use the default full scale range of -10V -> +10V (note: can be configured to many other ranges)
                    IOList dials = edIn.AInputs.Take(3).AsIOList();
                    dials.Label = "Console Dials"; //optionally label group, useful for debugging
                    Console.WriteLine("Dial 0 current value " + dials[0].Value);
                    Console.WriteLine("Dial 1 current value " + dials[1].Value);
                    Console.WriteLine("Dial 2 current value " + dials[2].Value);
                    //writing event handler function
                    //when the light intensity is above a target level, prompt the user
                    AIOLineChangedEventHandler lightsTooBrightEventHandler = (line, device, value, changeType) =>
                    {
                        //previous sampled value was below the target value and current sampled value is above the target value
                        if(changeType == AIOChangeTypes.Above)
                        {
                            Console.WriteLine("Light is too bright on output " + line.IONumber);
                        }
                    };
                    //attaching function to event handler
                    AIOLineChangedEventHandler dialTurnedEventHandler = DialTurnedEventHandlerFunction;
                    AIOLineChangedEventHandler optimumBrightnessLevelEventHandler = OptimumBrightnessLevelEventHandlerFunction;
                    //subscribe to event handler on lights
                    //event triggered when the light output touches 3 or 5
                    lights.SubscribeToTargetRangeEvent(ref optimumBrightnessLevelEventHandler, 4, 1);
                    //event triggered when the light output touches 6.5
                    lights.SubscribeToTargetEvent(ref lightsTooBrightEventHandler, 6.5);
                    //subscribe to event on dials
                    dials.SubscribeToDeltaEvent(ref dialTurnedEventHandler, 1);//event triggered when the delta changes by 1
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
            }
        }
        //event handler function outside the main program
        //when the dial is turned by greater than delta value update the corresponding light output
        private static void DialTurnedEventHandlerFunction(IOLine line, EDDevice device, double value, AIOChangeTypes changeType)
        {
            Console.WriteLine("Analog input " + line.IONumber + " changed by greater than the delta of " + value);
            //turn lights to new output value for the corresponding input
            lights[line.IONumber].AValue = line.AValue * 0.25;//0.25 is a conversion factor
        }
        //when the light intensity enters or exits the optimum brightness level, prompt the user
        private static void OptimumBrightnessLevelEventHandlerFunction(IOLine line, EDDevice device, double value, AIOChangeTypes changeType)
       {
            //previous sampled value was outside the delta range of the target value and
            //current sampled value is inside the delta range of the target value
            if(changeType == AIOChangeTypes.Enter)
            {
                Console.WriteLine("Entering optimum brightness level on output " + line.IONumber);
            }
            //previous sampled value was inside the delta range of the target value and
            //current sampled value is outside the delta range of the target value
            else if (changeType == AIOChangeTypes.Exit)
            {
                Console.WriteLine("Exiting optimum brightness level on output " + line.IONumber);
            }
        }
    }
}

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

using System;
using Brainboxes.IO;

namespace Analog_demo_feedback_control_system
{

    class Program
    {
        static ED560 edOut;
        static double flowRateConversionFactor = 5.0d / (20 - 4);
        static double flowRateOffset = -1.25d;
        static void Main(string[] args)
        {
            using (ED549 edIn = new ED549(new TCPConnection("192.168.0.77")))
            {
                using (ED560 edOut = new ED560(new TCPConnection("192.168.0.89")))
                {
                    edOut.Connect();
                    edIn.Connect();
                    //optionally provide a label, useful for debugging
                    IOLine flowMeter = edIn.AInputs[0];
                    flowMeter.Label = "Flow Meter";
                    IOLine flowValve = edOut.AOutputs[0];
                    flowValve.Label = "Flow Valve";
                    // define events handlers:
                    //attaching function to event handler by referencing a function outside of this function block
                    AIOLineChangedEventHandler maintainFlowRateHandler = MaintainFlowRateHandlerFunction;
                    //or define a event within this 'main' function block
                    AIOLineChangedEventHandler flowRateChangedEvent = (line, device, value, changeTypes) =>
                    {
                        //for every change of 0.1 m/s update the console to display the current flow rate in m/s
                        Console.WriteLine("The Flow rate is: " + (line.AValue * flowRateConversionFactor + flowRateOffset) + " m/s");
                    };
                    //subscribe to event handlers:
                    //for every change of 0.1m/s update the console to display the current flow rate in m/s
                    flowMeter.SubscribeToDeltaEvent(ref flowRateChangedEvent, ConvertToMilliAmps(0.1));
                    //the aim of the system is to maintain a flow rate of 3 m/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(ref maintainFlowRateHandler,
                                                          ConvertToMilliAmps(3),
                                                          ConvertToMilliAmps(0.2));
                }
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }
        //to find the flow meter input(in mA) for a given flow rate (in m/s)
        private static double ConvertToMilliAmps(double flowRate)
        {
            return ((flowRate - flowRateOffset) / flowRateConversionFactor);
        }

            //as the flow rate nears the target value adjust the valve to maintain flow rate at 3 m/s
        private static void MaintainFlowRateHandlerFunction(IOLine line, EDDevice device, double value, AIOChangeTypes changeTypes)
        {
            //difference between current input and optimum value in mA
            //multiply difference by conversion factor and add it to the exisiting output value
            edOut.AOutputs[0].AValue = (ConvertToMilliAmps(3) - line.AValue) * (20 / 16) + edOut.AOutputs[0].AValue;
        }
    }
}
FAQs