简体   繁体   English

获取安装到 /dev 文件夹的 USB 设备的唯一序列号

[英]Get unique serial number of USB device mounted to /dev folder

I attach 2 webcam to computer and it was listed in /dev folder: /dev/video0;我将 2 个网络摄像头连接到计算机,它列在 /dev 文件夹中:/dev/video0; /dev/video1. /开发/视频1。

Can you help me write C code to get serial number of webcam with input: /dev/video[0;1]你能帮我写 C 代码来获取网络摄像头的序列号吗?输入:/dev/video[0;1]

Just ran into this same problem and it took a bit to find the solution. 刚遇到同样的问题,需要一点时间才能找到解决方案。 Any solution which starts with "just use lsusb" is incorrect. 任何以“只使用lsusb”开头的解决方案都是错误的。 You can figure out the devices serial, but none of the extra information it provides help you determine which /dev/video it links to. 您可以找出设备串行,但它提供的任何额外信息都无法帮助您确定链接到哪个/ dev / video。

Solution: 解:

/bin/udevadm info --name=/dev/video1 | grep SERIAL_SHORT

Output: 输出:

E: ID_SERIAL_SHORT=256DEC57

Based on the hint of using udevadm and the tutorial from http://www.signal11.us/oss/udev/ I got below code to get the serial info of my webcam. 基于使用udevadm和http://www.signal11.us/oss/udev/教程的提示,我得到了下面的代码来获取我的网络摄像头的序列信息。

#include "stdio.h"
#include <libudev.h>

int main(int argc, char **argv)
{
  struct udev *udev;
  struct udev_device *dev;
  struct udev_enumerate *enumerate;
  struct udev_list_entry *list, *node;
  const char *path;

  udev = udev_new();
  if (!udev) {
    printf("can not create udev");
    return 0;
  }

  enumerate = udev_enumerate_new(udev);
  udev_enumerate_add_match_subsystem(enumerate, "video4linux");
  udev_enumerate_scan_devices(enumerate);

  list = udev_enumerate_get_list_entry(enumerate);
  udev_list_entry_foreach(node, list) {
    path = udev_list_entry_get_name(node);
    dev = udev_device_new_from_syspath(udev, path);

    printf("Printing serial for %s\n", path);
    printf("ID_SERIAL=%s\n",
        udev_device_get_property_value(dev, "ID_SERIAL"));
    printf("ID_SERIAL_SHORT=%s\n",
        udev_device_get_property_value(dev, "ID_SERIAL_SHORT"));

    udev_device_unref(dev);
  }
  return 0;
}

You can use lsusb , but you need to add verbose flag and make sure you use sudo with it, otherwise the serial will be incorrect.您可以使用lsusb ,但您需要添加详细标志并确保您使用sudo ,否则序列号将不正确。

sudo lsusb -v

If that is too verbose, then run lsusb to get the device id:如果这太冗长,则运行lsusb以获取设备 ID:

$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 012: ID 1ab1:0e11 Rigol Technologies
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Then run lsusb with device flag and grep the serial number.然后使用设备标志和序列号 grep 运行lsusb

So for the serial number of Rigol device:所以对于Rigol设备的序列号:

$ sudo lsusb -s 012 -v|grep -i iserial
  iSerial                 3 DP8C221100000

Playing around with libusb , it looks like there's a standard getSerialNumber() method. 使用libusb ,看起来有一个标准的getSerialNumber()方法。 Unfortunately, not all USB devices implement this. 不幸的是,并非所有USB设备都实现了这一 I have a couple cheap $4 webcams that return None for it. 我有几个便宜的4美元的网络摄像头,它们返回None。 These interfaces expose other metadata, like VendorID and ProductID, which I've seen some code try and use as a unique identifier, but it's not guaranteed to be unique, especially if you have multiple devices of the same make and model. 这些接口公开了其他元数据,例如VendorID和ProductID,我已经看到一些代码尝试并用作唯一标识符,但它不能保证是唯一的,特别是如果你有多个相同品牌和型号的设备。

But assuming you get a serial number for your device, the next problem is figuring out which /dev/videoN file it corresponds to. 但假设您获得了设备的序列号,下一个问题是确定它对应的/ dev / videoN文件。 I have an old version of libusb installed, so I couldn't get the method working that returned the full sysfs path of the USB device, so instead I scrapped the output from hwinfo . 我安装了旧版本的libusb,所以我无法获得返回USB设备的完整sysfs路径的方法,所以我废弃了hwinfo的输出。 I extracted all the chunks corresponding to cameras, and then from those I extracted the piece that looked like: 我提取了与摄像机相对应的所有块,然后从那些我提取的块看起来像:

SysFS BusID: 1-1.2:1.0

USB devices actually form a complicated tree, and that BusID encodes where the device is located in that tree. USB设备实际上形成一个复杂的树,并且BusID 编码设备在该树中的位置。

You can then take that BusID to find where the device lives in the filesystem as well as the video path, which should be at: 然后,您可以使用该BusID查找设备在文件系统中的位置以及视频路径,该路径应位于:

/sys/bus/usb/devices/<BusID>/video4linux/

That's a directory, and inside it you'll find a videoN file matching one in /dev. 这是一个目录,在其中你会发现一个与/ dev匹配的videoN文件。

Looking at lsusb you find out that it uses libusb , it has many functions, notably for usb device handling and enumeration . 看看lsusb你发现它使用了libusb ,它有很多功能,特别是对于usb设备处理和枚举 libudev might be relevant too. libudev也可能是相关的。

Alternatively, popen the lsusb command... 或者, popen lsusb命令...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM