-
Notifications
You must be signed in to change notification settings - Fork 0
/
metroMini.py
191 lines (144 loc) · 5.95 KB
/
metroMini.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import glob
from PyQt5.QtSerialPort import QSerialPort
from PyQt5.QtCore import pyqtSignal, QIODevice, QObject, QMutex, QMutexLocker
# _UPDATE_INTERVAL = 3
class MetroMini(QObject):
"""CLASS: MetroMini
This class wraps the serial port used to communicate with the peripheral Metro Mini processor.
SIGNALS SLOTS
------------------------ -----------------
broadcast (str) (Simulator) begin
displayRXMessage (str) () readData
displayTXMessage (str) (str) writeData
newDataAvailable (float)
printStatus (str)
ready ()
"""
# TODO: Gracefully handle serial connection errors
newDataAvailable = pyqtSignal(float)
"""SIGNAL: newDataAvailable
Emitted when a new pressure value has been received from the Metro Mini
Broadcasts:
float - The new value
Connects to:
DisplayState.updateDisplay (MainWindow.psiDisplay.defaultState)
"""
printStatus = pyqtSignal(str)
"""SIGNAL: printStatus
Displays a temporary message on the simulator's status bar
Broadcasts:
str - The temporary message to display
Connects to:
QMainWindow.QStatusBar.showMessage (MainWindow.simulator)
"""
displayTXMessage = pyqtSignal(str)
"""SIGNAL: displayTXMessage
Displays a message sent over serial in the simulator window
Broadcasts:
str - The message to display
Connects to:
QLabel.showMessage (MainWindow.simulator.serialSentMsg)
"""
displayRXMessage = pyqtSignal(str)
"""SIGNAL: displayRXMessage
Displays a message received from serial in the simulator window
Broadcasts:
str - The message to display
Connects to:
QLabel.showMessage (MainWindow.simulator.serialRcvdMsg)
"""
broadcast = pyqtSignal(str)
"""SIGNAL: broadcast
This signal is called when a message needs to be written to serial. It exists to ensure that the write signal is emitted in the correct thread.
Broadcasts:
str - The message to be sent
Connects to:
QState.exited (MainWindow.psiDisplay.waitState), (MainWindow.psiDisplay.requestTimer) QTimer.timeout, writeData
"""
ready = pyqtSignal()
"""SIGNAL: ready
Announces that the Metro Mini is ready for data exchange
Broadcasts:
none
Connects to:
MainWindow.initializeSerialObjects
"""
close = pyqtSignal()
def begin(self):
"""SLOT: begin
Launches the serial read/write signal loop
Expects:
none
Connects to:
QThread.started
"""
path = glob.glob("/dev/tty.usbserial-*")
self.serialPort = None
try:
path = path[0]
except IndexError:
self.displayRXMessage.emit("No serial connected")
self.ready.emit()
else:
self.serialPort = QSerialPort(path)
self.serialPort.setBaudRate(9600)
self.buffer = bytearray()
self.serialPort.readyRead.connect(self.readData)
self.close.connect(self.serialPort.close)
self.serialPort.open(QIODevice.ReadWrite)
finally:
self.lock = QMutex()
self.broadcast.connect(self.writeData)
def connectSimulator(self, sim):
"""METHOD: connectSimulator
Creates a connection between this object and the simulator window, if the program is running in simulation `
Called by:
MainWindow.__init__
Arguments:
FHKSimulator: The simulator window
Returns:
none
"""
self.sim = sim
self.printStatus.connect(lambda msg: self.sim.statusBar().showMessage(msg, 5000))
self.displayTXMessage.connect(self.sim.serialSentMsg.setText)
self.displayRXMessage.connect(self.sim.serialRcvdMsg.setText)
def readData(self):
"""SLOT: readData
Reads data from the serial port when it's available
Expects:
none
Connects to:
QSerialPort.readyRead
Emits:
newDataAvailable
"""
with QMutexLocker(self.lock):
bytesIn = self.serialPort.readAll()
for b in bytesIn:
if b == b'\n': # This translates to the \n character which means the message is complete
msg = self.buffer.decode().strip()
self.displayRXMessage.emit(msg)
if msg == "ready":
self.ready.emit()
else:
self.newDataAvailable.emit(float(msg))
self.printStatus.emit("Serial read complete")
self.buffer = bytearray() # Clear buffer
elif b != b'\r': # Ignore '\r' character too
self.buffer = self.buffer + b
def writeData(self, msg):
"""SLOT: writeData
Writes data to the serial port
Expects:
str - The message to be sent
Connects to:
broadcast
"""
with QMutexLocker(self.lock):
if self.serialPort is not None:
self.serialPort.write(msg.encode())
self.serialPort.waitForBytesWritten()
self.printStatus.emit("Serial write complete")
if msg != "request;": # This message prevents all others from being visible, and we know it's being sent if values are coming back
self.displayTXMessage.emit(msg)