我想用 C++ 知道设备名称。 我的 CP210x 设备分配有此驱动程序的/dev/ttyUSB0。 http://www.silabs.com/products/mcu/Pages/USBtoUARTBridgeVCPDrivers.aspx
在我的 C++ 设备中,我想知道设备名称(比如/dev/ttyUSB0、/dev/ttyUSB1 ...) 因为我有3个设备... 我要区分设备。
1/ 首先,我尝试了 libusb 但它可能还不够......
libusb_context *context = NULL;
libusb_device **list = NULL;
int rc = 0;
ssize_t count = 0;
uint8_t path[8];
rc = libusb_init(&context);
assert(rc == 0);
count = libusb_get_device_list(context, &list);
assert(count > 0);
for (size_t idx = 0; idx < count; ++idx) {
libusb_device *device = list[idx];
libusb_device_descriptor desc = {0};
rc = libusb_get_device_descriptor(device, &desc);
assert(rc == 0);
printf("Vendor:Device = %04x:%04x:%d\n", desc.idVendor, desc.idProduct, desc.bcdUSB);
//printf("Vendor:Device = %d:%d:%d:%d\n", desc.bDeviceClass, desc.bcdDevice, desc.iSerialNumber, desc.iProduct);
printf("%04x:%04x (bus %d, device %d) ",
desc.idVendor, desc.idProduct,
libusb_get_bus_number(device), libusb_get_device_address(device)); //, device->filename);
rc = libusb_get_port_numbers(device,path,sizeof(path));
if (rc > 0) {
printf(" path: %d", path[0]);
for (int j = 1; j < rc; j++){
printf(".%d", path[j]);
}
printf("\n");
}
//desc.bcdUSB
}
返回
Vendor:Device = 1d6b:0002:512
1d6b:0002 (bus 2, device 1) Vendor:Device = 1d6b:0001:272
1d6b:0001 (bus 8, device 1) Vendor:Device = 1d6b:0001:272
1d6b:0001 (bus 7, device 1) Vendor:Device = 1d6b:0001:272
1d6b:0001 (bus 6, device 1) Vendor:Device = 1d6b:0002:512
1d6b:0002 (bus 1, device 1) Vendor:Device = 1d6b:0001:272
1d6b:0001 (bus 5, device 1) Vendor:Device = 10c4:ea60:272
10c4:ea60 (bus 4, device 10) path: 2
Vendor:Device = 1d6b:0001:272
1d6b:0001 (bus 4, device 1) Vendor:Device = 1d6b:0001:272
1d6b:0001 (bus 3, device 1)
有没有办法知道内核模块分配的设备名称?
最佳答案
终于解决了。。谢谢大家! 我使用 shell 脚本。我从我的 C++ 编程中调用它。
#!/bin/sh
echo password | sudo -S cat /proc/tty/driver/usbserial | grep cp210x | awk -F'[ :]' '{print "/dev/ttyUSB"$1" "$8" "$10}'
输出在这里。
/dev/ttyUSB0 10c4 ea60
关于c++ - CP210x assigned with/dev/ttyUSB0,如何在C++中知道设备名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36284925/