Example #1
0
        static void Main(string[] args)
        {
            UInt32 ftdiDeviceCount = 0;

            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;

            // Create new instance of the FTDI device class
            FTDI myFtdiDevice = new FTDI();

            // Determine the number of FTDI devices connected to the machine
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
            // Check status
            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                Console.WriteLine("Number of FTDI devices: " + ftdiDeviceCount.ToString());
                Console.WriteLine("");
            }
            else
            {
                // Wait for a key press
                Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }

            // If no devices available, return
            if (ftdiDeviceCount == 0)
            {
                // Wait for a key press
                Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }

            // Allocate storage for device info list
            FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

            // Populate our device list
            ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);

            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                for (UInt32 i = 0; i < ftdiDeviceCount; i++)
                {
                    Console.WriteLine("Device Index: " + i.ToString());
                    Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags));
                    Console.WriteLine("Type: " + ftdiDeviceList[i].Type.ToString());
                    Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID));
                    Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId));
                    Console.WriteLine("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString());
                    Console.WriteLine("Description: " + ftdiDeviceList[i].Description.ToString());
                    Console.WriteLine("");
                }
            }


            // Open first device in our list by serial number
            ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }


            // Create our device EEPROM structure based on the type of device we have open
            if (ftdiDeviceList[0].Type == FTDI.FT_DEVICE.FT_DEVICE_232R)
            {
                // We have an FT232R or FT245R so use FT232R EEPROM structure
                FTDI.FT232R_EEPROM_STRUCTURE myEEData = new FTDI.FT232R_EEPROM_STRUCTURE();
                // Read the device EEPROM
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT232REEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling ReadFT232REEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to read device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                Console.WriteLine("EEPROM Contents for device at index 0:");
                Console.WriteLine("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID));
                Console.WriteLine("Product ID: " + String.Format("{0:x}", myEEData.ProductID));
                Console.WriteLine("Manufacturer: " + myEEData.Manufacturer.ToString());
                Console.WriteLine("Manufacturer ID: " + myEEData.ManufacturerID.ToString());
                Console.WriteLine("Description: " + myEEData.Description.ToString());
                Console.WriteLine("Serial Number: " + myEEData.SerialNumber.ToString());
                Console.WriteLine("Max Power: " + myEEData.MaxPower.ToString() + "mA");
                Console.WriteLine("Self Powered: " + myEEData.SelfPowered.ToString());
                Console.WriteLine("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString());
                Console.WriteLine("");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = String.Empty;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT232REEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling WriteFT232REEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to write device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }
            else if (ftdiDeviceList[0].Type == FTDI.FT_DEVICE.FT_DEVICE_2232)
            {
                // We have an FT2232 so use FT2232 EEPROM structure
                FTDI.FT2232_EEPROM_STRUCTURE myEEData = new FTDI.FT2232_EEPROM_STRUCTURE();
                // Read the device EEPROM
                ftStatus = myFtdiDevice.ReadFT2232EEPROM(myEEData);
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT2232EEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling ReadFT2232EEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to read device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                Console.WriteLine("EEPROM Contents for device at index 0:");
                Console.WriteLine("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID));
                Console.WriteLine("Product ID: " + String.Format("{0:x}", myEEData.ProductID));
                Console.WriteLine("Manufacturer: " + myEEData.Manufacturer.ToString());
                Console.WriteLine("Manufacturer ID: " + myEEData.ManufacturerID.ToString());
                Console.WriteLine("Description: " + myEEData.Description.ToString());
                Console.WriteLine("Serial Number: " + myEEData.SerialNumber.ToString());
                Console.WriteLine("Max Power: " + myEEData.MaxPower.ToString() + "mA");
                Console.WriteLine("Self Powered: " + myEEData.SelfPowered.ToString());
                Console.WriteLine("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString());
                Console.WriteLine("");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = String.Empty;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT2232EEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling WriteFT2232EEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to write device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }
            else if (ftdiDeviceList[0].Type == FTDI.FT_DEVICE.FT_DEVICE_BM)
            {
                // We have an FT232B or FT245B so use FT232B EEPROM structure
                FTDI.FT232B_EEPROM_STRUCTURE myEEData = new FTDI.FT232B_EEPROM_STRUCTURE();
                // Read the device EEPROM
                ftStatus = myFtdiDevice.ReadFT232BEEPROM(myEEData);
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT232BEEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling ReadFT232BEEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to read device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                Console.WriteLine("EEPROM Contents for device at index 0:");
                Console.WriteLine("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID));
                Console.WriteLine("Product ID: " + String.Format("{0:x}", myEEData.ProductID));
                Console.WriteLine("Manufacturer: " + myEEData.Manufacturer.ToString());
                Console.WriteLine("Manufacturer ID: " + myEEData.ManufacturerID.ToString());
                Console.WriteLine("Description: " + myEEData.Description.ToString());
                Console.WriteLine("Serial Number: " + myEEData.SerialNumber.ToString());
                Console.WriteLine("Max Power: " + myEEData.MaxPower.ToString() + "mA");
                Console.WriteLine("Self Powered: " + myEEData.SelfPowered.ToString());
                Console.WriteLine("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString());
                Console.WriteLine("");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = String.Empty;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT232BEEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling WriteFT232BEEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to write device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }


            // Use cycle port to force a re-enumeration of the device.
            // In the FTD2XX_NET class library, the cycle port method also
            // closes the open handle so no need to call the Close method separately.
            ftStatus = myFtdiDevice.CyclePort();

            UInt32 newFtdiDeviceCount = 0;

            do
            {
                // Wait for device to be re-enumerated
                // The device will have the same location since it has not been
                // physically unplugged, so we will keep trying to open it until it succeeds
                ftStatus = myFtdiDevice.OpenByLocation(ftdiDeviceList[0].LocId);
                Thread.Sleep(1000);
            } while (ftStatus != FTDI.FT_STATUS.FT_OK);

            // Close the device
            myFtdiDevice.Close();

            // Re-create our device list
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref newFtdiDeviceCount);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }

            // Re-populate our device list
            ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);

            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                for (UInt32 i = 0; i < ftdiDeviceCount; i++)
                {
                    Console.WriteLine("Device Index: " + i.ToString());
                    Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags));
                    Console.WriteLine("Type: " + ftdiDeviceList[i].Type.ToString());
                    Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID));
                    Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId));
                    Console.WriteLine("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString());
                    Console.WriteLine("Description: " + ftdiDeviceList[i].Description.ToString());
                    Console.WriteLine("");
                }
            }

            // Wait for a key press
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
            return;
        }
