Skip to content
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
15 changes: 12 additions & 3 deletions src/ethopy/interfaces/Arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@


class Arduino(Interface):
thread_end, msg_queue, callbacks = threading.Event(), PriorityQueue(maxsize=1), True

def __init__(self, **kwargs):
if not globals()["IMPORT_SERIAL"]:
raise ImportError(
Expand All @@ -37,9 +35,11 @@ def __init__(self, **kwargs):
self.timeout = 0.001
self.no_response = False
self.timeout_timer = time.time()
self.thread_end = threading.Event()
self.msg_queue = PriorityQueue(maxsize=1)
self.ser = Serial(self.port, baudrate=self.baud)
time.sleep(1)
self.thread_runner = threading.Thread(target=self._communicator)
self.thread_runner = threading.Thread(target=self._communicator, daemon=True)
self.thread_runner.start()

def give_liquid(self, port, duration=False):
Expand Down Expand Up @@ -94,6 +94,15 @@ def off_proximity(self):
return not self.position.state

def cleanup(self):
"""Stop the communicator thread and close the serial connection.

The thread must be stopped before the serial port is closed.
"""
self.thread_end.set()
if self.thread_runner.is_alive():
self.thread_runner.join(timeout=2)
if self.thread_runner.is_alive():
log.warning("Arduino communicator thread did not stop in time.")
self.ser.close() # Close the Serial connection

def setup_touch_exit(self):
Expand Down
Loading