使用 C 或 C++ 从 USB 设备接收数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2981558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:40:31  来源:igfitidea点击:

Receiving data from a USB device in C or C++

c++windowsusb

提问by Midas

I need a list of all plugged in USB devices and have the user select one to let the console application receive any data the USB device sends.

我需要所有插入的 USB 设备的列表,并让用户选择一个让控制台应用程序接收 USB 设备发送的任何数据。

I can then start playing around with the data in my program.

然后我可以开始在我的程序中处理数据。

I don't want to use library's, only standard C++ functions, and the program should work in Windows 98.

我不想使用库的,只使用标准的 C++ 函数,并且该程序应该可以在 Windows 98 中运行。

回答by Hans Passant

This is a verypersistent question in forums and programming Q+A sites. Never with a happy ending. The B in USB means bus. That is a term in computer hardware design to describe an electrical interface for electronic devices to exchange data. It plays the exact same role as, say, the PCI (express) bus inside your machine. Since it is an electrical specification first and foremost, USB supports a very large number of types of devices. Anything from a wireless network adapter, modem, flash memory card to a teapot warmer. Just about the only kinds of devices that it doesn't handle well are ones that require a very large bandwidth, like a video adapter.

这是论坛和编程问答网站中一个非常持久的问题。永远没有幸福的结局。USB中的B表示总线。这是计算机硬件设计中的一个术语,用于描述电子设备交换数据的电气接口。它扮演的角色与机器内的 PCI(快速)总线完全相同。由于首先是电气规范,因此 USB 支持大量类型的设备。从无线网络适配器、调制解调器、闪存卡到茶壶加热器,应有尽有。几乎唯一不能很好处理的设备类型是需要非常大带宽的设备,例如视频适配器。

The USB specification has a very elegant protocol specification that describes how devices can share the bus and how they can exchange data. That protocol spec however does not describe the format of the data at all, it merely defines the notion of being able to deliver chunks of bytes. It is up to the device itself to give a meaning to those bytes.

USB 规范有一个非常优雅的协议规范,它描述了设备如何共享总线以及它们如何交换数据。然而,该协议规范根本没有描述数据的格式,它只是定义了能够传递字节块的概念。由设备本身决定这些字节的含义。

On the machine end, you need software to interpret those bytes and make the machine do something interesting with them. That requires a device driver. Just like your video card and your network interface card require a device driver. Obviously a video driver is verydifferent from a NIC driver. The same is true for USB drivers, there is little commonality.

在机器端,您需要软件来解释这些字节并使机器对它们做一些有趣的事情。这需要一个设备驱动程序。就像您的视频卡和网络接口卡需要设备驱动程序一样。显然,视频驱动程序与 NIC 驱动程序有很大不同。USB驱动也一样,通用性不大。

If you want to write software that treats USB devices similar then you need to write that at the level where they still have something in common. That's at the USB controller level, you could write a filter driver that injects itself in the USB driver stack and peeks at the I/O request packets between the controller and the device driver. Similar to, say, the winpcap filter driver that spies on TCP/IP traffic. There isn't much of anything interesting to see though, you'd be staring at the blobs of bytes that pass back and forth. It is a much bigger problem than winpcap, at least it sees bytes fly by whose meaning is documented somewhere in an RFC. That's not the case for USB, the company that makes the USB device is also usually the device driver supplier. They keep the internal format undocumented.

如果您想编写处理 USB 设备相似的软件,那么您需要在它们仍然具有共同点的级别上编写该软件。这是在 USB 控制器级别,您可以编写一个过滤驱动程序,将自身注入 USB 驱动程序堆栈并查看控制器和设备驱动程序之间的 I/O 请求数据包。类似于监视 TCP/IP 流量的 winpcap 过滤器驱动程序。不过,没有什么有趣的东西可看,您会盯着来回传递的字节块。这是一个比 winpcap 更大的问题,至少它看到字节飞行,其含义记录在 RFC 的某处。USB 的情况并非如此,制造 USB 设备的公司通常也是设备驱动程序供应商。他们保留未记录的内部格式。

