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 2.0 and above

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

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

            //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 -> 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 3.5 and above

The following code works in C# targeted at .net 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
            using (EDDevice ed = EDDevice.Create("192.168.0.158") )
            {
                ed.Label = "Pump Control in Cabinet 3"; //give it a label, useful for debugging

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

                //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
            }
        }
    }
}