LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-12-2022, 08:50 AM   #1
intestinal fortitude
LQ Newbie
 
Registered: Jan 2014
Posts: 13

Rep: Reputation: Disabled
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!
 
Old 08-12-2022, 04:12 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
/sys is the interface.

Remember, everything is a file, yadda yadda.
 
1 members found this post helpful.
Old 08-12-2022, 05:10 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
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.
Old 08-16-2022, 10:59 AM   #4
intestinal fortitude
LQ Newbie
 
Registered: Jan 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-01-2022, 05:23 PM   #5
GREYES71
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: Reputation: Disabled
Quote:
Originally Posted by intestinal fortitude View Post
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
 
Old 11-02-2022, 06:45 AM   #6
intestinal fortitude
LQ Newbie
 
Registered: Jan 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
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.
Old 11-02-2022, 08:45 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] usb broadband modem has to be re-plug in order to be recognized as /dev/ttyUSBx 72rcfj789w93 Slackware 3 01-26-2018 02:34 PM
Difference between writing to /dev/ttyACMx and /dev/ttyUSBx mlalitha Linux - Newbie 6 01-19-2016 12:25 PM
[SOLVED] 3G modem not visible as /dev/ttyUSBX Aldebaran Slackware 4 01-27-2011 10:18 PM
Force a USB device to use a specific /dev/ttyUSBX garnser Linux - Software 4 02-10-2006 06:28 AM
USB pilot-link under Fedora Core 3: /dev/ttyUSBx there but not there reeder.29 Linux - Laptop and Netbook 2 10-17-2005 10:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration