简体   繁体   English

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

[英]Receiving data from a USB device in C or C++

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中使用。

This is a very persistent question in forums and programming Q+A sites. 这是论坛和编程Q + A网站中一个非常持久的问题。 Never with a happy ending. 从来没有幸福的结局。 The B in USB means bus. USB中的B表示总线。 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. 它与您机器内的PCI(快速)总线的作用完全相同。 Since it is an electrical specification first and foremost, USB supports a very large number of types of devices. 由于它首先是电气规范,因此USB支持非常多种类型的设备。 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. 几乎没有哪种设备能够很好地处理需要非常大带宽的设备,比如视频适配器。

The USB specification has a very elegant protocol specification that describes how devices can share the bus and how they can exchange data. USB规范有一个非常优雅的协议规范,描述了设备如何共享总线以及它们如何交换数据。 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. 设备本身取决于这些字节的含义。

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. 显然,视频驱动程序与NIC驱动程序非常不同。 The same is true for USB drivers, there is little commonality. 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. 如果你想编写类似USB设备的软件,那么你需要在它们仍然有共同点的层面上编写它。 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. 这是在USB控制器级别,您可以编写一个过滤器驱动程序,将自身注入USB驱动程序堆栈并查看控制器和设备驱动程序之间的I / O请求数据包。 Similar to, say, the winpcap filter driver that spies on TCP/IP traffic. 类似于,例如,窥探TCP / IP流量的winpcap过滤器驱动程序。 There isn't much of anything interesting to see though, you'd be staring at the blobs of bytes that pass back and forth. 虽然看起来没有什么有趣的东西,你会盯着来回传递的字节blob。 It is a much bigger problem than winpcap, at least it sees bytes fly by whose meaning is documented somewhere in an RFC. 这是一个比winpcap更大的问题,至少它看到字节飞行的意思记录在RFC的某处。 That's not the case for USB, the company that makes the USB device is also usually the device driver supplier. USB不是这种情况,制造USB设备的公司通常也是设备驱动器供应商。 They keep the internal format undocumented. 他们保持内部格式没有记录。

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驱动程序已经进入第3环(用户模式)以保持操作系统稳定。

To get started, download the Windows WDK (aka "DDK") and read Walter Oney's books. 要开始使用,请下载Windows WDK(又名“DDK”)并阅读Walter Oney的书籍。 Preferably all of them. 最好全部。

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? 如何编写USB程序,想要了解它的背景,同时又不想阅读它,不想同时使用库? 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: 有一个来自Stefan Meyer的项目“libUSB Win32”目前没有经过大量的开发,但用C语言写的可能这对你来说很有用,它也有能力在古老的​​windows版本下运行;)你可以在这里找到它:
http://sourceforge.net/apps/trac/libusb-win32/wiki 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: 前段时间我在VB6中写了这个使用“libUSB Win32”不知道它是否对你有用:
http://www.activevb.de/cgi-bin/upload/download.pl?id=3061 http://www.activevb.de/cgi-bin/upload/download.pl?id=3061

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有很大的支持。

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. USB并不容易,相信我......我正在研究USB项目,但是从后端开始。 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. 在我看来,你也需要关注后端,这是你真正无需处理图书馆的所有内容,但无论你做什么,你都无法绕过阅读。

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 Wiki and usbmadesimple to 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嗅探器。

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. 我知道你不想写驱动程序,但这就是驱动程序所做的:从设备接收数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM