/
main.py
340 lines (245 loc) · 10.7 KB
/
main.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
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
'''
┌─┐ ┌─┐ + +
┌──┘ ┴───────┘ ┴──┐++
│ │
│ ─── │++ + + +
███████───███████ │+
│ │+
│ ─┴─ │
│ │
└───┐ ┌───┘
│ │
│ │ + +
│ │
│ └──────────────┐
│ │
│ ├─┐
│ ┌─┘
│ │
└─┐ ┐ ┌───────┬──┐ ┌──┘ + + + +
│ ─┤ ─┤ │ ─┤ ─┤
└──┴──┘ └──┴──┘ + + + +
神兽保佑
代码无BUG!
'''
import sys
from PyQt5.QtWidgets import QMainWindow,QWidget,QLayout,QApplication,QMessageBox,QAction
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import Qt,QTimer,QDateTime
from PyQt5.Qt import QThread,QMutex
import Sieral_UI
import config
import serial
import serial.tools.list_ports
class Worker(QThread):
def __init__(self):
super().__init__()
class Serial_MainWindow(QMainWindow,Sieral_UI.Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.rec_selction()
self.send_selction()
self.timer = QTimer()
self.timer1 = QTimer()
# self.time_now = time.strftime('%H:%M:%S',time.localtime())
self.timer1.timeout.connect(self.time_now)
self.timer.timeout.connect(self.data_send)
self.ser = serial.Serial()
self.port_check()
self.thread = Worker()
self.qmut = QMutex()
con = config.Myform()
self.comboBox_port.addItem(con.com)
self.comboBox_baud.setCurrentText(con.baud)
self.comboBox_databit.setCurrentText(con.databit)
self.comboBox_polarity.setCurrentText(con.polarity)
self.comboBox_stopbit.setCurrentText(con.stopbit)
self.comboBox_baud.currentIndexChanged.connect(self.combox_baud_cb)
self.comboBox_databit.currentIndexChanged.connect(self.combox_databit_cb)
self.comboBox_polarity.currentIndexChanged.connect(self.combox_polarity_cb)
self.comboBox_stopbit.currentIndexChanged.connect(self.combox_stopbit_cb)
self.btn_send.clicked.connect(self.btn_send_cb)
self.action_start.setEnabled(True)
self.action_Pause.setEnabled(False)
self.action_Stop.setEnabled(False)
self.action_start.triggered.connect(self.action_start_cb)
self.action_Pause.triggered.connect(self.action_Pause_cb)
self.action_Clear.triggered.connect(self.action_Clear_cb)
self.action_Stop.triggered.connect(self.action_Stop_cb)
self.Infor = QAction('串口信息',self)
self.menu_2.addAction(self.Infor)
self.Infor.triggered.connect(self.display_cb)
def time_now(self):
time = QDateTime.currentDateTime()
timeDisplay = time.toString('hh:mm:ss')
self.timer1.start(1000)
return timeDisplay
def display_cb(self):
self.port_information()
def port_information(self):
imf_s = self.comboBox_port.currentText()
if imf_s != '':
# self.textBrowser.append('当前串口为:' + imf_s)
# self.textBrowser.append('当前波特率为:' + self.comboBox_baud.currentText())
# self.textBrowser.append('当前数字位位:' + self.comboBox_databit.currentText())
# self.textBrowser.append('当前校验位为:' + self.comboBox_polarity.currentText())
# self.textBrowser.append('当前停止位为:' + self.comboBox_stopbit.currentText())
self.textBrowser.append('*****************当前串口状态********************')
self.textBrowser.append(str(self.ser.getSettingsDict()))
self.textBrowser.append('*****************当前串口状态********************')
else:
QMessageBox.information(self,'Nothing','无串口信息!')
def port_check(self):
self.com_dict = {}
port_list = list(serial.tools.list_ports.comports())
self.comboBox_port.clear()
for port in port_list:
self.com_dict['%s' % port[0]] = '%s' % port[1]
self.comboBox_port.addItem(port[0])
if len(self.com_dict) == 0:
QMessageBox.information(self,'提示','无可用串口')
def port_open(self):
self.ser.port = self.comboBox_port.currentText()
self.ser.baudrate = int(self.comboBox_baud.currentText())
self.ser.bytesize = int(self.comboBox_databit.currentText())
self.ser.stopbits = int(self.comboBox_stopbit.currentText())
if self.comboBox_polarity.currentText() == '无':
self.ser.parity = 'N'
elif self.comboBox_polarity.currentText() == '奇校验':
self.ser.parity = 'O'
elif self.comboBox_polarity.currentText() == '偶校验':
self.ser.parity = 'E'
try:
self.ser.open()
except:
QMessageBox.critical(self,'错误','该端口不能被打开!')
return None
if self.ser.isOpen():
self.action_start.setEnabled(False)
self.action_Stop.setEnabled(True)
self.action_Pause.setEnabled(True)
# serial.timer.start(2)
def port_close(self):
self.timer.stop()
try:
self.ser.close()
except:
pass
if self.ser.isOpen() == False:
self.action_start.setEnabled(True)
self.action_Stop.setEnabled(False)
self.action_Pause.setEnabled(False)
def send_time(self):
if self.checkBox_4.isChecked():
self.timer.start(self.spinBox.value())
else:
self.timer.stop()
self.data_send()
def data_send(self):
if self.ser.isOpen():
input_s = self.textEdit_get.toPlainText()
if input_s != '':
if self.radioButton_4.isChecked():
input_s = input_s.strip()
send_list = []
while input_s != '':
try:
num = int(input_s[0:2],16)
except ValueError:
QMessageBox.critical(self,'wrong data','请输入16进制数据,以空格分开!')
return None
input_s = input_s[2:].strip()
send_list.append(num)
input_s = bytes(send_list)
else:
input_s = (input_s +'\r\n').encode('utf-8')
self.ser.write(input_s)
if self.checkBox_2.isChecked():
if self.checkBox_3.isChecked():
self.textBrowser.append('Send: ' + self.textEdit_get.toPlainText() + ' ' +self.time_now())
else:
self.textBrowser.append('Send: ' + self.textEdit_get.toPlainText())
else:
pass
else:
pass
def data_rec(self):
try:
num = self.ser.inWaiting()
except:
self.ser.close()
return None
if num > 0:
data = self.ser.read(num)
num = len(data)
if self.radioButton_2.checkState():
out_s = ''
for i in range(0,num):
out_s = out_s + '{:02X}'.format(data[i])
if self.checkBox_3.isChecked():
self.textBrowser.append('Receive: ' + out_s + ' ' + self.time_now())
else:
self.textBrowser.append('Receive: ' + out_s)
else:
self.textBrowser.insertPlainText(data.decode('utf-8'))
textCursor = self.textBrowser.textCursor()
textCursor.movePosition(textCursor.End)
self.textBrowser.setTextCursor(textCursor)
else:
pass
def rec_selction(self):
self.radioButton_2.setChecked(True)
self.checkBox.setChecked(True)
self.checkBox_2.setChecked(True)
self.checkBox_3.setChecked(True)
def send_selction(self):
self.radioButton_4.setChecked(True)
self.checkBox_4.setChecked(False)
self.spinBox.setRange(1000,10000)
self.spinBox.setSingleStep(100)
# self.statusbar.showMessage('status:ok')
# self.comboBox_baud.currentIndexChanged.connect(self.comBox_baud_cb)
# self.btn_send.clicked.connect(self.btn_send_cb)
def action_start_cb(self):
self.port_open()
if self.ser.isOpen():
QMessageBox.information(self,'提示','串口已连接')
def action_Pause_cb(self):
self.port_close()
self.action_start.setEnabled(True)
self.action_Stop.setEnabled(True)
self.action_Pause.setEnabled(False)
QMessageBox.information(self,'提示','停止接收')
def action_Clear_cb(self):
QMessageBox.information(self,'提示','您点击了清除按钮')
self.textBrowser.clear()
# self.textEdit_get.clear()
def action_Stop_cb(self):
self.port_close()
if self.ser.isOpen() == False:
QMessageBox.information(self,'提示','串口关闭')
def combox_baud_cb(self):
data = self.comboBox_baud.currentText()
QMessageBox.information(self,'提示','当前选择波特率为:'+data,QMessageBox.Ok | QMessageBox.Cancel)
def combox_databit_cb(self):
data = self.comboBox_databit.currentText()
QMessageBox.information(self,'提示','当前选择数据位为:'+data,QMessageBox.Ok | QMessageBox.Cancel)
def combox_polarity_cb(self):
data = self.comboBox_polarity.currentText()
QMessageBox.information(self, '提示', '当前选择校验位为:' + data, QMessageBox.Ok | QMessageBox.Cancel)
def combox_stopbit_cb(self):
data = self.comboBox_stopbit.currentText()
QMessageBox.information(self, '提示', '当前选择停止位为:' + data, QMessageBox.Ok | QMessageBox.Cancel)
def btn_send_cb(self):
# str_send = self.textEdit_get.toPlainText()
# print(str_send)
# self.comboBox_port.addItem('COM4')
self.send_time()
# self.data_send()
# self.textBrowser.append(str_send)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Serial_MainWindow()
win.show()
sys.exit(app.exec_())