C# controlled lights via USB port

At work, we thought it would be cool if we could turn on 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.

I bought the FT245RL from SparkFun on a breakout circuit for ease of soldering. For details about chip, take a look at this DataSheet.

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

    }

}
Advertisements