matiasandina / FEDWatcher

Software and Hardware to connect FED3 devices over serial on Raspberry Pi 4
MIT License
4 stars 1 forks source link

Abstract the `serial.Serial` call. It's the same for all ports #1

Closed matiasandina closed 3 years ago

matiasandina commented 3 years ago

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?

matiasandina commented 3 years ago

This was already done. Ports will not change on a rpi so we can keep the hardcoded version :tada: