开发者社区> 问答> 正文

linux系统利用libudev获取USB设备的VID和PID?请各位大侠帮一忙,谢谢!

我在Ubuntu14.04终端下lsusb可以看到识别到的USB设备,但是Unable to find parent usb device。我的设备会虚拟出串口,如ttyUSB0,ttyUSB1,ttyUSB2,我想知道如何获取该设备的VID和PID,通过网上找的代码,不知道如何修改以下两个函数的相关参数?
udev_enumerate_add_match_subsystem(enumerate, "block");
dev = udev_device_get_parent_with_subsystem_devtype( dev, "usb", "usb_device")

展开
收起
云栖技术 2016-06-06 10:05:35 6806 0
1 条回答
写回答
取消 提交回答
  • 社区爱好者,专为云栖社区服务!
     #include <libudev.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <locale.h>
    #include <unistd.h>
    
    int main (void)
    {
        struct udev *udev;
        struct udev_enumerate *enumerate;
        struct udev_list_entry *devices, *dev_list_entry;
        struct udev_device *dev;
    
        /* Create the udev object */
        udev = udev_new();
        if (!udev) {
            printf("Can't create udev\n");
            exit(1);
        }
    
        /* Create a list of the devices in the 'hidraw' subsystem. */
        enumerate = udev_enumerate_new(udev);
        udev_enumerate_add_match_subsystem(enumerate, "hidraw");
        udev_enumerate_scan_devices(enumerate);
        devices = udev_enumerate_get_list_entry(enumerate);
        /* For each item enumerated, print out its information.
           udev_list_entry_foreach is a macro which expands to
           a loop. The loop will be executed for each member in
           devices, setting dev_list_entry to a list entry
           which contains the device's path in /sys. */
        udev_list_entry_foreach(dev_list_entry, devices) {
            const char *path;
    
            /* Get the filename of the /sys entry for the device
               and create a udev_device object (dev) representing it */
            path = udev_list_entry_get_name(dev_list_entry);
            dev = udev_device_new_from_syspath(udev, path);
    
            /* usb_device_get_devnode() returns the path to the device node
               itself in /dev. */
            printf("Device Node Path: %s\n", udev_device_get_devnode(dev));
    
            /* The device pointed to by dev contains information about
               the hidraw device. In order to get information about the
               USB device, get the parent device with the
               subsystem/devtype pair of "usb"/"usb_device". This will
               be several levels up the tree, but the function will find
               it.*/
            dev = udev_device_get_parent_with_subsystem_devtype(
                   dev,
                   "usb",
                   "usb_device");
            if (!dev) {
                printf("Unable to find parent usb device.");
                exit(1);
            }
    
            /* From here, we can call get_sysattr_value() for each file
               in the device's /sys entry. The strings passed into these
               functions (idProduct, idVendor, serial, etc.) correspond
               directly to the files in the directory which represents
               the USB device. Note that USB strings are Unicode, UCS2
               encoded, but the strings returned from
               udev_device_get_sysattr_value() are UTF-8 encoded. */
            printf("  VID/PID: %s %s\n",
                    udev_device_get_sysattr_value(dev,"idVendor"),
                    udev_device_get_sysattr_value(dev, "idProduct"));
            printf("  %s\n  %s\n",
                    udev_device_get_sysattr_value(dev,"manufacturer"),
                    udev_device_get_sysattr_value(dev,"product"));
            printf("  serial: %s\n",
                     udev_device_get_sysattr_value(dev, "serial"));
            udev_device_unref(dev);
        }
        /* Free the enumerator object */
        udev_enumerate_unref(enumerate);
    
        udev_unref(udev);
    
        return 0;       
    2019-07-17 19:27:44
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Alibaba Cloud Linux 3 发布 立即下载
ECS系统指南之Linux系统诊断 立即下载
ECS运维指南 之 Linux系统诊断 立即下载