页面实现效果:
main.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
from ui.Ui_main_window import Ui_MainWindow
from views.serial_assist_widget import *
class MainWidget(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('自定义窗口标题')
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# 初始化UI界面
self.init_ui()
def init_ui(self):
self.ui.tabWidget.addTab(SerialAssistWidget(self),"串口助手工具")
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = MainWidget()
widget.show()
sys.exit(app.exec_())
serial_assist_widget.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
import threading
sys.path.append('../')
from ui.Ui_serial_assist_widget import Ui_SerialAssistWidget
from views.serial_setting_dialog import SettingDialog
from drivers.driver_serial import *
class SerialAssistWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_SerialAssistWidget()
self.ui.setupUi(self)
self.sp = None
# 设备列表默认为空
self.device_port = []
# 打开页面要扫描设备并显示
self.refresh_device_slot()
# 点击事件
self.init_ui()
def init_ui(self):
# 波特率设置按钮信号与槽函数
self.ui.btn_setting.clicked.connect(self.show_dialog_slot)
# 设备刷新按钮信号与槽函数
self.ui.btn_refersh.clicked.connect(self.refresh_device_slot)
# 链接设备按钮信号与槽函数
self.ui.btn_connect.clicked.connect(self.btn_connect_slot)
# 发送数据按钮信号与槽函数
self.ui.btn_send.clicked.connect(self.btn_send_slot)
# 清空数据发送按钮信号与槽函数
self.ui.pushButton_4.clicked.connect(self.clear_send_text)
# 清空数据接受按钮信号与槽函数
self.ui.pushButton_3.clicked.connect(self.clear_recv_text)
def clear_send_text(self):
self.ui.edit_send.clear()
def clear_recv_text(self):
self.ui.edit_recv.clear()
def btn_send_slot(self):
if not self.sp:
QMessageBox.warning(self,"警告","请先链接设备")
return
send_text = self.ui.edit_send.toPlainText()
if send_text == '':
QMessageBox.warning(self,"警告","请输入要发送的内容")
return
self.sp.write(send_text.encode("utf-8"))
# 在末尾必须加上换行符,否则不能立即收到
self.sp.write(b'\n')
def btn_connect_slot(self):
# 如果设备未选择,则弹出警告提示
if not self.device_port:
QMessageBox.warning(self,"警告","请选择设备")
return
# 如果self.sp为真说明已经链接了,再次点击要断开链接
if self.sp:
self.sp.close()
self.ui.btn_connect.setText("点击链接")
self.ui.label_status.setPixmap(QPixmap(':/icon/disc'))
self.sp=None
return
# 收集波特率和设备信息
baud = self.ui.cb_baud.currentText()
device_index = self.ui.cb_device.currentIndex()
print(device_index,type(device_index))
print(self.device_port)
device_name = self.device_port[device_index][0]
# 链接设备,成功则设置按钮状态与图标,失败则弹出警告
self.sp = SerialDevice(device_name,int(baud))
res, msg = self.sp.open()
if not res:
QMessageBox.warning(self,"警告",msg)
return
# 设置按钮状态与图标
self.ui.btn_connect.setText("断开链接(已链接)")
self.ui.label_status.setPixmap(QPixmap(':/icon/connect'))
# 链接成功后就应开启子线程接受数据
t = threading.Thread(target=self.recv_data,daemon=True)
t.start()
def recv_data(self):
while True:
data = self.sp.readline()
# 去掉末尾的换行符
data = data[:-1].decode('utf-8')
self.ui.edit_recv.append(data)
def refresh_device_slot(self):
self.device_port = scan_serial_ports()
print(self.device_port)
if len(self.device_port)>0:
for device,description in self.device_port:
self.ui.cb_device.addItem(description)
# 设置默认选择第一个
self.ui.cb_device.setCurrentIndex(0)
def show_dialog_slot(self):
self.dialog = SettingDialog(self)
self.dialog.signal.connect(self.dialog_slot)
self.dialog.exec_()
def dialog_slot(self,bt,data_bite):
# 将信号中的bt同步到串口助手页面
self.ui.cb_baud.setCurrentText(bt)
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = SerialAssistWidget()
widget.show()
sys.exit(app.exec_())
serial_setting_dialog.py
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
sys.path.append("../")
from ui.Ui_serial_setting_dialog import Ui_Dialog
class SettingDialog(QDialog):
setting_signal = pyqtSignal(str,str)
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle('自定义窗口标题')
# 初始化UI界面
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.init_ui()
def btn_ok_slot(self):
bt = self.ui.cb_bt.currentText()
data = self.ui.cb_data.currentText()
self.setting_signal.emit(bt,data)
self.close()
def init_ui(self):
self.ui.btnOk.clicked.connect(self.btn_ok_slot)
self.ui.btnCancel.clicked.connect(self.close)
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = SettingDialog()
widget.show()
sys.exit(app.exec_())