/// <summary> /// Returns the index of the first available FTDI 232H device found in the system /// </summary> /// <returns></returns> static public int GetFirstDevIndex() { FTDI ftdi = new FTDI(); int count = 10; FTDI.FT_DEVICE_INFO_NODE[] devlist = new FTDI.FT_DEVICE_INFO_NODE[count]; FTDI.FT_STATUS status = ftdi.GetDeviceList(devlist); if (status != FTDI.FT_STATUS.FT_OK) { throw new FTI2CException("Problem getting FTDI device list"); } int index = -1; for (int i = 0; i < count; i++) { FTDI.FT_DEVICE_INFO_NODE devinfo = devlist[i]; if (devinfo != null) { if (devinfo.Type == FTD2XX_NET.FTDI.FT_DEVICE.FT_DEVICE_232H) { index = i; FTDI.FT_DEVICE device_type = devinfo.Type; break; } } } return(index); }
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 void PrintDeviceData() { Console.WriteLine(); uint deviceID = 0; FTDI.FT_DEVICE ftDevice = new FTDI.FT_DEVICE(); string des; device.GetDeviceID(ref deviceID); device.GetDeviceType(ref ftDevice); device.GetDescription(out des); Console.WriteLine("Device ID: " + deviceID); Console.WriteLine("Device Type: " + ftDevice); Console.WriteLine("Device Description: " + des); Console.WriteLine(); }
static extern FTDI.FT_STATUS FT_GetDeviceInfoDetail(UInt32 index, ref UInt32 flags, ref FTDI.FT_DEVICE chiptype, ref UInt32 id, ref UInt32 locid, byte[] serialnumber, byte[] description, ref IntPtr ftHandle);
/// <summary> /// Find the FDTI chip, connect, set baud to 9600, set sync bit-bang mode /// </summary> /// <returns></returns> public bool Connect() { // Determine the number of FTDI devices connected to the machine this.ftStatus = ftdiDevice.GetNumberOfDevices(ref this.ftdiDeviceCount); // Check status if (this.ftStatus == FTDI.FT_STATUS.FT_OK) { PackageHost.WriteInfo("Number of FTDI devices: {0}", this.ftdiDeviceCount); } else { PackageHost.WriteError("Failed to get number of devices (error {0})", this.ftStatus); return(false); } // Check device count if (ftdiDeviceCount == 0) { PackageHost.WriteError("No relay board found, please try again"); return(false); } else { if (PackageHost.ContainsSetting("SerialNumber") && !string.IsNullOrEmpty(PackageHost.GetSettingValue("SerialNumber"))) { // Open device by serial number this.ftStatus = this.ftdiDevice.OpenBySerialNumber(PackageHost.GetSettingValue("SerialNumber")); } else { // Open the first device do { this.ftStatus = this.ftdiDevice.OpenByIndex(deviceId++); }while (this.ftStatus != FTDI.FT_STATUS.FT_OK && deviceId < ftdiDeviceCount); } // Get serial number if (this.ftStatus == FTDI.FT_STATUS.FT_OK) { this.ftStatus = this.ftdiDevice.GetSerialNumber(out serialNumber); this.ftStatus = this.ftdiDevice.GetCOMPort(out portCOM); this.ftStatus = this.ftdiDevice.GetDescription(out description); this.ftStatus = this.ftdiDevice.GetDeviceID(ref deviceId); this.ftStatus = this.ftdiDevice.GetDriverVersion(ref driverVersion); FTDI.FT_DEVICE deviceType = FTDI.FT_DEVICE.FT_DEVICE_UNKNOWN; this.ftStatus = this.ftdiDevice.GetDeviceType(ref deviceType); this.deviceType = deviceType.ToString(); } // Check status if (ftStatus == FTDI.FT_STATUS.FT_OK) { PackageHost.WriteInfo("Connected on device '{0}'", this.SerialNumber); } else { PackageHost.WriteError("Unable to connect to the relay board"); return(false); } // Set Baud rate to 9600 this.ftStatus = this.ftdiDevice.SetBaudRate(BAUD_RATE); // Set FT245RL to synchronous bit-bang mode, used on sainsmart relay board this.ftdiDevice.SetBitMode(0xFF, FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG); // Switch off all the relays this.ftdiDevice.Write(startup, 1, ref bytesToSend); // Ok ! return(true); } }