Linux 中是否可以检测 USB 扩展卡的存在?

发布于 2024-12-28 00:33:02 字数 70 浏览 1 评论 0原文

我想编写一些 C++ 程序,可以检测 USB 扩展卡或 SD 卡读卡器的存在,而无需插入任何东西。这可能吗?在Linux 中?

I would like to write some C++ program that can detect the presence of an USB expansion card, or an SD card reader without it necessarily having anything plugged in to it. Is this possible? In Linux?

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

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

发布评论

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

评论(2

初懵 2025-01-04 00:33:02

如果您知道确切的供应商 ID 和/或产品 ID,您可以搜索它,如下所示:

for (bus = busses; bus; bus = bus->next)
for (dev = bus->devices; dev; dev = dev->next)
  if ((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
    return dev;

libusb 教程

if you know the exact vendor id and/or product id you could search for it, like this:

for (bus = busses; bus; bus = bus->next)
for (dev = bus->devices; dev; dev = dev->next)
  if ((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
    return dev;

libusb tutorial

违心° 2025-01-04 00:33:02

是的,您可以通过简单的 dmesg 获取 idVendor 和 idProduct。然后,输入上面由知识渊博的人显示的搜索条件。如果您想更深入地研究,并且您有 Linux,那么您可以探索 /drivers/usb/core 中的 usb.h
有一个结构:struct usb_device{}
如果您更热衷于探索,您应该查看 driver.chub.c 等方法,例如 announce_device() 打印dmesg 之后的 USB 设备详细信息。永远探索它! :)

Yes, You can get the idVendor and idProduct by a simple dmesg. And then, put a search condition for it just shown above by a knowledgeble man. If you want to dig deeper, and if you have a linux, then you can explore usb.h present in your <kernel_source>/drivers/usb/core.
There is a structure : struct usb_device{}.
If you yet more keen to explore then, you should check out driver.c and hub.c for methods like announce_device() which prints the USB device details after a dmesg. Explore it for good! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文