0

I'm writing a small C application to run on my (Linux) QNAP NAS that will talk to an Arduino (I have no difficulty with any of the USB code for the arduino). (The arduino has a trusted application on it that accepts text commands via USB serial.)

My wish was to find it using the USB vendor/product IDs (only half implemented at the moment). What I have so far (see below) works quite nicely in that it does find the device.

// runs on NAS
#include <stdio.h>
#include <usb.h>

main () {
  struct usb_bus *bus;
  struct usb_device *dev;
  usb_init();
  usb_find_busses();
  usb_find_devices();
  for (bus = usb_busses; bus; bus = bus->next) {
    for (dev = bus->devices; dev; dev = dev->next) {
      printf("Trying %s/%s\n", bus->dirname, dev->filename);
      printf("\tVendor = 0x%04x\n", dev->descriptor.idVendor);
      printf("\tBus = 0x%03x\n", bus->location);
      printf("\tFile = %s\n", dev->filename);
      if (dev->descriptor.idVendor==0x403) {
        printf("\t  HEY, THIS IS MINE!\n");
        usb_dev_handle *handle = usb_open(dev);
        printf("\t   HANDLE 0x%08x\n", (int) handle);
        //printf(handle, "1,5,62,75\n");
        usb_close(handle);
      }
    }
  }
}

The trouble is that now I want to send/receive a little bit of text with the device and I don't know how to do that.

I have expected I should be generating a device name from something in the usb_device struct and then open it like a file (like one would do on Windows).

If that's correct, I need to know the correct way to find out the device name...

I believe I'm using libusb.

I happen to know that -- as currently configured/connected -- it's ttyUSB0 but I'd like to know that using code.

thank you!

Alex P.
  • 30,437
  • 17
  • 118
  • 169
X-Ray
  • 2,816
  • 1
  • 37
  • 66
  • I recall reading similar Qs on this topic. searching for `[c] libusb vendor id` shows 6 possible threads. Good luck. – shellter Nov 12 '16 at 03:07

2 Answers2

2

I suggest using libusbp. It is a C library with a C++ wrapper, and there is example code showing how to get the name of a serial port based on the vendor ID and product ID of the USB device:

https://github.com/pololu/libusbp/blob/master/examples/port_name/port_name.cpp

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Thank you for this; it looked promising but my weak linux/gcc skills & experience are making it quite difficult to get things working. "libusbp.hpp: no such file or directory". I would have thought that by providing -lLibUSBP -rpath=/src/LibUSBP -L/src/LibUSBP command line parameters would make that work. Clearly, I don't know enough about gcc. – X-Ray Nov 12 '16 at 16:14
  • Linux filenames are case sensitive and you got the case wrong in your command. See the libusbp README, which discusses how to install it and compile a program that uses it. – David Grayson Nov 12 '16 at 19:27
  • Thank you for the extra information. Couldn't get it working; clearly my lack of understanding of gcc and how it finds libraries is quite weak. I know too little to fool around with this stuff. I have decided to completely skip this part. – X-Ray Nov 14 '16 at 05:30
  • 1
    I have checked out `libusbp` and turns out it does use `libudev` for looking up the serial port names on Linux – Alex P. Jan 08 '17 at 16:24
0

libusb is a library used for low level communication with USB devices. But when your communication is limited just to reading device's USB descriptor - libusb is not the best tool.

Linux systems use udev subsystem to manage hot-plug devices. udev reads descriptors of all plugged USB devices and stores them in its database. libudev is the library that you should use to get such info like device names of enumerated devices. In your case you need to remember that usb_device and usb_interface are separate things - only the latter would have the tty device file assigned to it.

Alternatively you could just use udev config to assign a constant device name to your specific device. So you would not have to be looking for it.

Alex P.
  • 30,437
  • 17
  • 118
  • 169