最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于PyQt6編寫一個串口調試助手

 更新時間:2024年12月27日 11:18:27   作者:小灰灰搞電子  
這篇文章主要為大家詳細介紹了如何基于PyQt6編寫一個串口調試助手,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下

一、界面預覽

二、實現(xiàn)源碼

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets
from PyQt6 import QtSerialPort
import PyQt6
from PyQt6.QtCore import QIODevice,QIODeviceBase
from PyQt6.QtCore import QDate, QTime, QDateTime, Qt

class Ui_Form(object):

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(791, 627)
        self.form = Form

        self.gridLayout_3 = QtWidgets.QGridLayout(Form)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout()
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.groupBox_2 = QtWidgets.QGroupBox(Form)
        self.groupBox_2.setObjectName("groupBox_2")
        self.gridLayout_4 = QtWidgets.QGridLayout(self.groupBox_2)
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.textBrowser = QtWidgets.QTextBrowser(self.groupBox_2)
        self.textBrowser.setObjectName("textBrowser")
        self.gridLayout_4.addWidget(self.textBrowser, 0, 0, 1, 1)
        self.horizontalLayout_4.addWidget(self.groupBox_2)
        self.groupBox = QtWidgets.QGroupBox(Form)
        self.groupBox.setObjectName("groupBox")
        self.gridLayout = QtWidgets.QGridLayout(self.groupBox)
        self.gridLayout.setObjectName("gridLayout")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_2 = QtWidgets.QLabel(self.groupBox)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_2.addWidget(self.label_2)
        self.comboBoxBaudrate = QtWidgets.QComboBox(self.groupBox)
        self.comboBoxBaudrate.setObjectName("comboBoxBaudrate")
        self.horizontalLayout_2.addWidget(self.comboBoxBaudrate)
        self.horizontalLayout_2.setStretch(0, 1)
        self.horizontalLayout_2.setStretch(1, 3)
        self.gridLayout.addLayout(self.horizontalLayout_2, 1, 0, 1, 1)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.comboBoxSerialPort = QtWidgets.QComboBox(self.groupBox)
        self.comboBoxSerialPort.setObjectName("comboBoxSerialPort")
        self.horizontalLayout.addWidget(self.comboBoxSerialPort)
        self.horizontalLayout.setStretch(0, 1)
        self.horizontalLayout.setStretch(1, 3)
        self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label_3 = QtWidgets.QLabel(self.groupBox)
        self.label_3.setObjectName("label_3")
        self.horizontalLayout_3.addWidget(self.label_3)
        self.btnOpenClose = QtWidgets.QPushButton(self.groupBox)
        self.btnOpenClose.setObjectName("btnOpenClose")
        self.horizontalLayout_3.addWidget(self.btnOpenClose)
        self.horizontalLayout_3.setStretch(0, 1)
        self.horizontalLayout_3.setStretch(1, 3)
        self.gridLayout.addLayout(self.horizontalLayout_3, 2, 0, 1, 1)
        spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
        self.gridLayout.addItem(spacerItem, 3, 0, 1, 1)
        self.horizontalLayout_4.addWidget(self.groupBox)
        self.horizontalLayout_4.setStretch(0, 5)
        self.horizontalLayout_4.setStretch(1, 3)
        self.verticalLayout_3.addLayout(self.horizontalLayout_4)
        self.groupBox_3 = QtWidgets.QGroupBox(Form)
        self.groupBox_3.setObjectName("groupBox_3")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.groupBox_3)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.textEdit = QtWidgets.QTextEdit(self.groupBox_3)
        self.textEdit.setObjectName("textEdit")
        self.horizontalLayout_5.addWidget(self.textEdit)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.btnSend = QtWidgets.QPushButton(self.groupBox_3)
        self.btnSend.setMinimumSize(QtCore.QSize(0, 50))
        self.btnSend.setObjectName("btnSend")
        self.verticalLayout.addWidget(self.btnSend)
        self.btnClear = QtWidgets.QPushButton(self.groupBox_3)
        self.btnClear.setMinimumSize(QtCore.QSize(0, 50))
        self.btnClear.setObjectName("btnClear")
        self.verticalLayout.addWidget(self.btnClear)
        self.horizontalLayout_5.addLayout(self.verticalLayout)
        self.horizontalLayout_5.setStretch(0, 5)
        self.horizontalLayout_5.setStretch(1, 1)
        self.gridLayout_2.addLayout(self.horizontalLayout_5, 0, 0, 1, 1)
        self.verticalLayout_3.addWidget(self.groupBox_3)
        self.verticalLayout_3.setStretch(0, 4)
        self.verticalLayout_3.setStretch(1, 2)
        self.gridLayout_3.addLayout(self.verticalLayout_3, 0, 0, 1, 1)

        self.textBrowser.setLineWrapMode(QtWidgets.QTextBrowser.LineWrapMode.WidgetWidth)
        self.paraInit()

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
    def paraInit(self):
        availablePorts = QtSerialPort.QSerialPortInfo.availablePorts()
        self.comboBoxSerialPort.clear()
        for port in availablePorts:
            self.comboBoxSerialPort.addItem(port.portName())

        # 初始化波特率
        baudrates = QtSerialPort.QSerialPortInfo.standardBaudRates()
        self.comboBoxBaudrate.clear()
        for i in baudrates:
            self.comboBoxBaudrate.addItem(str(i))

        self.serial = QtSerialPort.QSerialPort()
        #連接信號與槽
        self.btnOpenClose.clicked.connect(self.btnOpenCloseClick)
        self.btnSend.clicked.connect(self.btnSendClick)
        self.btnClear.clicked.connect(self.btnClearClick)
        self.serial.readyRead.connect(self.serialReadyRead)
    def btnOpenCloseClick(self):
        if(self.serial.isOpen()):
            print("關閉串口")
            self.btnOpenClose.setText("打開串口")
            self.serial.close()
            self.comboBoxBaudrate.setEnabled(True)
            self.comboBoxSerialPort.setEnabled(True)
        else:
            print("打開串口")
            self.serial.setPortName(self.comboBoxSerialPort.currentText())
            self.serial.setBaudRate(int(self.comboBoxBaudrate.currentText()))
            self.serial.setDataBits(QtSerialPort.QSerialPort.DataBits.Data8)
            self.serial.setParity(QtSerialPort.QSerialPort.Parity.NoParity)
            self.serial.setStopBits(QtSerialPort.QSerialPort.StopBits.OneStop)
            self.serial.setFlowControl(QtSerialPort.QSerialPort.FlowControl.NoFlowControl)
            print(self.serial.portName())
            print(self.serial.baudRate())
            if(self.serial.open(QIODeviceBase.OpenModeFlag.ReadWrite)):
                self.btnOpenClose.setText("關閉串口")
                self.comboBoxBaudrate.setEnabled(False)
                self.comboBoxSerialPort.setEnabled(False)
            else:
                QtWidgets.QMessageBox.critical(self.form,"錯誤","打開串口失敗:"+self.serial.errorString())




    def btnSendClick(self):
        text = self.textEdit.toPlainText()
        textByte = text.encode("UTF-8")
        self.serial.write(textByte)
    def btnClearClick(self):
        self.textEdit.clear()
    def serialReadyRead(self):
        buffer = self.serial.read(1024)
        buffer = buffer.decode("UTF-8")
        time = QDateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss  ")
        # self.textBrowser.append(time+buffer)

        self.textBrowser.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
        tc = self.textBrowser.textCursor()
        tc.movePosition(QtGui.QTextCursor.MoveOperation.End)
        tc.insertText(time+buffer)


    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.groupBox_2.setTitle(_translate("Form", "GroupBox"))
        self.groupBox.setTitle(_translate("Form", "GroupBox"))
        self.label_2.setText(_translate("Form", "波特率  :"))
        self.label.setText(_translate("Form", "串口選擇:"))
        self.label_3.setText(_translate("Form", "串口操作:"))
        self.btnOpenClose.setText(_translate("Form", "打開串口"))
        self.groupBox_3.setTitle(_translate("Form", "GroupBox"))
        self.btnSend.setText(_translate("Form", "發(fā)送"))
        self.btnClear.setText(_translate("Form", "清空"))


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = QtWidgets.QWidget()

    ui = Ui_Form()  # 這個是類名,名字根據(jù)自定義的情況變化
    ui.setupUi(mainWindow)
    mainWindow.setWindowTitle("串口調試助手")
    mainWindow.show()
    sys.exit(app.exec())

三、需要的軟件包

到此這篇關于基于PyQt6編寫一個串口調試助手的文章就介紹到這了,更多相關PyQt6串口調試內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 教你利用python的matplotlib(pyplot)繪制折線圖和柱狀圖

    教你利用python的matplotlib(pyplot)繪制折線圖和柱狀圖

    Python繪圖需要下載安裝matplotlib模塊,它是一個數(shù)學繪圖庫,我們將使用它來制作簡單的圖表,如折線圖和散點圖,下面這篇文章主要給大家介紹了關于利用python的matplotlib(pyplot)繪制折線圖和柱狀圖的相關資料,需要的朋友可以參考下
    2022-05-05
  • python3 小數(shù)位的四舍五入(用兩種方法解決round 遇5不進)

    python3 小數(shù)位的四舍五入(用兩種方法解決round 遇5不進)

    這篇文章主要介紹了python3 小數(shù)位的四舍五入(用兩種方法解決round 遇5不進),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • Python pip安裝第三方庫的攻略分享

    Python pip安裝第三方庫的攻略分享

    pip 就是 Python 標準庫(The Python Standard Library)中的一個包,只是這個包比較特殊,用它可以來管理 Python 標準庫(The Python Standard Library)中其他的包。本文為大家介紹了pip安裝第三方庫的方法,需要的可以參考一下
    2022-11-11
  • 詳解Python列表賦值復制深拷貝及5種淺拷貝

    詳解Python列表賦值復制深拷貝及5種淺拷貝

    這篇文章主要介紹了Python列表賦值,復制,深拷貝以及5種淺拷貝詳解,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • 使用Python生成新春煙花效果的方法

    使用Python生成新春煙花效果的方法

    新春佳節(jié)到來之際,煙花的璀璨綻放象征著喜慶和熱鬧,如果能通過代碼在屏幕上生成煙花效果,既能增加節(jié)日的趣味,也能加深對編程的理解,本篇博客將詳細介紹如何使用Python生成新春煙花效果,包括實現(xiàn)原理、代碼解析以及常見問題的解決方案,需要的朋友可以參考下
    2025-01-01
  • Python使用PyAutoGUI實現(xiàn)桌面自動化的實戰(zhàn)指南

    Python使用PyAutoGUI實現(xiàn)桌面自動化的實戰(zhàn)指南

    在現(xiàn)代辦公和開發(fā)環(huán)境中,我們常常被大量重復性的桌面操作所困擾,本文主要和大家詳細介紹一下Python如何使用PyAutoGUI實現(xiàn)桌面自動化功能,感興趣的小伙伴可以了解下
    2026-01-01
  • python?http通信接口開發(fā)示例

    python?http通信接口開發(fā)示例

    本文主要介紹了python?http通信接口開發(fā)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • python實現(xiàn)簡單的飛機大戰(zhàn)

    python實現(xiàn)簡單的飛機大戰(zhàn)

    這篇文章主要為大家詳細介紹了python實現(xiàn)簡單的飛機大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Python匿名函數(shù)/排序函數(shù)/過濾函數(shù)/映射函數(shù)/遞歸/二分法

    Python匿名函數(shù)/排序函數(shù)/過濾函數(shù)/映射函數(shù)/遞歸/二分法

    這篇文章主要介紹了Python匿名函數(shù)/排序函數(shù)/過濾函數(shù)/映射函數(shù)/遞歸/二分法 ,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • Python中win32包的安裝及常見用途介紹

    Python中win32包的安裝及常見用途介紹

    在Windows環(huán)境下,Python Win32模塊通常隨Python安裝包一起安裝,這篇文章主要介紹了Python中win32包的安裝及常見用途的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-06-06

最新評論

广河县| 华容县| 沙雅县| 阿瓦提县| 阜城县| 连平县| 白银市| 长岛县| 平度市| 开封市| 营山县| 唐山市| 苍山县| 阿瓦提县| 肥西县| 泽州县| 屯昌县| 汝城县| 阜平县| 吐鲁番市| 邵阳市| 大余县| 杭锦旗| 聂拉木县| 福建省| 札达县| 合阳县| 莫力| 乌鲁木齐市| 寿阳县| 漠河县| 杭州市| 佳木斯市| 萝北县| 青龙| 溧水县| 兴隆县| 昌吉市| 南涧| 冕宁县| 长海县|