How do I use C# to communicate with my Remote 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

Brainboxes.IO C# API in visual studio

C# for .net Framework 2.0 and above

The following code works in C# targeted at .net version 2.0 and above (also 100% compatible with .NET Core):

using System;
using Brainboxes.IO;

namespace Brainboxes.IO.Samples.CSharpConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            //tcp connection
            IConnection connection = new TCPConnection("192.168.0.158");
            //or serial connection on COM3
            //note serial connections only work in Windows using brainboxes virtual com port driver. In Linux you must use TCP
            //connection = new SerialConnection(3);

            //ED588 is the model number of the device could be e.g. ED038, ED204 etc.
            EDDevice ed = new ED588(connection);
            ed.Label = "Pump Control in Cabinet 3"; //give a label, useful for debugging

            //open connection
            ed.Connect();

            //read value of Digital Output Dout 0
            int value = ed.Outputs[0].Value;
            Console.WriteLine("Digital output 0 is "+value);

            //read value of Digital Input Din 7
            Console.WriteLine("Digital input 7 is "+ed.Inputs[7].Value);

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

            //set Dout0 closed/on/1
            ed.Outputs[0].Value = 1;

            //optionally label a line for ease when debugging
            ed.Inputs[3].Label = "Pump enabled Monitor";

            //add event listener for rising edge on Din3
            ed.Inputs[3].IOLineRisingEdge += DIN3IOLineRisingEdge;

            //add event listener for falling edge on any of the inputs
            ed.Inputs.IOLineFallingEdge += Inputs_IOLineFallingEdge;

            //add event listener for any change on any of the inputs or outputs of the device
            ed.IOLineChanged += ed_IOLineChanged;

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

            //close connections after a key press
            ed.Disconnect();
        }

        //called when DIN3 goes from 0 -> 1
        private static void DIN3IOLineRisingEdge(IOLine line, EDDevice device, IOChangeTypes changeType)
        {
            Console.WriteLine(line +" rising edge!");
            //or for full debug data about line:
            Console.WriteLine(line.Describe());
        }

        //called when any of the inputs goes from 1 -[gt] 0
        private static void Inputs_IOLineFallingEdge(IOLine line, EDDevice device, IOChangeTypes changeType)
        {
            Console.WriteLine(line + " falling edge!");
        }

        //called when any change occurs on any of the inputs or output
        private static void ed_IOLineChanged(IOLine line, EDDevice device, IOChangeTypes changeType)
        {
            Console.WriteLine(line + " of device "+ device);

            //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());
        }
    }
}

C# for .NET Core .net Framework 3.5 and above

The following code works in C# targeted at .NET CORE (1.0 and above) and .net Framework version 3.5 and above

using System;
using System.Linq;
using Brainboxes.IO;

//The following code works in C# targeted at .net version 3.5 and above and uses LINQ & lambdas
namespace Brainboxes.IO.Samples.CSharpConsoleLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            //The following factory method will connect to any brainboxes ED device
            //and return the correct object instance for the device
            //The create method automatically open a connection to the device
            //note serial connections only work in Windows using brainboxes virtual com port driver. In Linux you must use TCP
            using (EDDevice ed = EDDevice.Create("192.168.0.158") )
            {

            //for alternative to the factory method see the example above


                ed.Label = "Pump Control in Cabinet 3"; //give it a label, useful for debugging

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

                //first 3 outputs are connected to lights
                IOList lights = ed.Outputs.Take(3).AsIOList();

                //see if light 0 is currently on
                Console.WriteLine("Light 0 is "+(lights[0].Value == 1 ? "on" : "off"));

                //attach event handler to falling edge of all buttons
                //when the button is turned on the corresponding light will come on
                buttons.IOLineFallingEdge += (line, device, changeType) =>
                {
                    Console.WriteLine("Button " + line.IONumber + " Pressed");
                    lights[line.IONumber].Value = 1; //turn on light
                };

                //attach event handler to rising edge of all buttons
                //when the button is released the corresponding light will turn off
                buttons.IOLineRisingEdge += (line, device, changeType) =>
                {
                    Console.WriteLine("Button " + line.IONumber + " Released");
                    lights[line.IONumber].Value = 0; //turn off light
                };

                //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
                //in a production environment, or if a long lasting persistent connection is required
                //it is advised to create the device list the previous example
            }
        }
    }
}
FAQs