So I've got a serial device, however the serial port changes most times I reconnect it. What I want to do is to list all serial ports, iterate over them until I find the correct one, and then connect to that one. I know that I can list all ports from the commandline like:
$ python -m serial.tools.list_ports
/dev/cu.YYYY
/dev/cu.XXXX
ZZZZ ports found
I want to do that from a python, but this doesn't work:
>>> import serial
>>> for port in serial.tools.list_ports:
... print(f'Current port: {port}')
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'serial' has no attribute 'tools'
What can I do? I really don't want to execute python -m serial.tools.list_ports as an external shell command, as it just seems silly since the library already has an API for python scripts.
Thanks!