Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-12-2022, 08:50 AM
|
#1
|
LQ Newbie
Registered: Jan 2014
Posts: 19
Rep: 
|
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!
|
|
|
08-12-2022, 04:12 PM
|
#2
|
LQ Guru
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,352
|
/sys is the interface.
Remember, everything is a file, yadda yadda.
|
|
1 members found this post helpful.
|
08-12-2022, 05:10 PM
|
#3
|
Moderator
Registered: Aug 2002
Posts: 26,757
|
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
|
|
2 members found this post helpful.
|
08-16-2022, 10:59 AM
|
#4
|
LQ Newbie
Registered: Jan 2014
Posts: 19
Original Poster
Rep: 
|
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.
|
|
|
11-01-2022, 05:23 PM
|
#5
|
LQ Newbie
Registered: Jul 2014
Location: Buenos Aires, Argentina
Distribution: Debian [Wheezy-Jessie] && Ubuntu [Hardy-Lucid-Trusty] && Suse [9~10.3] && Mandrake [5.2 Leeloo~10.1]
Posts: 9
Rep: 
|
Quote:
Originally Posted by intestinal fortitude
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
|
|
|
11-02-2022, 06:45 AM
|
#6
|
LQ Newbie
Registered: Jan 2014
Posts: 19
Original Poster
Rep: 
|
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.
Last edited by intestinal fortitude; 11-02-2022 at 12:23 PM.
|
|
1 members found this post helpful.
|
11-02-2022, 08:45 AM
|
#7
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,201
|
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.
Last edited by sundialsvcs; 11-02-2022 at 08:46 AM.
|
|
|
08-27-2024, 08:30 PM
|
#8
|
LQ Newbie
Registered: Jan 2014
Posts: 19
Original Poster
Rep: 
|
Shortly after originally posting, the project got shelved for a while. I've recently been able to continue work on it. This is some pseudo-code on how I resolved the problem:
Code:
Use libusb to enumerate all of the attached USB devices
Loop through device list:
Search the list of attached devices for one with matching Vendor ID and Hardware ID
Read the serial number. Move to next device if it doesn't match.
If there is a matching entry in the USB device list:
Call libusb_get_bus_number() to get the device's bus number
Call libusb_get_port_number() to get the device's port number
Call opendir("/sys/bus/usb-serial/devices")
Each entry in the directory is a symbolic link into the corresponding USB device's hierarchy
Loop through the directory contents with readdir():
Use readlink() on each entry to get its target
Look for an entry that is a symbolic link to the USB device with matching bus & port numbers
If there's a matching entry, then the name of the file in /sys/bus/usb-serial/devices is the name of the USB-tty device.
Example: After plugging in the device and searching the list of devices, the program determines it is USB Bus 1, Port 8. Directory listing of /sys/bus/usb-serial/devices
Code:
localhost$ ls -l /sys/bus/usb-serial/devices
total 0
lrwxrwxrwx 1 root root 0 Aug 27 19:59 ttyUSB0 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8:1.0/ttyUSB0
As you can see, ttyUSB0 points to USB device Bus 1, Port 8. So I know that /dev/ttyUSB0 is the serial port that corresponds to my USB device. An alternative directory that I could have used is /dev/serial/by-path:
Code:
% ls -l /dev/serial/by-path
total 0
lrwxrwxrwx 1 root root 13 Aug 27 19:59 pci-0000:00:14.0-usb-0:8:1.0-port0 -> ../../ttyUSB0
The directory /dev/serial/by-path has the opposite mapping as /sys/bus/usb-serial/devices (USB-to-tty vs. tty-to-USB). I decided to stick with /sys/bus/usb-serial/devices because the fact it is in /sys makes me feel a bit more warm and fuzzy.
|
|
1 members found this post helpful.
|
08-28-2024, 11:51 AM
|
#9
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,201
|
I think that your solution is certainly the most "robust." And, easiest to manage. (Because, the entire resolution is handled "within your code," versus dependency upon some external configuration which "your code" could not check.)
|
|
|
08-28-2024, 06:10 PM
|
#10
|
Moderator
Registered: Aug 2002
Posts: 26,757
|
If the same cable/device was always plugged into the same port I believe ../usb-serial/devices would work but I assumed by your first post by any order that was not the case.
|
|
|
All times are GMT -5. The time now is 04:01 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|