简体   繁体   中英

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?

What I want to do is communicate with a microcontroller, for example, that doesn't have a USB driver on board via 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.

Basically what I want to be able to do is treat that USB port like a serial port. 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.

Edit: BTW I'm using Windows 7

The usual solution is to use an FTDI USB-to-serial chip, such as the FT232R , on your microcontroller board. 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.

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. Unplugging and replugging, even weeks later, gets the same number, but it's a different number on a different Mac.

On Windows, it shows up as a new COM port if you have the drivers installed, IIRC. 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. This can get tricky if you plug in more than one FTDI chip.

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 ). Most OSs have support for this standard USB device class.

Use one of the USB to serial port adapters. The USB port will look just like a serial port to your C++ program.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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