From 81a6615897ee1d927af3ede9bda0f026fb05d021 Mon Sep 17 00:00:00 2001 From: AB <43527304+01baftb@users.noreply.github.com> Date: Fri, 15 Jan 2021 22:51:57 -0500 Subject: [PATCH] Check for ports already in use before connecting to serial port --- pyfirmata/pyfirmata.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pyfirmata/pyfirmata.py b/pyfirmata/pyfirmata.py index 0963cea7..e32cc579 100755 --- a/pyfirmata/pyfirmata.py +++ b/pyfirmata/pyfirmata.py @@ -84,9 +84,26 @@ class Board(object): _command = None _stored_data = [] _parsing_sysex = False + _boards_in_use = [] def __init__(self, port, layout=None, baudrate=57600, name=None, timeout=None): - self.sp = serial.Serial(port, baudrate, timeout=timeout) + + # Check to see if port was previously opened. If not then connect to the port. + sp = None + for i in self._boards_in_use: + if i.port == port: + sp = i + break + if sp != None: + # Port already open. + # TODO: Determine if we should return or continue rest of the steps in the _init__ sequence + pass + else: + # The requested port is asked to be opened for the first time + self.sp = serial.Serial(port, baudrate, timeout=timeout) + # Keep track of all ports already created and in use + self._boards_in_use.append(self.sp) + # Allow 5 secs for Arduino's auto-reset to happen # Alas, Firmata blinks its version before printing it to serial # For 2.3, even 5 seconds might not be enough.