Example #2
0
        internal void encode(string SerialNumber, string ChipID, string New)
        {
            content.Clear();


            FTDI.FT_DEVICE_INFO_NODE keyPro;
            content.Clear();
            if (!keyProMap.TryGetValue(ChipID, out keyPro))
            {
                content.Append("Device not found! SerialNumber:" + SerialNumber);
                return;
            }
            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
            // Create new instance of the FTDI device class
            FTDI myFtdiDevice = new FTDI();

            // Open device in our list by serial number
            ftStatus = myFtdiDevice.OpenBySerialNumber(keyPro.SerialNumber);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                content.Append("Failed to open device (error " + ftStatus.ToString() + ")" + "\n");
                return;
            }
            // get encode
            string encode = getMD5("SANWA" + ChipID + New);

            encode = encode.Substring(0, 8) + encode.Substring(12, 8);

            // Create our device EEPROM structure based on the type of device we have open
            if (keyPro.Type == FTDI.FT_DEVICE.FT_DEVICE_232R)
            {
                // We have an FT232R or FT245R so use FT232R EEPROM structure
                FTDI.FT232R_EEPROM_STRUCTURE myEEData = new FTDI.FT232R_EEPROM_STRUCTURE();
                // Read the device EEPROM
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT232REEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION e)
                {
                    content.Append("Exception thrown when calling ReadFT232REEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    content.Append("Failed to read device EEPROM (error " + ftStatus.ToString() + ")" + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = New;
                myEEData.Description  = encode;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT232REEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    content.Append("Exception thrown when calling WriteFT232REEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    content.Append("Failed to write device EEPROM (error " + ftStatus.ToString() + ")" + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                content.Append("EEPROM Contents for device at index 0:" + "\n");
                content.Append("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID) + "\n");
                content.Append("Product ID: " + String.Format("{0:x}", myEEData.ProductID) + "\n");
                content.Append("Manufacturer: " + myEEData.Manufacturer.ToString() + "\n");
                content.Append("Manufacturer ID: " + myEEData.ManufacturerID.ToString() + "\n");
                //content.Append("Description: " + myEEData.Description.ToString() + "\n");
                content.Append("Description: " + encode + "\n");
                content.Append("Serial Number: " + myEEData.SerialNumber.ToString() + "\n");
                content.Append("Max Power: " + myEEData.MaxPower.ToString() + "mA" + "\n");
                content.Append("Self Powered: " + myEEData.SelfPowered.ToString() + "\n");
                content.Append("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString() + "\n");
            }
            else if (keyPro.Type == FTDI.FT_DEVICE.FT_DEVICE_2232)
            {
                // We have an FT2232 so use FT2232 EEPROM structure
                FTDI.FT2232_EEPROM_STRUCTURE myEEData = new FTDI.FT2232_EEPROM_STRUCTURE();
                // Read the device EEPROM
                ftStatus = myFtdiDevice.ReadFT2232EEPROM(myEEData);
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT2232EEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    content.Append("Exception thrown when calling ReadFT2232EEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    content.Append("Failed to read device EEPROM (error " + ftStatus.ToString() + ")" + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                content.Append("EEPROM Contents for device at index 0:" + "\n");
                content.Append("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID) + "\n");
                content.Append("Product ID: " + String.Format("{0:x}", myEEData.ProductID) + "\n");
                content.Append("Manufacturer: " + myEEData.Manufacturer.ToString() + "\n");
                content.Append("Manufacturer ID: " + myEEData.ManufacturerID.ToString() + "\n");
                //content.Append("Description: " + myEEData.Description.ToString() + "\n");
                content.Append("Description: " + encode + "\n");
                content.Append("Serial Number: " + myEEData.SerialNumber.ToString() + "\n");
                content.Append("Max Power: " + myEEData.MaxPower.ToString() + "mA" + "\n");
                content.Append("Self Powered: " + myEEData.SelfPowered.ToString() + "\n");
                content.Append("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString() + "\n");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                //myEEData.SerialNumber = String.Empty;
                myEEData.Description = encode;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT2232EEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    content.Append("Exception thrown when calling WriteFT2232EEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    content.Append("Failed to write device EEPROM (error " + ftStatus.ToString() + ")" + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }
            else if (keyPro.Type == FTDI.FT_DEVICE.FT_DEVICE_BM)
            {
                // We have an FT232B or FT245B so use FT232B EEPROM structure
                FTDI.FT232B_EEPROM_STRUCTURE myEEData = new FTDI.FT232B_EEPROM_STRUCTURE();
                // Read the device EEPROM
                ftStatus = myFtdiDevice.ReadFT232BEEPROM(myEEData);
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT232BEEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    content.Append("Exception thrown when calling ReadFT232BEEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    content.Append("Failed to read device EEPROM (error " + ftStatus.ToString() + ")" + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                content.Append("EEPROM Contents for device at index 0:" + "\n");
                content.Append("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID) + "\n");
                content.Append("Product ID: " + String.Format("{0:x}", myEEData.ProductID) + "\n");
                content.Append("Manufacturer: " + myEEData.Manufacturer.ToString() + "\n");
                content.Append("Manufacturer ID: " + myEEData.ManufacturerID.ToString() + "\n");
                //content.Append("Description: " + myEEData.Description.ToString() + "\n");
                content.Append("Description: " + encode + "\n");
                content.Append("Serial Number: " + myEEData.SerialNumber.ToString() + "\n");
                content.Append("Max Power: " + myEEData.MaxPower.ToString() + "mA" + "\n");
                content.Append("Self Powered: " + myEEData.SelfPowered.ToString() + "\n");
                content.Append("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString() + "\n");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                //myEEData.SerialNumber = String.Empty;
                myEEData.Description = encode;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT232BEEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    content.Append("Exception thrown when calling WriteFT232BEEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    content.Append("Failed to write device EEPROM (error " + ftStatus.ToString() + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }


            // Use cycle port to force a re-enumeration of the device.
            // In the FTD2XX_NET class library, the cycle port method also
            // closes the open handle so no need to call the Close method separately.
            ftStatus = myFtdiDevice.CyclePort();

            UInt32 newFtdiDeviceCount = 0;

            do
            {
                // Wait for device to be re-enumerated
                // The device will have the same location since it has not been
                // physically unplugged, so we will keep trying to open it until it succeeds
                ftStatus = myFtdiDevice.OpenByLocation(keyPro.LocId);
                Thread.Sleep(1000);
            } while (ftStatus != FTDI.FT_STATUS.FT_OK);

            // Close the device
            myFtdiDevice.Close();

            // Re-create our device list
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref newFtdiDeviceCount);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                content.Append("Failed to get number of devices (error " + ftStatus.ToString() + ")" + "\n");
                return;
            }
        }
Example #3
0
        //finds the ftdi port
        private string getFTDIPort()
        {
            FTDI ftdi = new FTDI();

            FTDI.FT_STATUS state;

            uint deviceCount = 0;

            state = ftdi.GetNumberOfDevices(ref deviceCount);

            if (state != FTDI.FT_STATUS.FT_OK)
            {
                return(null);
            }

            FTDI.FT_DEVICE_INFO_NODE[] deviceNodes = new FTDI.FT_DEVICE_INFO_NODE[deviceCount];
            state = ftdi.GetDeviceList(deviceNodes);

            if (state != FTDI.FT_STATUS.FT_OK)
            {
                return(null);
            }

            string port = null;

            foreach (FTDI.FT_DEVICE_INFO_NODE node in deviceNodes)
            {
                if (node.Type == FTDI.FT_DEVICE.FT_DEVICE_2232)
                {
                    //Dieses Gerät hat 2 serielle Anschlüsse. Wir brauchen den Typ B.
                    if (node.Description.EndsWith("B"))
                    {
                        state = ftdi.OpenBySerialNumber(node.SerialNumber);

                        if (state != FTDI.FT_STATUS.FT_OK)
                        {
                            continue;
                        }

                        String tmpPort;
                        state = ftdi.GetCOMPort(out tmpPort);

                        if (state != FTDI.FT_STATUS.FT_OK)
                        {
                            Trace.WriteLine("Failed to get COM-Port (error " + state.ToString() + ")");
                            ftdi.Close();
                            continue;
                        }

                        if (tmpPort == null || tmpPort.Length == 0)
                        {
                            Trace.WriteLine(string.Format("Failed to get COM-Port for device {0}.", node.SerialNumber));
                            ftdi.Close();
                            continue;
                        }

                        FTDI.FT2232_EEPROM_STRUCTURE eepromData = new FTDI.FT2232_EEPROM_STRUCTURE();

                        try
                        {
                            state = ftdi.ReadFT2232EEPROM(eepromData);
                        }
                        catch (Exception)
                        {
                            Trace.WriteLine("Exception thrown when calling ReadFT2232EEPROM");
                        }

                        if (state != FTDI.FT_STATUS.FT_OK)
                        {
                            Trace.WriteLine("Failed to read device EEPROM (error " + state.ToString() + ")");
                            ftdi.Close();
                            continue;
                        }

                        Debug.WriteLine(port);
                        ftdi.Close();

                        if (!eepromData.Manufacturer.Equals("EnOcean GmbH") && !eepromData.Manufacturer.Equals("Viessmann") && !eepromData.Manufacturer.Equals("Eltako GmbH"))
                        {
                            Trace.WriteLine(String.Format("Wrong Manufacturer {0}", eepromData.Manufacturer));
                            continue;
                        }

                        if (tmpPort != null)
                        {
                            port = tmpPort;
                            break;
                        }
                    }
                }
                else if (node.Type == FTDI.FT_DEVICE.FT_DEVICE_232R)
                {
                    state = ftdi.OpenBySerialNumber(node.SerialNumber);

                    if (state != FTDI.FT_STATUS.FT_OK)
                    {
                        continue;
                    }

                    String tmpPort;
                    state = ftdi.GetCOMPort(out tmpPort);

                    if (state != FTDI.FT_STATUS.FT_OK)
                    {
                        Trace.WriteLine("Failed to get COM-Port (error " + state.ToString() + ")");
                        ftdi.Close();
                        continue;
                    }

                    if (tmpPort == null || tmpPort.Length == 0)
                    {
                        Trace.WriteLine(string.Format("Failed to get COM-Port for device {0}.", node.SerialNumber));
                        ftdi.Close();
                        continue;
                    }

                    FTDI.FT232R_EEPROM_STRUCTURE eepromData = new FTDI.FT232R_EEPROM_STRUCTURE();

                    try
                    {
                        state = ftdi.ReadFT232REEPROM(eepromData);
                    }
                    catch (Exception)
                    {
                        Trace.WriteLine("Exception thrown when calling ReadFT232REEPROM");
                    }

                    if (state != FTDI.FT_STATUS.FT_OK)
                    {
                        Trace.WriteLine("Failed to read device EEPROM (error " + state.ToString() + ")");
                        ftdi.Close();
                        continue;
                    }

                    ftdi.Close();

                    if (!eepromData.Manufacturer.Equals("EnOcean GmbH") && !eepromData.Manufacturer.Equals("Viessmann") && !eepromData.Manufacturer.Equals("Eltako GmbH"))
                    {
                        Trace.WriteLine(String.Format("Wrong Manufacturer {0}", eepromData.Manufacturer));
                        continue;
                    }

                    if (tmpPort != null)
                    {
                        port = tmpPort;
                        break;
                    }
                }
            }

            return(port);
        }