In the Libusb open source source, there are based on VID and PID to open the interface of the device. However, this is not the only device. Because there are a lot of devices vid and PID are the same, such as the iphone.
But the serial number of the device is unique, so according to the reference vid and PID open source code, the following is the opening of a designated serialnumber device.
libusb_device_handle* Libusb_open_device_with_serialnumber (
libusb_context* ctx, const char* serial_number) {
struct libusb_device** devs;
struct libusb_device* dev;
struct libusb_device_handle* handle = NULL;
Char string[128];
Char serialnumber[128] = "";
uint8_t String_index[3];
size_t i = 0;
int r = 0;
if (Serial_number)
strcpy (SerialNumber, Serial_number);
if (Libusb_get_device_list (CTX, &devs) < 0)
return NULL;
while (dev = devs[i++])! = NULL) {
struct Libusb_device_descriptor desc;
r = Libusb_get_device_descriptor (Dev, &desc);
if (R < 0)
Goto out;
STRING_INDEX[2] = Desc.iserialnumber;
r = Libusb_open (Dev, &handle);
if (r < 0) {
handle = NULL;
Break
}
if (Libusb_get_string_descriptor_ascii (handle, string_index[2], (unsigned char*) string, $) >= 0) {
if (!strcmp (SerialNumber, String))
Break
}
Libusb_close (handle);
handle = NULL;
}
Out
Libusb_free_device_list (devs, 1);
return handle;
}
Libusb is opened according to the serialnumber of the device