1 from PySide2.QtCore
import QThread
3 import serial.tools.list_ports
8 Class for a thread that handles incoming serial messages 11 super(MessageReception, self).
__init__()
21 print(
"Message Reception thread started")
24 message = self.mainWindow.comm.read(self.mainWindow.messageSize)
25 if len(message) == self.mainWindow.messageSize:
26 print(
"message received")
27 unpacked_msg = self.mainWindow.s.unpack(message)
28 print(str(self.
counter) +
": ", end=
'')
37 Method to stop and close the thread 40 print(
"Stopping Message Reception thread")
44 Method to set the current position of all motors based on the information in the received message 45 :param msg: received message 47 for i
in range(self.mainWindow.numberOfMotors):
48 self.mainWindow.dictMot[
"motor" + str(i+1)].setCurrentPosition(msg[i+1])
50 for i
in range(self.mainWindow.numberOfMotors):
51 self.mainWindow.dictMot[
"motor" + str(i + 1)].setGoalPosition(msg[i + 1])
52 self.mainWindow.updateSliderPositions()
57 Update drawer state (open/closed) 58 :param msg: received message 60 for i
in range(len(self.mainWindow.drawersList)):
61 self.mainWindow.drawersList[i].setState(msg[i+8])
66 Class for a thread that handles outgoing serial messages 69 super(MessageTransmission, self).
__init__()
79 print(
"Message Transmission thread started")
82 if len(self.mainWindow.msgDeque) > 0:
83 self.mainWindow.msgMu.lock()
84 self.mainWindow.comm.write(self.mainWindow.msgDeque.popleft())
85 self.mainWindow.msgMu.unlock()
89 Method to stop and close the thread 92 print(
"Stopping Message Transmission thread")
97 Initialize serial communication with a specified port 98 :param port: serial port name 99 :return: serial object and bool indicating if connection was successful 102 ser = serial.Serial(port, 9600, timeout=0.1)
103 print(
"Connected to %s" % port)
105 except serial.serialutil.SerialException:
106 print(
"Failed to connect to %s." % port)
109 return ser, connected
114 scan available serial ports and return the list 115 :return: list of available serial ports 117 ports = serial.tools.list_ports.comports()
118 ports_list = [
'Select a communication port']
119 ports_list.extend(ports)
def setMotorCurrentPosition(self, msg)
def setDrawerState(self, msg)
def __init__(self, mainWindow)
def __init__(self, mainWindow)
def initSerialConnection(port)