简体   繁体   English

如何让 C++ 像串行端口一样从 USB 端口执行 I/O

[英]How to get C++ to perform I/O from a USB Port like a Serial Port

Is it possible to read/write from/to a specific USB port, avoiding all that USB handshaking stuff?是否可以从特定的 USB 端口读取/写入,避免所有 USB 握手的东西?

What I want to do is communicate with a microcontroller, for example, that doesn't have a USB driver on board via USB.我想要做的是与一个微控制器通信,例如,它没有通过 USB 板载 USB 驱动程序。 I want my C++ application to be able to send data via a specific USB port and then to receive data from that same USB port.我希望我的 C++ 应用程序能够通过特定的 USB 端口发送数据,然后从同一个 USB 端口接收数据。

Basically what I want to be able to do is treat that USB port like a serial port.基本上我想做的就是把 USB 端口当作串口来对待。 Is this possible?这可能吗? Is there a workaround I can use?有我可以使用的解决方法吗?

Thanks in advance.提前致谢。

PS: I know this isn't the best way to go about communication with a uC, but let's assume, for some reason, I have to do it this way. PS:我知道这不是 go 关于与 uC 通信的最佳方式,但我们假设,出于某种原因,我必须这样做。

Edit: BTW I'm using Windows 7编辑:顺便说一句,我正在使用 Windows 7

The usual solution is to use an FTDI USB-to-serial chip, such as the FT232R , on your microcontroller board.通常的解决方案是在您的微控制器板上使用 FTDI USB 转串行芯片,例如FT232R Then, as far as your MCU is concerned, it's talking to a serial port, and on the host machine you just have to have the right drivers.然后,就您的 MCU 而言,它正在与串行端口通信,并且在主机上您只需要拥有正确的驱动程序即可。

On my Mac, the FTDI chip shows up as a serial port at /dev/tty.usbmodem____ , where ____ is some persistent unique ID assigned by the Mac on first detection.在我的 Mac 上,FTDI 芯片显示为/dev/tty.usbmodem____的串行端口,其中____是 Mac 在第一次检测时分配的一些持久唯一 ID。 Unplugging and replugging, even weeks later, gets the same number, but it's a different number on a different Mac.即使在数周后拔出和重新插入,也会得到相同的号码,但在不同的 Mac 上却是不同的号码。

On Windows, it shows up as a new COM port if you have the drivers installed, IIRC.在 Windows 上,如果您安装了驱动程序 IIRC,它将显示为新的COM端口。 On Linux, it shows up as /dev/ttyUSB_ where _ is a serial number starting at 0, if you have FTDI serial driver support enabled in the kernel.在 Linux 上,它显示为/dev/ttyUSB_其中_是从 0 开始的序列号,如果您在 kernel 中启用了 FTDI 串行驱动程序支持。 This can get tricky if you plug in more than one FTDI chip.如果您插入多个 FTDI 芯片,这可能会变得很棘手。

If your microcontroller has a built-in USB port, then you need to find the software for it to identify itself as a standard CDC (Communication Device Class) serial port device ( standard documentation available ).如果您的微控制器有一个内置的 USB 端口,那么您需要找到软件,将其标识为标准 CDC(通信设备类)串行端口设备(提供标准文档)。 Most OSs have support for this standard USB device class.大多数操作系统都支持此标准 USB 设备 class。

Use one of the USB to serial port adapters.使用 USB 之一转串口适配器。 The USB port will look just like a serial port to your C++ program. USB 端口看起来就像 C++ 程序的串行端口。

In general you can do a close approximation of this using the HID interface: drivers are built in, and you can send feature reports in both directions.一般来说,您可以使用 HID 接口对此进行近似:内置驱动程序,您可以双向发送功能报告。 It isn't quite serial port simplicity (since it is packets, as opposed to a continuous byte stream) but hidapi makes it really simple and cross-platform.它不是很简单的串行端口(因为它是数据包,而不是连续的字节流),但hidapi使它非常简单和跨平台。

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

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