PyQT——URAT串口调试助手(上位机界面)

页面实现效果:
main.py
python 复制代码
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
python 复制代码
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
python 复制代码
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_())
相关推荐
jaray10 分钟前
PyCharm 2024.3.2 Professional 如何更换 PyPI 镜像源
ide·python·pycharm·pypi 镜像源
Psycho_MrZhang13 分钟前
Neo4j Python SDK手册
开发语言·python·neo4j
web3.088899936 分钟前
1688图片搜索API,相似商品精准推荐
开发语言·python
少云清1 小时前
【性能测试】15_JMeter _JMeter插件安装使用
开发语言·python·jmeter
光羽隹衡1 小时前
机器学习——TF-IDF实战(红楼梦数据处理)
python·tf-idf
2401_894828122 小时前
从原理到实战:随机森林算法全解析(附 Python 完整代码)
开发语言·python·算法·随机森林
B站计算机毕业设计超人2 小时前
计算机毕业设计Python知识图谱中华古诗词可视化 古诗词情感分析 古诗词智能问答系统 AI大模型自动写诗 大数据毕业设计(源码+LW文档+PPT+讲解)
大数据·人工智能·hadoop·python·机器学习·知识图谱·课程设计
玄同7652 小时前
Python「焚诀」:吞噬所有语法糖的终极修炼手册
开发语言·数据库·人工智能·python·postgresql·自然语言处理·nlp
johnny2332 小时前
Python管理工具:包、版本、环境
python
羽翼.玫瑰2 小时前
关于重装Python失败(本质是未彻底卸载Python)的问题解决方案综述
开发语言·python