using System; using System.Runtime.InteropServices; using System.IO; using System.Threading; namespace Test { public class OpenDMX { public static byte[] buffer; public static uint handle; public static bool done = false; public static int bytesWritten = 0; public static FT_STATUS status; public const byte BITS_8 = 8; public const byte STOP_BITS_2 = 2; public const byte PARITY_NONE = 0; public const UInt16 FLOW_NONE = 0; public const byte PURGE_RX = 1; public const byte PURGE_TX = 2; [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_Open(UInt32 uiPort, ref uint ftHandle); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_Close(uint ftHandle); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_Read(uint ftHandle, IntPtr lpBuffer, UInt32 dwBytesToRead, ref UInt32 lpdwBytesReturned); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_Write(uint ftHandle, IntPtr lpBuffer, UInt32 dwBytesToRead, ref UInt32 lpdwBytesWritten); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_SetDataCharacteristics(uint ftHandle, byte uWordLength, byte uStopBits, byte uParity); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_SetFlowControl(uint ftHandle, char usFlowControl, byte uXon, byte uXoff); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_GetModemStatus(uint ftHandle, ref UInt32 lpdwModemStatus); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_Purge(uint ftHandle, UInt32 dwMask); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_SetBreakOn(uint ftHandle); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_SetBreakOff(uint ftHandle); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_GetStatus(uint ftHandle, ref UInt32 lpdwAmountInRxQueue, ref UInt32 lpdwAmountInTxQueue, ref UInt32 lpdwEventStatus); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_ResetDevice(uint ftHandle); [DllImport("FTD2XX.dll")] public static extern FT_STATUS FT_SetDivisor(uint ftHandle, char usDivisor); public static void start() { buffer = new byte[4]; // can be any length up to 512. The shorter the faster. handle = 0; status = FTD.FT_Open(0, ref handle); Thread thread = new Thread(new ThreadStart(writeData)); thread.Start(); setDmxValue(1, 255); // set DMX channel 1 to maximum value } public static void setDmxValue(int channel, byte value) { buffer[channel]=value; } public static void writeData() { while (!done) { initOpenDMX(); FTD.FT_SetBreakOn(handle); FTD.FT_SetBreakOff(handle); bytesWritten = FTD.write(handle, buffer, buffer.Length); System.Threading.Thread.Sleep(50); } } public static int write(uint handle, byte[] data, int length) { IntPtr ptr = Marshal.AllocHGlobal((int)length); Marshal.Copy(data, 0, ptr, (int)length); uint bytesWritten = 0; FTD.FT_Write(handle, ptr, (uint)length, ref bytesWritten); return (int)bytesWritten; } public static void initOpenDMX() { status = FTD.FT_ResetDevice(handle); status = FTD.FT_SetDivisor(handle, (char)12); // set baud rate status = FTD.FT_SetDataCharacteristics(handle, FTD.BITS_8, FTD.STOP_BITS_2, FTD.PARITY_NONE); status = FTD.FT_SetFlowControl(handle, (char)FTD.FLOW_NONE, 0, 0); status = FTD.FT_ClrRts(handle); status = FTD.FT_Purge(handle, FTD.PURGE_TX); status = FTD.FT_Purge(handle, FTD.PURGE_RX); } } /// /// Enumaration containing the varios return status for the DLL functions. /// public enum FT_STATUS2 { FT_OK = 0, FT_INVALID_HANDLE, FT_DEVICE_NOT_FOUND, FT_DEVICE_NOT_OPENED, FT_IO_ERROR, FT_INSUFFICIENT_RESOURCES, FT_INVALID_PARAMETER, FT_INVALID_BAUD_RATE, FT_DEVICE_NOT_OPENED_FOR_ERASE, FT_DEVICE_NOT_OPENED_FOR_WRITE, FT_FAILED_TO_WRITE_DEVICE, FT_EEPROM_READ_FAILED, FT_EEPROM_WRITE_FAILED, FT_EEPROM_ERASE_FAILED, FT_EEPROM_NOT_PRESENT, FT_EEPROM_NOT_PROGRAMMED, FT_INVALID_ARGS, FT_OTHER_ERROR }; }