游不离2010 2015-09-10 02:21 采纳率: 0%
浏览 4294

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")

  • 写回答

1条回答 默认 最新

  • oyljerry 2015-09-10 02:27
    关注
     #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;       
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题