Commit 7ee0725454b5b59c4098952673b5d48026172209

Authored by okahilak
Committed by GitHub
1 parent 465096eb
Exists in master

MOD: Publish event when serial port is connected or disconnected (#329)

- Also, move code around a bit so that when a pulse is triggered into
  the serial port, publishing the corresponding event is done inside
  SerialPortConnection class.
invesalius/data/serial_port_connection.py
... ... @@ -50,19 +50,25 @@ class SerialPortConnection(threading.Thread):
50 50 import serial
51 51 self.connection = serial.Serial(self.port, baudrate=115200, timeout=0)
52 52 print("Connection to port {} opened.".format(self.port))
  53 +
  54 + Publisher.sendMessage('Serial port connection', state=True)
53 55 except:
54 56 print("Serial port init error: Connecting to port {} failed.".format(self.port))
55 57  
  58 + def Disconnect(self):
  59 + if self.connection:
  60 + self.connection.close()
  61 + print("Connection to port {} closed.".format(self.port))
  62 +
  63 + Publisher.sendMessage('Serial port connection', state=False)
  64 +
56 65 def SendPulse(self):
57   - success = False
58 66 try:
59 67 self.connection.write(self.BINARY_PULSE)
60   - success = True
  68 + Publisher.sendMessage('Serial port pulse triggered')
61 69 except:
62 70 print("Error: Serial port could not be written into.")
63 71  
64   - return success
65   -
66 72 def run(self):
67 73 while not self.event.is_set():
68 74 trigger_on = False
... ... @@ -97,6 +103,4 @@ class SerialPortConnection(threading.Thread):
97 103 #
98 104 time.sleep(0.3)
99 105 else:
100   - if self.connection:
101   - self.connection.close()
102   - print("Connection to port {} closed.".format(self.port))
  106 + self.Disconnect()
... ...
invesalius/gui/task_navigator.py
... ... @@ -386,9 +386,7 @@ class Navigation():
386 386  
387 387 def PedalStateChanged(self, state):
388 388 if state is True and self.coil_at_target and self.SerialPortEnabled():
389   - success = self.serial_port_connection.SendPulse()
390   - if success:
391   - Publisher.sendMessage('Pulse triggered', state=True)
  389 + self.serial_port_connection.SendPulse()
392 390  
393 391 def StartNavigation(self, tracker):
394 392 tracker_fiducials, tracker_fiducials_raw = tracker.GetTrackerFiducials()
... ...