libusb获取设备名
时间: 2023-07-27 16:06:33 浏览: 505
libusb在Qt for android中的调用
要获取libusb中设备的名称,您可以使用libusb_device_descriptor()函数获取设备描述符,然后使用libusb_get_string_descriptor_ascii()函数获取设备的字符串描述符以下是一个示例代码:
```c
#include <stdio.h>
#include <libusb-1.0/libusb.h>
int main() {
libusb_context *ctx = NULL;
libusb_device **devs;
ssize_t count;
int r;
r = libusb_init(&ctx);
if (r < 0) {
printf("Failed to initialize libusb\n");
return 1;
}
count = libusb_get_device_list(ctx, &devs);
if (count < 0) {
printf("Failed to get device list\n");
libusb_exit(ctx);
return 1;
}
for (ssize_t i = 0; i < count; ++i) {
libusb_device *dev = devs[i];
struct libusb_device_descriptor desc;
r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
printf("Failed to get device descriptor\n");
continue;
}
char manufacturer[256], product[256], serial[256];
r = libusb_get_string_descriptor_ascii(dev, desc.iManufacturer, (unsigned char *)manufacturer, sizeof(manufacturer));
if (r < 0) {
printf("Failed to get manufacturer string descriptor\n");
}
r = libusb_get_string_descriptor_ascii(dev, desc.iProduct, (unsigned char *)product, sizeof(product));
if (r < 0) {
printf("Failed to get product string descriptor\n");
}
r = libusb_get_string_descriptor_ascii(dev, desc.iSerialNumber, (unsigned char *)serial, sizeof(serial));
if (r < 0) {
printf("Failed to get serial number string descriptor\n");
}
printf("Device %d: Manufacturer=%s, Product=%s, Serial=%s\n", i+1, manufacturer, product, serial);
}
libusb_free_device_list(devs, 1);
libusb_exit(ctx);
return 0;
}
```
这段代码使用libusb_get_string_descriptor_ascii()函数获取设备的制造商、产品和序列号字符串描述符,并将它们打印出来。
请确保已经安装了libusb库,并使用编译器将代码编译为可执行文件。运行该可执行文件后,您将看到打印出的设备列表,包括每个设备的制造商、产品和序列号。
希望对您有帮助!
阅读全文