Brainboxes provides a .NET API which allows easy integration of Brainboxes Remote IO modules into your Windows software applications. The following sample code demonstrates how to use the Brainboxes.IO api to create a simple windows forms application using C#.

Requirements

C# Code Sample

The code sample can be used to create a windows forms application using Visual Studio and produces an application like the one pictured:

Windows Forms application written in C#

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

/*
This file is part of the C# example/library code for communication with with 
Brainboxes Ethernet-attached data acquisition and control products, and is 
provided by Brainboxes Limited.  Examples in other programming languages are 
also available.
Visit http://www.brainboxes.com to see our range of Brainboxes Ethernet-
attached data acquisition and control products, and to check for updates to
this code package.

This is free and unencumbered software released into the public domain.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Brainboxes.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace Brainboxes.IO.Samples
{
    /// 
    /// Create a simple Graphical User Interface which allows a user to 
    /// send and recieve ASCII commands to/from a Brainboxes Ethernet to DIO Device
    /// using either a TCP connection or a virtual com port.
    /// Note the to use a virtual com port first the port must be installed using
    /// Brainboxes Boost.IO Manager
    /// 
    public partial class EDConsole : Form
    {
        private EDDevice EDDevice;

        public EDConsole()
        {
            InitializeComponent();

            EDDevice = new EDDevice(null, new ASCIIProtocol());

            WriteLine("Choose a method to connect to the Brainboxes ED Device");
        }

        /// 
        /// Add a message to the console (which is a Windows.Form.TextBox)
        /// Scroll the text box down to the newly added message
        /// 
        /// 
        public void Write(string message)
        {
            this.terminal.AppendText(message);
            //scroll to bottom
            this.terminal.SelectionStart = this.terminal.Text.Length;
            this.terminal.ScrollToCaret();
            this.Refresh();
        }

        /// 
        /// Same as Write but adds a newline to the end of the string
        /// 
        /// 
        public void WriteLine(string line)
        {
            this.Write(line + "\r\n");
        }

        /// 
        /// Attempt to connect to the ED device using the settings
        /// specified by the user
        /// 
        protected void connect()
        {
            this.tcpSettingsPanel.Visible = false;
            this.serialSettingsPanel.Visible = false;
            try
            {
                WriteLine("Connecting...");
                this.EDDevice.Connect();
                WriteLine("Connected !");
                this.sendCommandPanel.Enabled = true;
                this.disconnectButton.Visible = true;
            }
            catch (Exception e)
            {
                WriteLine("Connection Error: " + e.Message);
                this.tcpSettingsPanel.Visible = true;
                this.serialSettingsPanel.Visible = true;
            }
        }

        /// 
        /// Disconnect from the ED device
        /// 
        protected void disconnect()
        {
            this.sendCommandPanel.Enabled = false;

            try
            {
                WriteLine("Disconnecting...");
                this.EDDevice.Disconnect();
                WriteLine("Disconnected !");
            }
            catch (Exception e)
            {
                WriteLine("Disconnect Error: " + e.Message);
            }
            finally
            {
                this.tcpSettingsPanel.Visible = true;
                this.serialSettingsPanel.Visible = true;
                this.disconnectButton.Visible = false;
            }
 
        }

        /// 
        /// Try to send the command in the ASCIICommand Text box to the
        /// ED Device and display the response
        /// 
        private void sendCommand()
        {
            try
            {
                this.sendCommandButton.Enabled = false;
                WriteLine("TX <== " + this.ASCIICommand.Text);
                string receiveCommand = this.EDDevice.SendCommand(this.ASCIICommand.Text);
                if (receiveCommand == null)
                {
                    WriteLine("This command does not have a response.");
                }
                else
                {
                    WriteLine("RX ==> " + receiveCommand);
                }
            }
            catch (Exception e)
            {
                WriteLine("Send Command Error: " + e.Message);
            }
            finally
            {
                this.sendCommandButton.Enabled = true;
            }
        }

        /// 
        /// If the user clicks the "send Command" button then attempt
        /// to send command to the ED device
        /// 
        /// 
        /// 
        protected void button1_Click(object sender, EventArgs e)
        {
            this.sendCommand();
        }

        /// 
        /// If the user presses the enter key when in the ASCII command text box
        /// then attempt to send the command
        /// 
        /// 
        /// 
        private void ASCIICommand_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                this.sendCommand();
            }
        }

        /// 
        /// Attempt to connect to the ED device over TCP using the settings
        /// supplied
        /// 
        /// 
        /// 
        private void tcpConnectButton_Click(object sender, EventArgs e)
        {
            WriteLine("");
            WriteLine("Connecting over TCP IP");
            string IPAddress = ipAddressTextBox1.Text + "." + ipAddressTextBox2.Text + "." + ipAddressTextBox3.Text + "." + ipAddressTextBox4.Text;
            int port = Convert.ToInt32(portNumberTextBox.Text);
            WriteLine("Address: " + IPAddress + ":" + port.ToString());
            WriteLine("Default connection timeout if address not found: 20 seconds");
            this.EDDevice.Connection = new TCPConnection(IPAddress, port);
            this.connect();
        }

        /// 
        /// Attempt to connect over serial com port
        /// note the ED device virtual com must be installed
        /// using Brainboxes Boost.IO Manager
        /// 
        /// 
        /// 
        private void serialConnectButton_Click(object sender, EventArgs e)
        {
            WriteLine("");
            WriteLine("Connecting over Serial");
            string comText = comPortComboBox.SelectedItem != null ? comPortComboBox.SelectedItem.ToString() : comPortComboBox.Text;
            WriteLine(comText);
            this.EDDevice.Connection = new SerialConnection(comText, 115200);
            this.connect();
        }

        private void disconnectButton_Click(object sender, EventArgs e)
        {
            this.disconnect();
        }

        /// 
        /// When the user presses the enter key when in the TCP settings
        /// panel then attempt to connect to the ED device
        /// 
        /// 
        /// 
        private void tcpPanel_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                this.tcpConnectButton_Click(sender,e);
            }
        }

        private void comPortComboBox_DropDown(object sender, EventArgs e)
        {
            //list all valid system com ports
            this.comPortComboBox.DataSource = System.IO.Ports.SerialPort.GetPortNames();
        }

        private void comPortComboBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                this.serialConnectButton_Click(sender, e);
            }
        }
    }

}