Writing filter drivers requires pretty advanced skills, there are lots of pain points. Like crashing the operating system when you make a simple mistake. There has also been considerable flux in the Windows Driver Model lately, USB drivers have been getting moved into ring 3 (user mode) to keep the operating system stable.

编写过滤器驱动程序需要非常高级的技能,有很多痛点。就像当你犯了一个简单的错误时使操作系统崩溃一样。最近 Windows 驱动程序模型也有相当大的变化,USB 驱动程序已被移入 ring 3(用户模式)以保持操作系统稳定。

To get started, download the Windows WDK (aka "DDK") and read Walter Oney's books. Preferably all of them.

首先,下载 Windows WDK(又名“DDK”)并阅读 Walter Oney 的书籍。最好是全部。

回答by Matti Virkkunen

Here's something to get you started:

这里有一些东西可以让你开始:

And for crying out loud update to an OS that's not from the last millenium. I hear Linux has great support for USB.

并大声疾呼更新非上一千年的操作系统。我听说 Linux 对 USB 有很大的支持。

回答by OlimilOops

How can one write a program for USB, wanting to understand the background of it, while not wanting to read much about it, not wanting to use a library, all in the same time? Anyway. there is a project "libUSB Win32" from Stefan Meyer not under heavy development at the moment, but written in C maybe this could be something for you, it has also the ability to run under ancient windows versions ;) you can find it here:
http://sourceforge.net/apps/trac/libusb-win32/wiki
a while ago i have written this in VB6 that makes use of "libUSB Win32" don't know if it can be useful for you:
http://www.activevb.de/cgi-bin/upload/download.pl?id=3061

如何为 USB 编写一个程序,既想了解它的背景,又不想阅读太多关于它的内容,不想使用库,所有这些都在同一时间?反正。Stefan Meyer 有一个项目“libUSB Win32”目前还没有进行大量开发,但是用 C 编写的也许这可能适合你,它也有在古老的 Windows 版本下运行的能力;)你可以在这里找到它:
http://sourceforge.net/apps/trac/libusb-win32/wiki
不久前我用 VB6 写了这个,它使用了“libUSB Win32”,不知道它是否对你有用:
http:// www.activevb.de/cgi-bin/upload/download.pl?id=3061

回答by egrunin

I simply need a list of all plugged in USB devices and have the user select one to let the console application receive any data the USB device sends.

我只需要一个所有插入 USB 设备的列表,并让用户选择一个让控制台应用程序接收 USB 设备发送的任何数据。

Getting the list isn't the big problem, it's receiving the data.

获取列表不是大问题,重要的是接收数据。

I know you don't want to write a driver, but this is what drivers do:receive data from a device.

我知道您不想编写驱动程序,但这就是驱动程序所做的:从设备接收数据。

回答by DRD

The advice you're getting here is on the mark; USB is not easy, believe me... I'm working on a USB project but from the back-end. Seems to me that you need to be looking at the back-end too, that's where you really don't have to deal with libraries and all that, but you won't be able to get around the reading whatever you do.

你在这里得到的建议是正确的;USB 并不容易,相信我......我正在开发一个 USB 项目,但来自后端。在我看来,您还需要查看后端,在那里您真的不必处理库和所有这些,但是无论您做什么,您都无法绕过阅读。

Don't get discouraged yet, but you do need to wet you feet before you jump in and drown. USB2.0 and USB3.0 are quite dry, but I found OSDev Wikiand usbmadesimpleto be useful stepping stones.

不要气馁,但你确实需要在跳进去淹死之前弄湿你的脚。USB2.0 和 USB3.0 相当枯燥,但我发现OSDev Wikiusbmadesimple是有用的垫脚石。

If you still need to be in the PC, then you should consider trying to get a hold of some open source usb code for maybe a USB sniffer if you can find it.

如果你仍然需要在 PC 上,那么你应该考虑尝试获取一些开源 USB 代码,如果你能找到的话,可能是一个 USB 嗅探器。