static void Main(string[] args) { FTDI ftdi = new FTDI(); FTDI.FT_DEVICE_INFO_NODE[] devicelist = new FTDI.FT_DEVICE_INFO_NODE[2]; FTDI.FT_STATUS ftStatus = ftdi.GetDeviceList(devicelist); Debug.Assert(ftStatus == FTDI.FT_STATUS.FT_OK); ftStatus = ftdi.OpenByIndex(1); Debug.Assert(ftStatus == FTDI.FT_STATUS.FT_OK); if (ftdi.IsOpen) { FTDI.FT_DEVICE type = FTDI.FT_DEVICE.FT_DEVICE_UNKNOWN; ftStatus = ftdi.GetDeviceType(ref type); Debug.Assert(ftStatus == FTDI.FT_STATUS.FT_OK); Debug.Assert(type == FTDI.FT_DEVICE.FT_DEVICE_2232H); //ftdi. FTD2XX_NET.FTDI.FT2232H_EEPROM_STRUCTURE ee2232h = new FTDI.FT2232H_EEPROM_STRUCTURE(); ftStatus = ftdi.ReadFT2232HEEPROM(ee2232h); Debug.Assert(ftStatus == FTDI.FT_STATUS.FT_OK); } else { throw new Exception(); } ftStatus = ftdi.Close(); Debug.Assert(ftStatus == FTDI.FT_STATUS.FT_OK); }
public bool Initialize(out string message) // Desc: Initilize USB communication // Output: message: Message // bool: true = success, false = fail { uint ftdiDeviceCount = 0; uint i; uint myDeviceNum = 0; FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList; FTDI.FT2232H_EEPROM_STRUCTURE myEEData = new FTDI.FT2232H_EEPROM_STRUCTURE(); // Determine the number of FTDI devices connected to the machine ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount); if (ftStatus != FTDI.FT_STATUS.FT_OK) { message = "FTDI GetNumberOfDevices Failed"; return(false); } if (ftdiDeviceCount == 0) { message = "No device found"; return(false); } // Get device info ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount]; ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList); if (ftStatus != FTDI.FT_STATUS.FT_OK) { message = "FTDI GetDeviceList Failed"; return(false); } if (ftdiDeviceList[0] == null) { message = "FTDI GetDeviceList Failed"; return(false); } // Determine which device to use for (i = 0; i < ftdiDeviceCount; i++) { if (ftdiDeviceList[i].Description == "TTL232R") { myDeviceNum = i; break; } } if (i == ftdiDeviceCount) { message = "FTDI devices doesn't fit the description"; return(false); } // Open the selected device ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[myDeviceNum].SerialNumber); if (ftStatus != FTDI.FT_STATUS.FT_OK) { message = "FTDI OpenBySerialNumber Failed"; return(false); } // Setup baud rate ftStatus = myFtdiDevice.SetBaudRate(1250000); // 9600 if (ftStatus != FTDI.FT_STATUS.FT_OK) { message = "FTDI SetBaudRate Failed"; return(false); } // Set data characteristics - Data bits, Stop bits, Parity ftStatus = myFtdiDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE); if (ftStatus != FTDI.FT_STATUS.FT_OK) { message = "FTDI SetDataCharacteristics Failed"; return(false); } // Set flow control - set RTS/CTS flow control ftStatus = myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0x00, 0x00); if (ftStatus != FTDI.FT_STATUS.FT_OK) { message = "FTDI SetFlowControl Failed"; return(false); } // Set read timeout, write timeout ftStatus = myFtdiDevice.SetTimeouts(3000, 3000); if (ftStatus != FTDI.FT_STATUS.FT_OK) { message = "FTDI SetTimeouts Failed"; return(false); } // Show result message = "Initialization complete"; initialized = true; return(true); }