使用C或C ++从USB设备接收数据 [英] Receiving data from a USB device in C or C++

查看:166
本文介绍了使用C或C ++从USB设备接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我可以开始使用我的程序中的数据。

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

我不想使用库的,只有标准的C ++函数,该程序应该在Windows 98中工作。 p>

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

推荐答案

这是一个在论坛和编程Q + A网站的非常从来没有一个快乐的结局。 USB中的B表示总线。这是在计算机硬件设计中的术语,以描述用于电子设备交换数据的电接口。它与机器中的PCI(快速)总线具有完全相同的作用。由于它是电气规范的首要和最重要的,USB支持非常大量类型的设备。任何从无线网络适配器,调制解调器,闪存卡到茶壶加热器。只有那些不能很好处理的设备是需要非常大的带宽的设备,例如视频适配器。

This is a very persistent 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规范有非常优雅的协议规范,描述设备如何共享总线以及如何交换数据。然而,该协议规范根本不描述数据的格式,它仅定义能够传递字节的块的概念。

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.

在机器端,你需要软件来解释这些字节,并使机器做一些有趣的事情他们。这需要一个设备驱动程序。就像您的视频卡和您的网络接口卡需要设备驱动程序。显然,视频驱动程序与NIC驱动程序不同。对于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 very different from a NIC driver. The same is true for USB drivers, there is little commonality.

如果你想编写软件来处理类似的USB设备,那么你需要在它们的水平仍然有一些共同点。这是在USB控制器级别,您可以编写一个过滤器驱动程序,该驱动程序在USB驱动程序堆栈中注入自身,并查看控制器和设备驱动程序之间的I / O请求数据包。类似于,例如,winpcap过滤器驱动程序,窥探TCP / IP流量。有没有什么有趣的东西看,虽然,你会盯着传来的字节的blob。这是一个比winpcap更大的问题,至少它看到字节飞,其含义被记录在RFC的某个地方。这不是USB的情况,制造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.

编写过滤器驱动程序需要相当高级的技能,有很多痛点。喜欢崩溃的操作系统,当你犯了一个简单的错误。在Windows驱动程序模型中也有相当大的流量最近,USB驱动程序已经转移到环3(用户模式),以保持操作系统稳定。

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 WDK(又名DDK)并阅读Walter Oney的书。最好是所有的。

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

这篇关于使用C或C ++从USB设备接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