7

我将 2 个网络摄像头连接到计算机,它列在 /dev 文件夹中:/dev/video0; /dev/video1.

你能帮我写 C 代码来获取带有输入的网络摄像头的序列号吗:/dev/video[0;1]

4

4 回答 4

19

刚遇到同样的问题,花了一些时间才找到解决方案。任何以“仅使用 lsusb”开头的解决方案都是不正确的。您可以找出设备的序列号,但它提供的任何额外信息都不能帮助您确定它链接到哪个 /dev/video。

解决方案:

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

输出:

E: ID_SERIAL_SHORT=256DEC57
于 2015-09-26T20:44:20.273 回答
8

根据使用 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;
}
于 2016-11-02T12:59:11.867 回答
0

Playing around with libusb, it looks like there's a standard getSerialNumber() method. Unfortunately, not all USB devices implement this. I have a couple cheap $4 webcams that return None for it. 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.

But assuming you get a serial number for your device, the next problem is figuring out which /dev/videoN file it corresponds to. 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. 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.

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:

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

That's a directory, and inside it you'll find a videoN file matching one in /dev.

于 2015-04-16T00:58:01.580 回答
-1

lsusb你发现它使用了libusb,它有很多功能,特别是用于USB 设备处理和枚举libudev也可能是相关的。

或者,popen命令lsusb...

于 2013-09-04T05:09:52.417 回答