bool DMXThread::connectDMX() { if(m_dmxConnected == true) { return true; } wchar_t buf[64]; FT_ListDevices(0, buf, FT_LIST_BY_INDEX | FT_OPEN_BY_DESCRIPTION); m_ftHandle = FT_W32_CreateFile(buf, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FT_OPEN_BY_DESCRIPTION, 0); // connect to first device if (m_ftHandle == INVALID_HANDLE_VALUE) { m_error = tr("No DMX device was found"); return false; } FTDCB ftDCB; if (FT_W32_GetCommState(m_ftHandle, &ftDCB)) { // FT_W32_GetCommState ok, device state is in ftDCB ftDCB.BaudRate = 250000; ftDCB.Parity = FT_PARITY_NONE; ftDCB.StopBits = FT_STOP_BITS_2; ftDCB.ByteSize = FT_BITS_8; ftDCB.fOutX = false; ftDCB.fInX = false; ftDCB.fErrorChar = false; ftDCB.fBinary = true; ftDCB.fRtsControl = false; ftDCB.fAbortOnError = false; if (!FT_W32_SetCommState(m_ftHandle,&ftDCB)) { m_error = tr("The baud rate of the DMX device could not be set."); return false; } } else { m_error = tr("The DMX device could not be initialized"); return false; } FT_W32_PurgeComm(m_ftHandle, FT_PURGE_TX | FT_PURGE_RX); m_dmxConnected = true; return true; }
int dev_open_uart (int n_dev_indx, FT_HANDLE *ph_device) { FT_STATUS ft_status; DWORD dw_num_devs; LONG devLocation; ft_status = FT_ListDevices(&dw_num_devs, NULL, FT_LIST_NUMBER_ONLY); if (ft_status != FT_OK) return FALSE; if (dw_num_devs == 0){ // No devices were found return FALSE; } ft_status = FT_ListDevices((void*)n_dev_indx, &devLocation, FT_LIST_BY_INDEX | FT_OPEN_BY_LOCATION); if (ft_status != FT_OK) { return FALSE; } ft_status |= FT_ListDevices((void*)n_dev_indx, &devDescriptor, FT_LIST_BY_INDEX | FT_OPEN_BY_DESCRIPTION); ft_status |= FT_ListDevices((void*)n_dev_indx, &devSerial, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER); if (ft_status != FT_OK){ return FALSE; } #define FT_Classic 0 #if FT_Classic ft_status |= FT_OpenEx((void*)devLocation, FT_OPEN_BY_LOCATION, ph_device); ft_status |= FT_SetTimeouts(*ph_device, 500, 500); ft_status |= FT_SetLatencyTimer(*ph_device, 2); // Divisor selection // BAUD = 3000000 / Divisor // Divisor = (N + 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875) // Divisor = 24 ==> Baud 125000 ft_status |= FT_SetDivisor(*ph_device, 3000000 / 125000); // Set UART format 8N1 ft_status |= FT_SetDataCharacteristics(*ph_device, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if (ft_status != FT_OK){ return FALSE; } // Just in case FT_Purge(*ph_device, FT_PURGE_TX | FT_PURGE_RX); #else // Open a device for overlapped I/O using its serial number *ph_device = FT_W32_CreateFile( (LPCTSTR)devLocation, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED | FT_OPEN_BY_LOCATION, 0); if (*ph_device == INVALID_HANDLE_VALUE) { // FT_W32_CreateDevice failed return FALSE; } // ---------------------------------------- // --- Set comm parameters // ---------------------------------------- FTDCB ftDCB; FTTIMEOUTS ftTimeouts; FTCOMSTAT ftPortStatus; DWORD dw_port_error; if (!FT_W32_GetCommState(*ph_device, &ftDCB)) { return FALSE; } // Divisor selection // BAUD = 3000000 / Divisor // Divisor = (N + 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875) // Divisor = 24 ==> Baud 125000 ftDCB.BaudRate = 38400; ftDCB.fBinary = TRUE; /* Binary Mode (skip EOF check) */ ftDCB.fParity = FALSE; /* Enable parity checking */ ftDCB.fOutxCtsFlow = FALSE; /* CTS handshaking on output */ ftDCB.fOutxDsrFlow = FALSE; /* DSR handshaking on output */ ftDCB.fDtrControl = DTR_CONTROL_DISABLE; /* DTR Flow control */ ftDCB.fTXContinueOnXoff = FALSE; ftDCB.fErrorChar = FALSE; // enable error replacement ftDCB.fNull = FALSE; // enable null stripping ftDCB.fRtsControl = RTS_CONTROL_DISABLE; // RTS flow control ftDCB.fAbortOnError = TRUE; // abort reads/writes on error ftDCB.fOutX = FALSE; /* Enable output X-ON/X-OFF */ ftDCB.fInX = FALSE; /* Enable input X-ON/X-OFF */ ftDCB.fNull = FALSE; /* Enable Null stripping */ ftDCB.fRtsControl = RTS_CONTROL_DISABLE; /* Rts Flow control */ ftDCB.fAbortOnError = TRUE; /* Abort all reads and writes on Error */ // 8N1 ftDCB.ByteSize = 8; /* Number of bits/byte, 4-8 */ ftDCB.Parity = NOPARITY; /* 0-4=None,Odd,Even,Mark,Space */ ftDCB.StopBits = ONESTOPBIT; /* 0,1,2 = 1, 1.5, 2 */ if (!FT_W32_SetCommState(*ph_device, &ftDCB)) { return FALSE; } FT_W32_GetCommState(*ph_device, &ftDCB); // Set serial port Timeout values FT_W32_GetCommTimeouts(*ph_device, &ftTimeouts); ftTimeouts.ReadIntervalTimeout = 0; ftTimeouts.ReadTotalTimeoutMultiplier = 0; ftTimeouts.ReadTotalTimeoutConstant = 200; ftTimeouts.WriteTotalTimeoutConstant = 0; ftTimeouts.WriteTotalTimeoutMultiplier = 0; FT_W32_SetCommTimeouts(*ph_device, &ftTimeouts); FT_W32_ClearCommError(*ph_device, &dw_port_error, &ftPortStatus); FT_W32_PurgeComm(*ph_device, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_RXABORT | PURGE_TXABORT); #endif // End of W32 device init return TRUE; }