Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to most properly use libusb to talk to connected USB devices?

Tags:

How do I most properly use libusb to talk to connected USB devices?

Specifically, how do I transfer data to the USB devices, receive information from the devices, find out the name of the connected device, if they have storage, etc.

More specifically, I will be running this on a Mac OS X machine, so I know I can't just use Windows header files.

If there is a good explanation on libusb and USB devices, that would be helpful too.

like image 521
user473973 Avatar asked Jun 21 '13 16:06

user473973


People also ask

How does libusb work?

Libusb allows you to enumerate devices and select the one you want based on a specific Vendor/Product id (V/P Id). If you don't know this and can't find it online with the product's description then you can easily find it. If it is not online you will need to use an app similar to lsusb on Linux.


2 Answers

Here is a post on a similar question that might be useful to you. I include plenty of links.

But maybe you'd rather see it here. So in that case, here it goes!

Libusb allows you to enumerate devices and select the one you want based on a specific Vendor/Product id (V/P Id). If you don't know this and can't find it online with the product's description then you can easily find it.

If it is not online you will need to use an app similar to lsusb on Linux. (I don't believe it is on Mac.) When you run lsusb it lists connected devices and their V/P Ids. You can easily find your device by unplugging, running lsusb, and plugging the device back in and comparing. It's a piece of cake. Any usb list app on Mac will hopefully display the V/P ID like lsusb does.

Then once you have this V/P ID you will use libusb (if using 0.1) to enumerate all devices and find the device that matches that id. (I support using libusbx which happens to have a single find device function based on V/P id - in fact, libusbx is a whole lot more concise all around.)

After selecting your device you will send a packet using either Feature or Output Reports. This is the most complicated part because the packet you send is dependent on the individual device I believe. It is 8 bytes of data and only one of which is a single character you wish to send to the usb device. (If you wanted to send 8 characters, you would have to loop through an array of chars and send a feature or output report for each character.)

As an example feel free to reference a rather specific terminal example I wrote for controlling two LEDS. If it's helpful, great! It contains a libusbx and libusb-0.1 example.

I hope this helps!

like image 164
eatonphil Avatar answered Sep 30 '22 17:09

eatonphil


The process that you can follow is:

  1. Get the VID, PID for the device that you want to communicate using lsusb
  2. Try to open the device and read the device descriptor
  3. If you want name of the device use string descriptor to get that
  4. Check if any kernel driver is attached. If it is, then detach it and do some raw data transfer
  5. After getting the response again re-attach the driver.
like image 38
freeworld Avatar answered Sep 30 '22 16:09

freeworld