Linux/C++ 中的枚举 USB 设备

发布于 2024-10-10 05:42:32 字数 155 浏览 0 评论 0原文

我正在为 Linux 设备编写 IO 例程,该设备将连接各种不同且不断变化的 USB 设备。为此,我需要能够找出哪个设备连接到哪个端口,以便我可以使用正确的软件打开它。类似于“udevinfo”的东西是理想的,但我不知道如何开始编写这样的东西。

有关 C++ api 阅读的建议?

Im writing IO routines for a linux device that will have various and changing USB devices connected to it. To this end I need to be able to figure out which device is connected to which port so I can open it with the correct software. Something akin to 'udevinfo' would be ideal but I have no idea how to start in writing such.

Suggestions on c++ apis to read?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

幽梦紫曦~ 2024-10-17 05:42:32

看看 libudev++。这似乎就是您要找的。

Take a look at libudev++. It seems to be what you're looking for.

ぃ双果 2024-10-17 05:42:32

请参阅 libusblibusb_get_device_listlibusb_get_bus_numberlibusb_get_device_address

See libusb's libusb_get_device_list, libusb_get_bus_number, libusb_get_device_address.

凉月流沐 2024-10-17 05:42:32

GIO 应该可以帮助你。连接到 添加卷 和 < href="http://library.gnome.org/devel//gio/2.18/GVolumeMonitor.html#GVolumeMonitor-volume-removed" rel="nofollow">volume-removed 信号将提醒您的程序从系统中添加或删除的任何存储设备。如果不需要 GIO 提供的控制级别,可以使用 libudev++ 提供了 GIO 的高级包装器。

GIO should help you in that. Connecting to the volume-added and volume-removed signals will alert your program to any storage device added or removed from the system. If you do not need the level of control provided by GIO, you can use libudev++ which provided a high level wrapper over GIO.

戴着白色围巾的女孩 2024-10-17 05:42:32

我不知道你需要什么样的信息,但你可以直接浏览/sys/bus/usb?

i don't know what kind of information you need, but you could just go through /sys/bus/usb?

回眸一笑 2024-10-17 05:42:32

我最终在 chkconfig 文件中使用了 BASH 解决方案。我浏览所有 ttyUSB 条目并查看每个条目的驱动程序信息:

USB_ID=`egrep -i "mct u232|pl2303|keyspan" -m 1 /proc/tty/driver/usbserial | awk '{ printf( "$d", $1 )}'`
if [ -z $USB_ID ]
then
   echo $echo_n "No USB serial adapter found.";
   exit 1
fi

I ended up using a BASH solution in the chkconfig file. I walk through all ttyUSB entries and look at the driver info for each:

USB_ID=`egrep -i "mct u232|pl2303|keyspan" -m 1 /proc/tty/driver/usbserial | awk '{ printf( "$d", $1 )}'`
if [ -z $USB_ID ]
then
   echo $echo_n "No USB serial adapter found.";
   exit 1
fi
许久 2024-10-17 05:42:32

更新的解决方案:

迭代这些文件系统目录:

/dev/serial/by-id
/dev/snd/by-id
/dev/disk/by-id
/dev/input/by-id
/dev/v4l/by-id

取决于您要查找的设备类。

例如,找到我的 Arduino Nano 的串行端口:

#include <filesystem>
#include <string>

const std::string path = "/dev/serial/by-id";

for( const auto & file : std::filesystem::directory_iterator( path ) )
{
    const std::string s = "NANO_33_IoT";
    if( file.path().generic_string().find(s) )
    {
        return file.path();
    }
}

More recent solution:

Iterate over these file system directories:

/dev/serial/by-id
/dev/snd/by-id
/dev/disk/by-id
/dev/input/by-id
/dev/v4l/by-id

depending on what device class you are looking for.

For example, finding the serial port for my Arduino Nano:

#include <filesystem>
#include <string>

const std::string path = "/dev/serial/by-id";

for( const auto & file : std::filesystem::directory_iterator( path ) )
{
    const std::string s = "NANO_33_IoT";
    if( file.path().generic_string().find(s) )
    {
        return file.path();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文