C++ USB 编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2823539/
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
C++ USB Programming
提问by JB_SO
I am new to hardware programming(especially USB) so please bear with me and my questions. I am using C++ and I need to send/receive some data (a byte array) to/from a USB port on a microprocessor board.
我是硬件编程(尤其是 USB)的新手,所以请耐心等待我和我的问题。我正在使用 C++,我需要向/从微处理器板上的 USB 端口发送/接收一些数据(字节数组)。
Now, I have done some serial port programming before and I know that for a serial port you have to open a port, setup, perform i/o and finally close the port. I am guessing to use a USB port, it is not as simple as what I mentioned above.
现在,我以前做过一些串口编程,我知道对于一个串口,你必须打开一个端口,设置,执行 i/o,最后关闭端口。我猜想使用USB端口,它不像我上面提到的那么简单。
I do know that I want to use Microsoft standard drivers and implement standard Windows IO commands to accomplish this, since I believe there are no drivers for the microprocessor board for me to interact with.
我确实知道我想使用 Microsoft 标准驱动程序并实现标准的 Windows IO 命令来实现这一点,因为我相信微处理器板没有驱动程序供我与之交互。
If somebody can point me in the right direction as to the steps needed to "talk" to a USB port (open, setup, i/o) via standard Windows IO commands, I would truly and greatly appreciate it. Thanks you so much!!
如果有人能指出我通过标准 Windows IO 命令与 USB 端口(打开、设置、i/o)“对话”所需的步骤的正确方向,我将非常感谢。非常感谢!!
回答by Jon Cage
USB in a nutshellis excellent background reading.
简而言之,USB是出色的背景阅读。
For a generic USB module try LibUSBwhich includes some examples of talking to standard devices. LibUSB-Win32is the windows port of that library.
对于通用 USB 模块,请尝试LibUSB,其中包括一些与标准设备对话的示例。LibUSB-Win32是该库的 Windows 端口。
You're right though, it's nowhere nearas simple as an RS232 port.
你是正确的,虽然,它远不近那样简单的RS232端口。
What microprocessor are you using? Does it come with any USB hardware or are you thinking of trying to implement it all in software (not something I'd recommend even contemplating!). A lot of microprocessors come with code samples which can emulate simple HID USB devices like keyboards / mice etc. which you can use as a starting point. Failing that, there's probably a few off-the-shelf optionswhich will save you a lot of headaches :-)
你用的是什么微处理器?它是否带有任何 USB 硬件,或者您是否正在考虑尝试在软件中实现它(我什至不建议考虑考虑!)。许多微处理器都带有代码示例,可以模拟简单的 HID USB 设备,如键盘/鼠标等,您可以将其用作起点。否则,可能有一些现成的选项可以为您省去很多麻烦:-)
回答by SoftDeveloper
Find out the USB chipset used on the microprocessor board, e.g. Ftdi, Cypress, etc. These manufacturers supply drivers, libraries and example programs for the comms, this is the easiest way.
找出微处理器板上使用的 USB 芯片组,例如 Ftdi、Cypress 等。这些制造商为通信提供驱动程序、库和示例程序,这是最简单的方法。
There are no 'standard' drivers as such unless your board is set up as an HID device, however if you are using Windows, Microsoft supply a WinUSB.sys 'generic' driverwhich may or may not meet your needs (it doesn't support Isochronous transfers).
除非您的主板设置为 HID 设备,否则没有这样的“标准”驱动程序,但是如果您使用的是 Windows,Microsoft 会提供一个WinUSB.sys“通用”驱动程序,它可能满足也可能不满足您的需求(它不支持等时传输)。
回答by marshal craft
Should you write a driver for the USB device that will attach itself. If you know the type of chip that has the USB i/o on the board, the driver will be for that device.
您是否应该为将连接自身的 USB 设备编写驱动程序。如果您知道板上具有 USB i/o 的芯片类型,则驱动程序将用于该设备。
There may already be driver's for that chip if it is an STM or there may be an API to help develop drivers for it. It would probably be a custom device but you should look through device types to see if there is an existing type that matches your device. If so there may also be a driver already for it which you can use. Information about the board is necessary.
如果该芯片是 STM,则可能已经有驱动程序,或者可能有一个 API 来帮助为其开发驱动程序。它可能是一个自定义设备,但您应该查看设备类型以查看是否存在与您的设备匹配的现有类型。如果是这样,您也可以使用它的驱动程序。有关董事会的信息是必要的。
WRITE YOUR OWN DRIVERWith out getting too far into it, you should learn Microsoft's driver model, the difference between user mode and kernal mode drivers, what driver node's represent, driver hierarchy and how they logically attach themselves, different types of drivers (filter, function, bus), how kernal driver's communicate with user interface, dynamic link libraries, frameworks and API's, WDM framework/API, KMDF and UMDF API's and this is just a short list.
编写你自己的驱动程序不要太深入,你应该学习微软的驱动程序模型,用户模式和内核模式驱动程序之间的区别,驱动程序节点代表什么,驱动程序层次结构以及它们如何在逻辑上附加自己,不同类型的驱动程序(过滤器,函数、总线)、内核驱动程序如何与用户界面通信、动态链接库、框架和 API、WDM 框架/API、KMDF 和 UMDF API,这只是一个简短的列表。
ORyou can use an existing driver.
或者您可以使用现有的驱动程序。
USE USB MICROCONTROLLER SUPPLIED DRIVERWe do not know what firmware the USB microcontroller is using, if any. If there is no firmware (if say the chip came with out it or relies on eprom to store it and not flash) then if we knew if the chip supports usb bootloader, you can flash it or load it's firmware through the USB and there is no need for JTAG or other peripherials. Likely the usb alone is capable of getting you off the ground and anywhere from there. Typically these types of questions arise when considering board's with out a USB microcontroller.
使用 USB 微控制器提供的驱动程序我们不知道 USB 微控制器使用的是什么固件,如果有的话。如果没有固件(如果说芯片自带它或依靠 eprom 来存储它而不是闪存)那么如果我们知道芯片是否支持 USB 引导加载程序,您可以通过 USB 闪存或加载它的固件,并且有不需要 JTAG 或其他外设。可能仅靠 USB 就可以让您离开地面并从那里到任何地方。通常在考虑没有 USB 微控制器的电路板时会出现这些类型的问题。
Here is a link to a slighly related driver. Most USB microcontrollers serve the purpose of switching USB communication to UART as most microconrollers that actually do the thinking and control everything are not equiped with USB i/o but instead UART or some other i/o pins like I2C or SPI etc..
这是一个稍微相关的驱动程序的链接。大多数 USB 微控制器的目的是将 USB 通信切换到 UART,因为大多数实际进行思考和控制一切的微控制器都没有配备 USB i/o,而是配备了 UART 或其他一些 I/O 引脚,如 I2C 或 SPI 等。
https://www.silabs.com/products/mcu/Pages/USBXpress.aspx
https://www.silabs.com/products/mcu/Pages/USBXpress.aspx
That driver may actually work, if not something similar but for the manufacturer of your USB microcontroller; will almost certainly do the job. Hope this helps.
如果不是类似的东西,但对于 USB 微控制器的制造商,该驱动程序实际上可能有效;几乎肯定会完成这项工作。希望这可以帮助。
回答by Earlz
By hardware programming, do you mean you will be programming the microcontroller? If so, there is no way you will use "standard windows drivers" or anything like that.
通过硬件编程,您的意思是您将对微控制器进行编程吗?如果是这样,您将无法使用“标准 Windows 驱动程序”或类似的东西。
The USB standard is very thick. Check it out here
USB标准很厚。检查出来这里
It is a very involved topic and I recommend finding USB implementations already created for your microcontroller. (which we may be able to find if you gave us more info)
这是一个非常复杂的主题,我建议您查找已经为您的微控制器创建的 USB 实现。(如果您向我们提供更多信息,我们可能会找到)
回答by Thomas Matthews
What kind of microprocessor and what kind of board?
什么样的微处理器和什么样的板?
Many processors come with support for USB ports. Also, some hardware boards come with USB contollers too.
许多处理器都支持 USB 端口。此外,一些硬件板也带有 USB 控制器。
For example, the processor that my shop is using comes with two USB controllers that can act as a hub or as a device. We're even adding another processor to act as a USB controller.
例如,我的商店使用的处理器带有两个 USB 控制器,可用作集线器或设备。我们甚至添加了另一个处理器来充当 USB 控制器。