-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.py
More file actions
345 lines (284 loc) · 13 KB
/
Copy pathmainwindow.py
File metadata and controls
345 lines (284 loc) · 13 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from PyQt5.QtWidgets import QWidget, QLabel, QVBoxLayout, QMessageBox
from PyQt5.QtCore import QTimer, Qt, QEvent
from PyQt5.QtSerialPort import QSerialPortInfo
import pyqtgraph
import numpy
from ui.mainwindow import Ui_MainWindow
from serialrtx import SerialRtx
class MainWindow(QWidget):
_PHASE_SCALE = 5.625
_serial_process_buf = bytearray()
temp = 0.0
bite = 0.0
recvFrameNum = 0
sendFrameNum = 0
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.frame_verticalLayout = QVBoxLayout()
self.ui.m_PlotFrame.setLayout(self.frame_verticalLayout)
self._initPlot()
self._initUI()
self._updateFrame()
self._serialRtx = SerialRtx()
self._serialRtx.bindRecvSignal(self._serialRecvHandle)
self.timer = QTimer(self)
self.timer.timeout.connect(self._timerHandle)
self.timer.start(200)
def _initUI(self):
self._enumSerialPort()
self.ui.m_BaudEdit.setText('9600')
self.ui.m_SerialPortCtrlBtn.clicked.connect(self._serialPortCtrl)
self.ui.m_ModulateFreqEdit.setText("20")
self.ui.m_CenterFreqEdit.setText("17500")
self.ui.m_PulseWidthEdit.setText("50")
self.ui.m_PulseReplyPeriodEdit.setText("100000")
self.ui.m_AttEdit.setText("10")
self.ui.m_GNEdit.setText("30")
self.ui.m_RadioSwitchCheckBox.setChecked(True)
self.ui.m_RadioSwitchCheckBox.setText('开')
self.ui.m_SysModeCombox.setCurrentIndex(0)
self.ui.m_CaliPeriodEdit.setText("5")
self.ui.m_ChAAmpEdit.setText("5")
self.ui.m_ChAPhaseEdit.setText("28.125")
self.ui.m_ChBAmpEdit.setText("5")
self.ui.m_ChBPhaseEdit.setText("28.125")
self.ui.m_ChCAmpEdit.setText("5")
self.ui.m_ChCPhaseEdit.setText("28.125")
self.ui.m_TrigDelayEdit.setText("0")
self.ui.m_CenterFreqEdit.editingFinished.connect(self._updateFrame)
self.ui.m_ModulateFreqEdit.editingFinished.connect(self._updateFrame)
self.ui.m_PulseWidthEdit.editingFinished.connect(self._updateFrame)
self.ui.m_PulseReplyPeriodEdit.editingFinished.connect(self._updateFrame)
self.ui.m_SysModeCombox.currentIndexChanged.connect(self._updateFrame)
self.ui.m_AttEdit.editingFinished.connect(self._updateFrame)
self.ui.m_GNEdit.editingFinished.connect(self._updateFrame)
self.ui.m_RadioSwitchCheckBox.clicked.connect(self._rfSwitchChanged)
self.ui.m_RadioSwitchCheckBox.stateChanged.connect(self._updateFrame)
self.ui.m_CaliPeriodEdit.editingFinished.connect(self._updateFrame)
self.ui.m_ChAAmpEdit.editingFinished.connect(self._updateFrame)
self.ui.m_ChAPhaseEdit.editingFinished.connect(self._updateFrame)
self.ui.m_ChBAmpEdit.editingFinished.connect(self._updateFrame)
self.ui.m_ChBPhaseEdit.editingFinished.connect(self._updateFrame)
self.ui.m_ChCAmpEdit.editingFinished.connect(self._updateFrame)
self.ui.m_ChCPhaseEdit.editingFinished.connect(self._updateFrame)
self.ui.m_TrigModeCombo.currentIndexChanged.connect(self._updateFrame)
self.ui.m_TrigDelayEdit.editingFinished.connect(self._updateFrame)
self.ui.m_SendCmdBtn.clicked.connect(self._sendCmd)
# self.ui.brand_label.setText('<b><u>QSweepTerminal</u></b>')
# self.ui.brand_label.setAlignment(Qt.AlignRight)
# self.ui.copyright_label.setText('<i>CopyRight © 2019-2029 v1.0.0</i>')
# self.ui.copyright_label.setAlignment(Qt.AlignRight)
# self.verticalLayout.addWidget(brand_label)
def _rfSwitchChanged(self):
if self.ui.m_RadioSwitchCheckBox.isChecked():
self.ui.m_RadioSwitchCheckBox.setText('开')
else:
self.ui.m_RadioSwitchCheckBox.setText('关')
def _initPlot(self):
self.plot_win = pyqtgraph.GraphicsWindow()
self.plot_temp = self.plot_win.addPlot()
self.plot_win.nextRow()
self.plot_bite = self.plot_win.addPlot()
# self.verticalLayout.addWidget(self.plot_win)
self.frame_verticalLayout.addWidget(self.plot_win)
self.plot_temp.vb.setMouseEnabled(False, False)
self.plot_temp.showGrid(True, True, 0.8)
self.plot_temp.setMenuEnabled(False)
# self.plot_temp.vb.disableAutoRange(pyqtgraph.graphicsItems.ViewBox.ViewBox.YAxis)
# self.plot_temp.vb.disableAutoRange(pyqtgraph.graphicsItems.ViewBox.ViewBox.XAxis)
self.plot_temp.setYRange(-45, 100)
self.plot_temp.setXRange(0, 5000)
self.plot_temp.getAxis('left').setLabel('温度', units='°C')
self.plot_bite.vb.setMouseEnabled(False, False)
self.plot_bite.showGrid(True, True, 0.8)
self.plot_bite.setMenuEnabled(False)
# self.plot_bite.vb.disableAutoRange(pyqtgraph.graphicsItems.ViewBox.ViewBox.YAxis)
# self.plot_bite.vb.disableAutoRange(pyqtgraph.graphicsItems.ViewBox.ViewBox.XAxis)
self.plot_bite.setYRange(0, 5)
self.plot_bite.setXRange(0, 5000)
self.plot_bite.getAxis('left').setLabel('检波电压', units='v')
self.plot_temp_curve = self.plot_temp.plot(pen='g')
self.plot_bite_curve = self.plot_bite.plot(pen='g')
self.plot_temp_data = numpy.empty(5000)
self.plot_bite_data = numpy.empty(5000)
self.plot_data_index = 0
def _updatePlot(self):
if self.plot_data_index == 5000:
self.plot_temp_data[:-1] = self.plot_temp_data[1:]
self.plot_bite_data[:-1] = self.plot_bite_data[1:]
self.plot_data_index = 4999
self.plot_temp_data[self.plot_data_index] = self.temp
self.plot_bite_data[self.plot_data_index] = self.bite
self.plot_data_index += 1
self.plot_temp_curve.setData(self.plot_temp_data[:self.plot_data_index])
self.plot_bite_curve.setData(self.plot_bite_data[:self.plot_data_index])
def _enumSerialPort(self):
self.ui.m_SerialPortCombo.clear()
port_list = QSerialPortInfo.availablePorts()
for port in port_list:
self.ui.m_SerialPortCombo.addItem(port.portName())
def _updateFrame(self):
# 跳频率
modulate_freq = self.ui.m_ModulateFreqEdit.text()
modulate_freq = int(modulate_freq)
# frame_cmd = "#KR:" + str(modulate_freq * 32768) + ";"
frame_cmd = "#KR:" + '{:0=6}'.format(modulate_freq * 32768) + ";"
# 中心频点
center_freq = self.ui.m_CenterFreqEdit.text()
center_freq = int(center_freq)
frame_cmd += "#FRQ:" + '{:0=5}'.format(center_freq) + ";"
# 脉冲宽度
pulse_width = self.ui.m_PulseWidthEdit.text()
pulse_width = int(pulse_width)
frame_cmd += "#PW:" + '{:0=7}'.format(pulse_width) + ";"
# 重复周期
pulse_reply_period = self.ui.m_PulseReplyPeriodEdit.text()
pulse_reply_period = int(pulse_reply_period)
frame_cmd += "#PRT:" + '{:0=7}'.format(pulse_reply_period) + ";"
# 衰减
att = self.ui.m_AttEdit.text()
att = int(att)
frame_cmd += "#ATT:" + '{:0=2}'.format(att) + ";"
# 增益
gn = self.ui.m_GNEdit.text()
gn = int(gn)
frame_cmd += "#GN:" + '{:0=2}'.format(gn) + ";"
# 射频开关
if self.ui.m_RadioSwitchCheckBox.isChecked():
frame_cmd += "#RF:1;"
else:
frame_cmd += "#RF:0;"
# 系统模式
sys_mode = self.ui.m_SysModeCombox.currentIndex() + 1
frame_cmd += "#MOD:" + str(sys_mode) + ";"
# 校准时长
cali_period = self.ui.m_CaliPeriodEdit.text()
cali_period = float(cali_period)
cali_period = int(cali_period * 10)
frame_cmd += "#CT:" + '{:0=6}'.format(cali_period) + ";"
# 接收通道A幅度衰减
cha_amp = self.ui.m_ChAAmpEdit.text()
cha_amp = int(cha_amp)
frame_cmd += "#AAA:" + '{:0=2}'.format(cha_amp) + ";"
# 接收通道A相位偏置
cha_phase = self.ui.m_ChAPhaseEdit.text()
cha_phase = float(cha_phase)
cha_phase = int(cha_phase / self._PHASE_SCALE)
frame_cmd += "#PSA:" + '{:0=2}'.format(cha_phase) + ";"
# 接收通道B幅度衰减
chb_amp = self.ui.m_ChBAmpEdit.text()
chb_amp = int(chb_amp)
frame_cmd += "#AAB:" + '{:0=2}'.format(chb_amp) + ";"
# 接收通道B相位偏置
chb_phase = self.ui.m_ChBPhaseEdit.text()
chb_phase = float(chb_phase)
chb_phase = int(chb_phase / self._PHASE_SCALE)
frame_cmd += "#PSB:" + '{:0=2}'.format(chb_phase) + ";"
# 接收通道C幅度衰减
chc_amp = self.ui.m_ChCAmpEdit.text()
chc_amp = int(chc_amp)
frame_cmd += "#AAC:" + '{:0=2}'.format(chc_amp) + ";"
# 接收通道C相位偏置
chc_phase = self.ui.m_ChCPhaseEdit.text()
chc_phase = float(chc_phase)
chc_phase = int(chc_phase / self._PHASE_SCALE)
frame_cmd += "#PSC:" + '{:0=2}'.format(chc_phase) + ";"
# 触发模式
trig_mode = self.ui.m_TrigModeCombo.currentIndex()
frame_cmd += "#CF:" + str(sys_mode) + ";"
# 延迟
trig_delay = self.ui.m_TrigDelayEdit.text()
trig_delay = int(trig_delay)
frame_cmd += "#YS:" + '{:0=2}'.format(trig_delay) + ";"
self.ui.m_FrameDataEdit.setText(frame_cmd)
def _sendCmd(self):
if not self._serialRtx.isOpened():
QMessageBox.information(self, '提示', '串口未打开!', QMessageBox.Yes)
return
self.sendFrameNum += 1
self.ui.m_sendFrameNumLcd.display(self.sendFrameNum)
frame = self.ui.m_FrameDataEdit.toPlainText()
frame_bytes = frame.encode()
self._serialRtx.send(frame_bytes, len(frame))
return
def _serialPortCtrl(self):
if not self._serialRtx.isOpened():
serial_port_name = self.ui.m_SerialPortCombo.currentText()
if serial_port_name == '':
QMessageBox.information(self, '提示', '无串口连接!', QMessageBox.Yes)
return
baud = self.ui.m_BaudEdit.text()
baud = int(baud)
if not self._serialRtx.openPort(serial_port_name, baud):
QMessageBox.warning(self, '警告', '打开串口失败!串口是否已被占用?', QMessageBox.Yes)
return
self.ui.m_SerialPortCtrlBtn.setText('关闭串口')
else:
self._serialRtx.closePort()
self.ui.m_SerialPortCtrlBtn.setText('打开串口')
def _serialRecvHandle(self):
data = self._serialRtx.recv()
if len(data) == 0:
return
# print(data.hex(' '))
self._serial_process_buf.extend(data)
self.recvDataProcess()
def recvDataProcess(self):
start_index = self._serial_process_buf.find(b'#KR:')
if start_index == -1:
self._serial_process_buf.clear()
return
if start_index != 0:
for i in range(start_index):
self._serial_process_buf.pop(0)
end_index = self._serial_process_buf.find(b'*\r\n')
if end_index == -1:
if len(self._serial_process_buf) > 200:
for i in range(100):
self._serial_process_buf.pop(0)
return self.recvDataProcess()
else:
return
mid_index = self._serial_process_buf.find(b'#YS:')
reply_frame = self._serial_process_buf[start_index:mid_index+7]
reply_frame_str = ""
for byte in reply_frame:
reply_frame_str += ('%c' % byte)
self.ui.m_RplyFrameEdit.setText(reply_frame_str)
sta_frame = self._serial_process_buf[mid_index+8:end_index]
self._frameParse(sta_frame)
for i in range(end_index + 1):
self._serial_process_buf.pop(0)
return self.recvDataProcess()
def _frameParse(self, frame):
self.recvFrameNum += 1
self.ui.m_recvFrameNumLcd.display(self.recvFrameNum)
field = frame.split(b';')
if len(field) < 3:
return
temp_field = field[0].split(b':')
temp_str = temp_field[1].decode('utf-8', 'ignore')
try:
self.temp = float(temp_str)
except ValueError:
pass
bite_field = field[1].split(b':')
bite_str = bite_field[1].decode('utf-8', 'ignore')
try:
self.bite = float(bite_str)
except ValueError:
pass
self._updatePlot()
def _timerHandle(self):
self.ui.m_sendBytesNumLcd.display(self._serialRtx.sendBytesNum())
self.ui.m_recvBytesNumLcd.display(self._serialRtx.recvBytesNum())
def closeEvent(self, event):
reply = QMessageBox.question(self, '提示', '是否退出应用程序?', QMessageBox.Yes | QMessageBox.No)
if reply == QMessageBox.Yes:
if self._serialRtx.isOpened():
self._serialRtx.closePort()
event.accept()
else:
event.ignore()