Skip to content

Commit

Permalink
Auto "--port" selection could be improved (#25)
Browse files Browse the repository at this point in the history
When the `sa818.py` command is exec'd without specifying an explicit port then
a list of default device paths is scanned ("/dev/serial0", "/dev/ttyUSB0").
Unfortunately, the scan ends one once "a" device path has been found regardless
of whether the path is associated with an SA818. The `sa818.py` command, knowing
how to probe for an SA818, can continue scanning all of the known paths and in
many cases will find the device without it needing to be specified.

This change improves the default port scan.
  • Loading branch information
Allan-N authored Nov 20, 2024
1 parent e834a37 commit 9fb8e0f
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions sa818.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,30 @@ def __init__(self, port=None, baud=DEFAULT_BAUDRATE):

for _port in ports:
try:
# Try to open the serial port
self.serial = serial.Serial(port=_port, baudrate=baud,
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=self.READ_TIMEOUT)
logger.debug(self.serial)
break

# Send initialization command and check the reply
self.send(self.INIT)
reply = self.readline()
if reply == "+DMOCONNECT:0":
# if expected response, proceed
break
else:
# if unexpected response, try another port
logger.debug("Port %s not SA818: %s", _port, reply)
self.serial.close()
self.serial = None

except serial.SerialException as err:
logger.debug(err)
logger.debug("Port %s not available: %s", _port, err)
self.serial = None

if not isinstance(self.serial, serial.Serial):
raise IOError('Error openning the serial port')

self.send(self.INIT)
reply = self.readline()
if reply != "+DMOCONNECT:0":
raise SystemError('Connection error') from None

def close(self):
Expand Down

0 comments on commit 9fb8e0f

Please sign in to comment.