C# controlled lights via USB port

November 15, 2011 at 11:30 am (Computer Science, Personal)

At work, we thought it would be cool if we could turn on some siren lights when a server goes down.

Step #1 to make this happen was to be able to control simple electronics from custom code. I love C# and the USB port is what is common now so I researched and found the FT245RL “USB to FIFO” chip. This chip made it really easy for me. It plays well with drivers known by Windows.

When I plugged in the chip to the USB port, Windows 7 immediately recognized it and installed the drivers for it. You can use a couple of drivers the default is the simpler one to use where it turns the USB port into a virtual serial (COM) port.

Writing to serial ports is really standard so this is the code that outputs a byte through the chip:

private static void WriteByte(byte byteToWrite)
{
    using (SerialPort vcp = new SerialPort())
    {
        vcp.BaudRate = 9600;
        vcp.DataBits = 8;
        vcp.StopBits = StopBits.One;
        vcp.Parity = Parity.None;
        vcp.PortName = "COM5";

        vcp.Open();

        vcp.Write(new byte[] { byteToWrite }, 0, 1);

        vcp.Close();

    }

}

Permalink 2 Comments

Follow

Get every new post delivered to your Inbox.