CP210x分配了/ dev / ttyUSB0,如何知道C ++中的设备名称?

时间:2016-03-29 12:55:30

标签: c++ linux module

我想用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) 

有没有办法通过内核模块知道分配的设备名称?

1 个答案:

答案 0 :(得分:0)

最后,我解决了这个问题。谢谢大家! 我使用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