libftdi Archives

Subject: Error when multiple FTDI devices are connected and want to open a single one by product name

From: Karl Koehler <karl@xxxxxxxxxxxx>
To: "libftdi@xxxxxxxxxxxxxxxxxxxxxxx" <libftdi@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 1 Aug 2025 22:48:27 +0000
Hi,

We have found a problem that happens if there are 
* multiple FTDI 4232H devices connected to a PC via USB; 
* one of these does not have a programmed eeprom, and 
* I would like to open another one based on the product description or serial 
number.
The problem occurs because libusb_get_string_descriptor_ascii() will return an 
error. We should not care about this because we are searching for a different 
device.
What happens though is that ftdi_usb_open_desc_index() will just return, 
because it calls the macro "ftdi_error_return_free_device".
Instead, we should store the error message, and if no matching device is found, 
then that error should be returned instead of the generic "device not found".

Attached is a proposed patch; thank you for providing this library and thank 
you for your consideration.

 - Karl Koehler

---

--- a/libftdi1-1.5/src/ftdi.c  2020-07-07 12:32:55.000000000 -0700
+++ b/libftdi1-1.5/src/ftdi.c       2025-08-01 15:12:06.335941198 -0700
@@ -769,6 +769,9 @@
     char string[256];
     int i = 0;
 
+    const char* error_string = NULL;
+    int   error_code = 0;
+
     if (ftdi == NULL)
         ftdi_error_return(-11, "ftdi context invalid");
 
@@ -785,15 +788,15 @@
 
         if (desc.idVendor == vendor && desc.idProduct == product)
         {
-            if (libusb_open(dev, &ftdi->usb_dev) < 0)
-                ftdi_error_return_free_device_list(-4, "usb_open() failed", 
devs);
-
+            if (libusb_open(dev, &ftdi->usb_dev) < 0) {
+                error_string = "usb_open() failed"; error_code = -4; continue;
+            }
             if (description != NULL)
             {
                 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, 
desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
                 {
                     ftdi_usb_close_internal (ftdi);
-                    ftdi_error_return_free_device_list(-8, "unable to fetch 
product description", devs);
+                    error_string = "unable to fetch product description"; 
error_code = -8; continue;
                 }
                 if (strncmp(string, description, sizeof(string)) != 0)
                 {
@@ -806,7 +809,7 @@
                 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, 
desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
                 {
                     ftdi_usb_close_internal (ftdi);
-                    ftdi_error_return_free_device_list(-9, "unable to fetch 
serial number", devs);
+                    error_string = "unable to fetch serial number"; error_code 
= -9; continue;
                 }
                 if (strncmp(string, serial, sizeof(string)) != 0)
                 {
@@ -829,6 +832,9 @@
         }
     }
 
+    if (error_string) {
+        ftdi_error_return_free_device_list(error_code,error_string,devs);
+    }
     // device not found
     ftdi_error_return_free_device_list(-3, "device not found", devs);
 }
@@ -3475,7 +3481,7 @@
             i = 0x40;
         }
         if ((ftdi->type == TYPE_230X) && (i >=  0x40) && (i < 0x50)) {
-            uint16_t data;
+            uint16_t data = 0;
             if (ftdi_read_eeprom_location(ftdi, i, &data)) {
                 fprintf(stderr, "Reading Factory Configuration Data failed\n");
                 i = 0x50;




--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx

Current Thread
  • Error when multiple FTDI devices are connected and want to open a single one by product name, Karl Koehler <=