LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Have multiple USB<->RS2332 devices. Which /dev/ttyUSBx goes to each device? (https://www.linuxquestions.org/questions/programming-9/have-multiple-usb-rs2332-devices-which-dev-ttyusbx-goes-to-each-device-4175715605/)

intestinal fortitude 08-12-2022 08:50 AM

Have multiple USB<->RS2332 devices. Which /dev/ttyUSBx goes to each device?
 
I have multiple USB<->serial devices. They use the FTDI chip. As each USB device is plugged in, they are correctly detected by the kernel and given a serial port, /dev/ttyUSB0, /dev/ttyUSB1, etc.

Devices can be plugged in any order, they can be unplugged, and plugged in again, etc. How do I map which serial port is which USB device? i.e. I want to ask the kernel which /dev/ttyUSBx device it assigned to a specific USB<->serial device.

This question has come up a few times on stackexchange. Using shell scripting, it can be done by following directory links in /sys/bus/usb/devices, looking for a line in uevent files, etc. I want to do this from C or C++, and was wondering if there's system calls I can use to query the kernel/driver, instead of searching directories for files, parsing links and open/reading/parsing text files.

I am familiar with libusb, so I already know how to get the list of USB devices and find my devices: bus #, device #, ID, manufacturer string, serial number string, etc.

Thanks!

dugan 08-12-2022 04:12 PM

/sys is the interface.

Remember, everything is a file, yadda yadda.

michaelk 08-12-2022 05:10 PM

You should be able to write a udev rule based on the serial number to assign a particular device ID to each adapter.

Code:

SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="xxxx", ATTRS{serial}=="xxxx", SYMLINK+="ttyUSBX"
Using udevadm you can see all the attributes for each particular device.

udevadm info --name=/dev/ttyUSB0 --attribute-walk

intestinal fortitude 08-16-2022 10:59 AM

Thanks for the help!

IMO, having a separate /dev/ttyUSBx for each device is the cleaner solution. I am not allowed to make any changes to system configuration files (for reasons of work policy).

I will parse the files in /sys.

GREYES71 11-01-2022 05:23 PM

Quote:

Originally Posted by intestinal fortitude (Post 6374090)
Thanks for the help!

IMO, having a separate /dev/ttyUSBx for each device is the cleaner solution. I am not allowed to make any changes to system configuration files (for reasons of work policy).

I will parse the files in /sys.

Hi

I have a question.
What application do you use to communicate the host with the device?
Some derivative of SynCE? What version of Linux do you use? Device?

Thanks in advance

intestinal fortitude 11-02-2022 06:45 AM

I am writing the application that communicates with the devices. The application is written in C++.

One of the devices is the Thorlabs FW102C filter wheel. When plugged into the USB port, its USB chip is detected by the kernel as a USB<->RS232 bridge, and the FTDI device-driver creates a device-file named /dev/ttyUSBx (where 'x' is the next available number). The device-file can be opened and used to communicate with the device as if it was a classic serial port.

The system is Redhat Enterprise Linux, but the application I'm developing is distribution-agnostic.

sundialsvcs 11-02-2022 08:45 AM

Remember that "directories" such as /sys, /dev, /proc, are actually kernel interfaces. They do not correspond to any physical resource although they appear to be "directories" containing "files." It's all done by the kernel. Some of the files can be written to, usually only by root, and when this happen it causes changes to occur. I have never before seen anything as simply elegant as this.


All times are GMT -5. The time now is 07:18 AM.