0

I am running a custom C++ program on Raspbian that power cycles its USB ports every once in a while. However, I've noticed that the /dev/USB* enumeration is inconsistent between power cycles, and need to be able to handle this.

Are there any elegant ways to obtain a string such as "/dev/ttyUSB0" or "/dev/ttyUSB1" based on what's currently in /dev/? It's fair to assume there will only ever be one device connected, but it's not fair to assume what that device is aside from what's necessary for communication.

Things I've tried so far:

  1. dirent.h - seems clunky to grab all of "/dev/" and search for "ttyUSB"
  2. Forcing the USB device names to be static - I need my program to not be dependent on the device being used on the serial port

I'm not entirely-opposed to using system calls, but would like to avoid using "ls -l /dev/ttyUSB*" if possible.

Chris Mauer
  • 176
  • 1
  • 8
  • You should use udev rule to define your device. – tunglt Nov 19 '18 at 20:59
  • Unless I misunderstand something, that would require using device-specific information, which I would like to avoid due to the large number of devices this would need to support and maintain. I'd rather go the route of using an algorithm or library if possible. – Chris Mauer Nov 20 '18 at 21:09

1 Answers1

0

The best way to do this is to create a udev rule that will create a symlink when the device is plugged in. Your udev rule will look something like this:

ATTRS{interface}="usb_converter", SYMLINK+="custom_link"

Note that there are many more options under ATTRS that you can use to match against a specific USB device. You can get information such as the PID/VID, serial number of the device, manu7facturer, etc. If your USB to serial converter is one made by FTDI, generally what I do is to use FTPROG to reprogram the device and give it a unique interface name(in this case, usb_converter) and then match off of that to ensure that it will always come up the same.

See this answer for more information, as well as this guide for writing udev rules.

rm5248
  • 2,590
  • 3
  • 17
  • 16
  • I think you misunderstood part of the question: "It's fair to assume there will only ever be one device connected, but it's not fair to assume what that device is aside from what's necessary for communication" There will be many different devices being plugged inserted into this, and I wanted to avoid the route of inserting device-specific information, such as the serial number/manufacturer/etc. I'm more interested in libraries and algorithms that will save me from having to maintain a large list of supported devices. – Chris Mauer Nov 20 '18 at 21:04