Skip to content Skip to sidebar Skip to footer

Macpython: Programmatically Finding All Serial Ports

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 p

Solution 1:

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 isopen.

Then, take a look at pyserial for some good utility functions to check if a port is open and so forth.

Solution 2:

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.

Post a Comment for "Macpython: Programmatically Finding All Serial Ports"