1
votes

What I want to do is to create a simple Windows form app to control a Relay board to power on and off some lights. Nothing serious it is a personal project. This the link to the relay board I have : http://www.canakit.com/catalog/product/view/id/627/s/4-port-usb-relay-controller/ .

Now my question is how can I send commands to the relay and receive some from it via USB. The relay board comes with a driver. The boards support the commands below. So how do I send them to the board via my windows form.

From the user manual: It is compatible with both Windows and Apple OS X, as well as various Linux flavors and appears as a USB CDC (Communications Device Class) device which creates a Virtual Serial (COM) port allowing easy communication with the board through any programming language that supports serial communications (VB, VB.NET, C#, C, C++, Perl, Java, etc). A complete set of easy to use commands are available for complete control of all relays, I/O channels and sensors.

Supported Commands:

RELx.ON
RELx.OFF
RELx.TOGGLE
RELx.GET

RELS.ON
RELS.OFF
RELS.GET

CHx.ON
CHx.OFF
CHx.TOGGLE
CHx.GET
CHx.SETMODE
CHx.GETANALOG
CHx.GETTEMP

CHS.ON
CHS.OFF
CHS.GET
CHS.SETTEMPRES
3

3 Answers

1
votes

Plugin the device once you have installed the driver. Open device manager where you should now see a COM port as given by board manufacturer. You could have the COM port, baud rate, parity etc. provided in the documentation. Now you can create a c# console application with following code, I have made a sample to issue a RELx.ON followed by a RELx.OFF after a 3 second gap:

using System;
using System.IO.Ports;

namespace RelayConsole
{
    class Program
    {
        public static SerialPort port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
/* Replace COM5 with your COM port, 9600 with baud rate of your board, parity and bits as well as per your device documentation */

        static void Main(string[] args)
        {
            SerialProgram();
        }

        static void SerialProgram()
        {
            try
            {
                port.Open();
                port.Write("RELx.ON"); /* You can pass any command as per your documentation in port.Write */
                System.Threading.Thread.Sleep(3000);
                port.Write("RELx.OFF");
                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                if(ex is System.IO.IOException)
                {
                    Console.WriteLine("Port Exception: " + ex.ToString());
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("General Exception: " + ex.ToString());
                    Console.ReadLine();
                }
            }
        }
    }
}
0
votes

Looking at the instruction manual, the drivers give you serial port access (but no direct USB api), so you'll have to open a serial port to the relay board.

First off (if you havn't already), follow the instructions under Communicating with the Controller in the manual and use something like HyperTerminal to make sure you can 'talk' to the board.

Then have a look at these examples for simple serial comms from C#:

http://csharp.simpleserial.com/

http://msmvps.com/blogs/coad/archive/2005/03/23/SerialPort-_2800_RS_2D00_232-Serial-COM-Port_2900_-in-C_2300_-.NET.aspx

0
votes

You should use the System.IO.Ports.SerialPort class.

If you already saw the documentation of that class and are having trouble using it, please elaborate on what problems you are having.