问题
There is a custom USB device I need to communicate with. There is no particular driver for it. The device doesn't show as /dev/tty*
.
I found out how I can use libudev
to enumerate the USB devices and let's say I manage to get the corresponding struct udev_device
.
What I can't figure out is how I can open(2)
the device so I can write(2)
to and read(2)
from it.
Is it possible to communicate with a generic USB device, setting baudrate and just read/write? If so, what "file" should I open after I find the device with libudev
? If not, is writing a kernel-level driver necessary?
回答1:
libusb is an ideal choice. There are also several examples for such a case as you described.
In your case, you just need to:
- Create a default context.
- Request the device list.
- Search for the device with the desired vendor/product ID.
- Open the device, and then communicate via synchronous or asynchronous I/O calls.
- Clean up the requested device list.
- When done, close off the device handle and destroy the context.
This allows you to write a userspace application/library with relative ease. I've used it with microphones, cameras, and DIY hardware (mini assembly line devices I've built) with considerable success. The devs on the mailing list are also very helpful, though one or two fellows tend to be somewhat caustic to new members if their posts aren't perfectly written.
来源:https://stackoverflow.com/questions/32721359/how-to-enumerate-usb-devices-and-read-write-to-them