Untested idea to abstract the port creation into a dictionary.
The basic idea is that we would keep a dictionary with the ports themselves and the path associated with them as the key of the dictionary (we can also do something like "port1" as the key).
It should be easy to check the length of this thing for the configured ports, to check whether they are active or not and remove them appropriately.
def configPort(portdict, portpath):
# initialize serial port objects
if portpath is not None and portpath[0:4] == "/dev/":
newport = serial.Serial(
port = portpath,
baudrate = baud,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = timeout,
)
portdict[portpath] = newport
return portdict
We could have an initialization call that looks like the one below and pass the portpath as a list instead of a hardcoded value of the function setup or generate the list ourselves
ports = {}
portpaths = [""] # hardcoded version
# see https://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python
import serial.tools.list_ports
print(list(serial.tools.list_ports.comports()))
portpaths = serial.tools.list_ports.comports()
def setup(ports, portpaths):
for portpath in portpaths:
ports = configPort(ports, portpath)
...
Regarding
if portpath is not None and portpath[0:4] == "/dev/":
Is this the best way to check whether this is actually a port we can connect to?
Untested idea to abstract the port creation into a dictionary. The basic idea is that we would keep a dictionary with the ports themselves and the path associated with them as the key of the dictionary (we can also do something like "port1" as the key). It should be easy to check the length of this thing for the configured ports, to check whether they are active or not and remove them appropriately.
We could have an initialization call that looks like the one below and pass the
portpath
as a list instead of a hardcoded value of the functionsetup
or generate the list ourselvesRegarding
Is this the best way to check whether this is actually a port we can connect to?