I am looking for a solution to programmatically return all available serial ports with python.
At the moment I am entering ls /dev/tty.*
or ls /dev/cu.*
into the terminal to list ports and hardcoding them into the pyserial class.
I am looking for a solution to programmatically return all available serial ports with python.
At the moment I am entering ls /dev/tty.*
or ls /dev/cu.*
into the terminal to list ports and hardcoding them into the pyserial class.
You could do something like this:
import glob
def scan():
return glob.glob('/dev/tty*') + glob.glob('/dev/cu*')
for port in scan():
# do something to check this port is open.
Then, take a look at pyserial for some good utility functions to check if a port is open and so forth.
What about just doing the os.listdir
/ glob
equivalent of ls
to perform the equivalent of that ls
? Of course it's not going to be the case that some usable device is connected to each such special file (but, that holds for ls
as well;-), but for "finding all serial ports", as you ask in your Q's title, I'm not sure how else you might proceed.