LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-18-2024, 06:16 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797

Rep: Reputation: 282Reputation: 282Reputation: 282
Determine serial port based on USB description (bash)


Hi all,

Not sure if this is the correct section to ask; moderators feel free to move it.

I have a scenario where I need to identify the serial port (/dev/ttyACMx) of a USB device based on USB description. There are a number of these devices connected to a computer; they have the same VID and PID but differ in USB description.

Is there a single bash command that I can use?

Without that, the process that I visualise is to use lsusb to find the connected devices by VID/PID to see if they are connected, use a filter on the output of dmesg (probably read /var/log/dmesg using tac) and find the usb description and associated device (/dev/ttyACMx). The sequence can be reversed, not sure what's better but I will figure that out in time.

Last edited by Wim Sturkenboom; 06-24-2024 at 10:45 PM. Reason: Marked as solved
 
Old 06-19-2024, 03:42 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,852

Rep: Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977Reputation: 5977
You should be able to write a udev rule to assign persistent device names based on some unique description for each serial port.

Code:
SUBSYSTEM=="tty",ENV{descriptor}=="descriptor value",SYMLINK+="unique_device_name"
Reference that particular USB serial port as /dev/unique_device_name

Based on the output of udevadm for each device.

udevadm info /dev/ttyACMx

I have not played with udev rules in a long time...
 
1 members found this post helpful.
Old 06-20-2024, 05:52 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Thanks for the reply and for the pointer to udevdm. I do not think that I need to modify rules; I also find it a little risky as I don't have experience with them (if that is what you suggested)

I however found a way. What I know about the devices is that they are always /dev/ttyACMx. So for starters I came up with the below which basically gives me all required info
Code:
for device in /dev/ttyACM*; do udevadm info --name $device; done
The output for ttyACM1
Code:
P: /devices/pci0000:00/0000:00:1d.7/usb1/1-5/1-5.4/1-5.4.2/1-5.4.2:1.0/tty/ttyACM1
N: ttyACM1
S: serial/by-id/usb-SparkFun_SparkFun_Pro_Micro22-if00
S: serial/by-path/pci-0000:00:1d.7-usb-0:5.4.2:1.0
E: DEVLINKS=/dev/serial/by-id/usb-SparkFun_SparkFun_Pro_Micro22-if00 /dev/serial/by-path/pci-0000:00:1d.7-usb-0:5.4.2:1.0
E: DEVNAME=/dev/ttyACM1
E: DEVPATH=/devices/pci0000:00/0000:00:1d.7/usb1/1-5/1-5.4/1-5.4.2/1-5.4.2:1.0/tty/ttyACM1
E: ID_BUS=usb
E: ID_MODEL=SparkFun_Pro_Micro22
E: ID_MODEL_ENC=SparkFun\x20Pro\x20Micro22
E: ID_MODEL_FROM_DATABASE=NM10/ICH7 Family USB2 EHCI Controller
E: ID_MODEL_ID=9204
E: ID_PATH=pci-0000:00:1d.7-usb-0:5.4.2:1.0
E: ID_PATH_TAG=pci-0000_00_1d_7-usb-0_5_4_2_1_0
E: ID_PCI_CLASS_FROM_DATABASE=Serial bus controller
E: ID_PCI_INTERFACE_FROM_DATABASE=EHCI
E: ID_PCI_SUBCLASS_FROM_DATABASE=USB controller
E: ID_REVISION=0100
E: ID_SERIAL=SparkFun_SparkFun_Pro_Micro22
E: ID_TYPE=generic
E: ID_USB_CLASS_FROM_DATABASE=Miscellaneous Device
E: ID_USB_DRIVER=cdc_acm
E: ID_USB_INTERFACES=:020200:0a0000:
E: ID_USB_INTERFACE_NUM=00
E: ID_USB_PROTOCOL_FROM_DATABASE=Interface Association
E: ID_VENDOR=SparkFun
E: ID_VENDOR_ENC=SparkFun
E: ID_VENDOR_FROM_DATABASE=Intel Corporation
E: ID_VENDOR_ID=1b4f
E: MAJOR=166
E: MINOR=1
E: SUBSYSTEM=tty
E: USEC_INITIALIZED=19858834082
I now need to puzzle how to extract the correct info; I did not find a direct way in the udevadm man page. It's basically the ID_MODEL and/or ID_MODEL_ENC. grep and awk come to mind although it's ages ago that I used *awk* ( the book is already dusted of )

Note
I can modify the MODEL_ID with the code that I program in the USB device. So each USB device has its own model.
 
Old 06-22-2024, 10:28 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
The devices no identify themselves slightly different

/dev/ttyACM0
Code:
E: ID_MODEL=SparkFun_Pro_Micro__1_
E: ID_MODEL_ENC=SparkFun\x20Pro\x20Micro\x20\x281\x29
and /dev/ttyACM2
Code:
E: ID_MODEL=SparkFun_Pro_Micro__2_
E: ID_MODEL_ENC=SparkFun\x20Pro\x20Micro\x20\x282\x29
After a serious battle with hex values I came up with the below solution
Code:
# check for arguments
if [ $# != 1 ]
then
  echo "No argument"
  exit -1
fi

emptyText=""

# count number of matching devices
count=0
# final port (device) to use
port=""

# loop through all ttyACM devices
for device in /dev/ttyACM*;
do
  # get info for devicesw 
  udevDevice=$(udevadm info --name $device)
  #echo "udevDevice = "
  #echo "$udevDevice"
  # grep what we're looking for
  udevGrep=$(echo -e "$udevDevice" |grep "$1");
  #echo "udevGrep = "
  #echo "$udevGrep"

  # if we have a match
  if [ "$udevGrep" != "$emptyText" ]
  then
    # assign to port
    port=$device
    #echo "$device"
    #echo "$result"
    # update count of matching devices
    ((count++))
  fi
done

# check results
if [[ $count -eq 0 ]]
then
  echo "No matching device found"
  exit -2
else
  if [[ $count -gt 1 ]]
  then
    echo "Multiple devices found"
    echo "$count"
    exit -3
  else
    echo -n "Device '"
    echo -n "$1"
    echo -n "' on port '"
    echo -n "$port"
    echo "'"
  fi
fi

exit 0
For now it will do; I might have overlooked some possible scenarios and the use of $1 as regexp might need a bit of fine tuning.

Typical output
Code:
$ ./findtty.sh "Micro (2)"
Device 'Micro (2)' on port '/dev/ttyACM2'

$ ./findtty.sh "Micro"
Multiple devices found
2
Again thanks for the pointer to udevadm.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
kernel oops on serial port/serial port acting erratically. mR_Slug Linux - Hardware 2 01-15-2024 02:48 PM
How to Communicate Virtual Serial Port on Host(windows) to Communicate with Virtual Serial Port on Guest System(Linux) aquamarine Linux - Newbie 2 09-16-2016 02:48 PM
[SOLVED] Serial terminal connected to USB Serial Port on Ubuntu 12.04 809areacode Linux - General 3 07-18-2013 10:06 PM
Is a USBtty (USB serial port) treated the same as tty (normal serial port) in C? spudgunner Programming 1 11-12-2010 01:19 PM
Atmel based USB stick found, but no HW description? Recirqie Linux - Wireless Networking 0 01-05-2005 04:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:06 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