Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for ports already in use before connecting to serial port #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pyfirmata/pyfirmata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down