Apr-07-2021, 06:55 PM
I want to find out which ttyUSB port has been assigned to a specific device.
I know the name of the device, it is a generic "Prolific" USB to Serial adapter cable. Over the past few days, I have noticed that it is not always on the same port: The first time I looked, it was on ttyUSB4 and today it is on ttyUSB0.
So, I wrote a short, simple script with the aim of reading the file names of the symlinks in /dev/serial/by-id/, finding the one that has "Prolific" in and reporting back to me the ttyUSB number.
The way I have done it, is that the script runs the bash command to search the directory and save the results to a temporary file. It then reads the file, finds the line with "Prolific" in and prints out the full ttyUSB node name.
Afterwards, it then deletes the file.
I would like to somehow not create a file, rather hold the contents of the bash search results as a list/tuple/something-or-other...
Here is the script:
I know the name of the device, it is a generic "Prolific" USB to Serial adapter cable. Over the past few days, I have noticed that it is not always on the same port: The first time I looked, it was on ttyUSB4 and today it is on ttyUSB0.
So, I wrote a short, simple script with the aim of reading the file names of the symlinks in /dev/serial/by-id/, finding the one that has "Prolific" in and reporting back to me the ttyUSB number.
The way I have done it, is that the script runs the bash command to search the directory and save the results to a temporary file. It then reads the file, finds the line with "Prolific" in and prints out the full ttyUSB node name.
Afterwards, it then deletes the file.
I would like to somehow not create a file, rather hold the contents of the bash search results as a list/tuple/something-or-other...
Here is the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import os def main(): cmd = 'ls -l /dev/serial/by-id | grep -h "Prolific"> usb_devices.txt' os.system(cmd) with open ( 'usb_devices.txt' ) as f1: contents = f1.read() x = contents.find( "tty" ) port = (contents[x:]) if x = = - 1 : print ( "Not found" ) else : print (port) f1.close() os.remove( 'usb_devices.txt' ) # Restart? yay or nay? restart = input ( "Do you want to try again? Y/N: " ) print ("") if restart.lower() = = 'y' : main() else : print ( "Goodbye" ) main() |