-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCall_Ui_SerialPort.py
More file actions
160 lines (141 loc) · 5.87 KB
/
Copy pathCall_Ui_SerialPort.py
File metadata and controls
160 lines (141 loc) · 5.87 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
# 逻辑文件
import re
import sys
import binascii
import time
from PyQt5.QtCore import QTimer, QUrl
from PyQt5.QtWidgets import *
from PyQt5.QtSerialPort import QSerialPort, QSerialPortInfo
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWebEngineWidgets import *
from Embeded import Ui_Form
from PyQt5.QtCore import QDate
from PyQt5 import QtGui
class MyMainWindow(QMainWindow, Ui_Form):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
self.setupUi(self)
# 设置实例
self.CreateItems()
# 设置信号与槽
self.CreateSignalSlot()
self.photo.setPixmap(QtGui.QPixmap('./3.jpg').scaled(self.photo.width(), self.photo.height()) )
QApplication.setApplicationDisplayName("Embeded PC")
self.setWindowIcon(QtGui.QIcon('./emo1.ico'))
# 设置实例
def CreateItems(self):
# Qt 串口类
self.com = QSerialPort()
# 设置信号与槽
def CreateSignalSlot(self):
self.Com_Open_Button.clicked.connect(self.Com_Open_Button_clicked)
self.Com_Close_Button.clicked.connect(self.Com_Close_Button_clicked)
self.Send_Button.clicked.connect(self.SendButton_clicked)
self.Com_Refresh_Button.clicked.connect(self.Com_Refresh_Button_Clicked)
self.com.readyRead.connect(self.Com_Receive_Data) # 接收数据
self.hexSending_checkBox.stateChanged.connect(self.hexShowingClicked)
self.hexSending_checkBox.stateChanged.connect(self.hexSendingClicked)
# 串口发送数据
def Com_Send_Data(self):
txData = self.textEdit_Send.toPlainText()
print(type(txData))
if len(txData) == 0 :
return
if self.hexSending_checkBox.isChecked() == False:
self.com.write(txData.encode('UTF-8'))
print(txData.encode('UTF-8'))
else:
Data = txData.replace(' ', '')
# 如果16进制不是偶数个字符, 去掉最后一个, [ ]左闭右开
if len(Data)%2 == 1:
Data = Data[0:len(Data)-1]
# 如果遇到非16进制字符
if Data.isalnum() is False:
QMessageBox.critical(self, '错误', '包含非十六进制数')
try:
print(Data)
hexData = binascii.a2b_hex(Data)
except:
QMessageBox.critical(self, '错误', '转换编码错误')
return
# 发送16进制数据, 发送格式如 ‘31 32 33 41 42 43’, 代表'123ABC'
try:
print(hexData)
self.com.write(hexData)
except:
QMessageBox.critical(self, '异常', '十六进制发送错误')
return
# 串口接收数据
def Com_Receive_Data(self):
try:
rxData = bytes(self.com.readAll())
except:
QMessageBox.critical(self, '严重错误', '串口接收数据错误')
if self.hexShowing_checkBox.isChecked() == False :
try:
self.textEdit_Recive.insertPlainText(rxData.decode('UTF-8'))
except:
pass
else :
Data = binascii.b2a_hex(rxData).decode('ascii')
# re 正则表达式 (.{2}) 匹配两个字母
hexStr = ' 0x'.join(re.findall('(.{2})', Data))
# 补齐第一个 0x
hexStr = '0x' + hexStr
self.textEdit_Recive.insertPlainText(hexStr)
self.textEdit_Recive.insertPlainText(' ')
# 串口刷新
def Com_Refresh_Button_Clicked(self):
self.Com_Name_Combo.clear()
com = QSerialPort()
com_list = QSerialPortInfo.availablePorts()
for info in com_list:
com.setPort(info)
if com.open(QSerialPort.ReadWrite):
self.Com_Name_Combo.addItem(info.portName())
com.close()
# 16进制显示按下
def hexShowingClicked(self):
if self.hexShowing_checkBox.isChecked() == True:
# 接收区换行
self.textEdit_Recive.insertPlainText('\n')
# 16进制发送按下
def hexSendingClicked(self):
if self.hexSending_checkBox.isChecked() == True:
pass
# 发送按钮按下
def SendButton_clicked(self):
self.Com_Send_Data()
# 串口刷新按钮按下
def Com_Open_Button_clicked(self):
#### com Open Code here ####
comName = self.Com_Name_Combo.currentText()
comBaud = int(self.Com_Baud_Combo.currentText())
self.com.setPortName(comName)
try:
if self.com.open(QSerialPort.ReadWrite) == False:
QMessageBox.critical(self, '严重错误', '串口打开失败')
return
except:
QMessageBox.critical(self, '严重错误', '串口打开失败')
return
self.Com_Close_Button.setEnabled(True)
self.Com_Open_Button.setEnabled(False)
self.Com_Refresh_Button.setEnabled(False)
self.Com_Name_Combo.setEnabled(False)
self.Com_Baud_Combo.setEnabled(False)
self.Com_isOpenOrNot_Label.setText(' 已打开')
self.com.setBaudRate(comBaud)
def Com_Close_Button_clicked(self):
self.com.close()
self.Com_Close_Button.setEnabled(False)
self.Com_Open_Button.setEnabled(True)
self.Com_Refresh_Button.setEnabled(True)
self.Com_Name_Combo.setEnabled(True)
self.Com_Baud_Combo.setEnabled(True)
self.Com_isOpenOrNot_Label.setText(' 已关闭')
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MyMainWindow()
myWin.show()
sys.exit(app.exec_())