DMX USB Pro(FTDI)非常偶发

时间:2013-03-28 01:44:42

标签: c# serial-port ftdi dmx512

我一直在尝试编写一个C#应用程序,使用Enttec DMX USB Pro盒将数据发送到舞台灯光。我提供了C#包装器,我已经得到了预期的响应,但它很少有效。我似乎必须在使用现成的应用程序之间切换以“重置”连接几次,然后才能让它开始响应我的写入。

我的DMX代码是

class DmxDriver
{
    const int DMX_PACKET_SIZE = 513;
    private bool connected;
    private FTDI device;
    private int startAddr;

    private byte[] packet;
    public DmxDriver(int baseDmxAddr)
    {
        startAddr = baseDmxAddr;
        device = new FTDI();
        FTDI.FT_STATUS result =  device.OpenByIndex(0);
        if (result == FTDI.FT_STATUS.FT_OK)
        {
            connected = true;
            Console.WriteLine("DMX connected");
        }
        else
        {
            connected = false;
            Console.WriteLine("DMX CONNECTION FAILED");
        }

        packet = new byte[DMX_PACKET_SIZE];
        for (int i = 0; i < DMX_PACKET_SIZE; i++)
        {
            packet[i] = 0;
        }
    }

    ~DmxDriver()
    {
        device.Close();
    }

    public bool deviceConnected()
    {
        return connected;
    }
    public void sendData()
    {
        if (packet.Length != 513)
        {
            return;
        }
        uint written = 0;
        FTDI.FT_STATUS result;

        byte[] header = new byte[4];
        header[0] = 0x7E; //start code
        header[1] = 6; //DMX TX
        header[2] = 255 & 0xFF; //pack length logical and with max packet size
        header[3] = 255 >> 8; //packet length shifted by byte length? DMX standard idk


        result = device.Write(header, 4, ref written);//send dmx header

        Console.WriteLine(result);
        Console.WriteLine(written);


        result = device.Write(packet, 513, ref written);//send data array
        Console.WriteLine(result);
        Console.WriteLine(written);


        byte[] endcode = new byte[1];
        endcode[0] = 0xE7;
        device.Write(endcode, 1, ref written);//send dmx end code

    }

0 个答案:

没有答案