Skip to content

Commit

Permalink
Toward disconnection managment, v3
Browse files Browse the repository at this point in the history
  • Loading branch information
pzaffino committed Jan 31, 2024
1 parent 59cf3fb commit 6367261
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions ArduinoConnect/ArduinoConnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ def deviceError(title, message, error_type="warning"):
deviceMBox.setText(message)
deviceMBox.exec()

def connectionError():
connectionMBox = qt.QMessageBox()
connectionMBox.setIcon(qt.QMessageBox().Critical)
connectionMBox.setWindowTitle("Connection dropped!")
connectionMBox.setText("Do you want to try reconnecting?")
connectionMBox.setStandardButtons(qt.QMessageBox.Yes)
connectionMBox.addButton(qt.QMessageBox.No)
connectionMBox.setDefaultButton(qt.QMessageBox.Yes)
if connectionMBox.exec() == qt.QMessageBox.Yes:
return True
else:
return False

#
# ArduinoAppTemplate
#
Expand Down Expand Up @@ -363,7 +376,6 @@ def __init__(self):
slicer.mrmlScene.AddNode(self.parameterNode)

self.arduinoConnection = None
self.disconnectedByUser = False

def sendMessage(self, messageToSend):
if self.arduinoConnection is not None:
Expand All @@ -384,6 +396,7 @@ def connect(self, port=None, baud=None, samplesPerSecond=None):

try:
self.arduinoConnection = serial.Serial(self.port, self.baud)
self.disconnectedByUser = False
except serial.serialutil.SerialException:
return False

Expand Down Expand Up @@ -419,11 +432,12 @@ def pollSerialDevice(self):
except (IOError, AttributeError):
self.disconnect(disconnectedByUser=False)

deviceError("Critical error", "Connection has dropped!\nClick OK to try connect again", "critical")
reconnected = self.connect()
userWantsToReconnect = connectionError()
if userWantsToReconnect:
reconnected = self.connect()

if not reconnected:
qt.QTimer.singleShot(1000/self.arduinoRefreshRateFps, self.pollSerialDevice)
if not reconnected:
qt.QTimer.singleShot(1000/self.arduinoRefreshRateFps, self.pollSerialDevice)

def processMessage(self, msg):
return msg
Expand Down

0 comments on commit 6367261

Please sign in to comment.