[python] Make a serial port tool (Part 1)!

      The serial port is called a serial interface, also known as a serial communication interface. According to electrical standards and protocols, it includes RS-232-C, RS-422, RS485, USB, etc. The RS-232-C, RS-422 and RS-485 standards only specify the electrical characteristics of the interface and do not involve connectors, cables or protocols. USB is a new interface standard developed in recent years and is mainly used in the field of high-speed data transmission.

      At work, we often use the serial port to read and write data. Today we are going to make a serial port tool ourselves. In this section, we first build the basic framework.

1. Environment configuration

1. Here I use Qt Designer directly to design the layout of the serial port interface.

(1) First, let’s make the port selection of our serial port, as well as the baud rate, serial port switch, etc.:

Several controls such as groupBox, label, comboBox, and pushButton are used here.

(2) Next, make our serial port advanced settings part:

(3) Then there is the serial port data sending part:

 There are two types here, ASCII and HEX types.

(4) Including data sending and receiving text boxes:

 (6) Toolbar production:

It includes four functions: file, settings, help, and exit.

(7) The final layout is as follows:

2. Generate the corresponding .py file

Source code attached:

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

# Form implementation generated from reading ui file 'Serial_.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# 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 PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(914, 672)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(20, 30, 321, 161))
        self.groupBox.setObjectName("groupBox")
        self.comboBox = QtWidgets.QComboBox(self.groupBox)
        self.comboBox.setGeometry(QtCore.QRect(130, 30, 161, 22))
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.label_23 = QtWidgets.QLabel(self.groupBox)
        self.label_23.setGeometry(QtCore.QRect(10, 30, 91, 21))
        self.label_23.setStyleSheet("background-color: rgb(85, 170, 255);")
        self.label_23.setAlignment(QtCore.Qt.AlignCenter)
        self.label_23.setObjectName("label_23")
        self.label_24 = QtWidgets.QLabel(self.groupBox)
        self.label_24.setGeometry(QtCore.QRect(10, 70, 91, 21))
        self.label_24.setStyleSheet("background-color: rgb(85, 170, 255);")
        self.label_24.setAlignment(QtCore.Qt.AlignCenter)
        self.label_24.setObjectName("label_24")
        self.comboBox_3 = QtWidgets.QComboBox(self.groupBox)
        self.comboBox_3.setGeometry(QtCore.QRect(130, 70, 161, 22))
        self.comboBox_3.setObjectName("comboBox_3")
        self.comboBox_3.addItem("")
        self.comboBox_3.addItem("")
        self.comboBox_3.addItem("")
        self.comboBox_3.addItem("")
        self.comboBox_3.addItem("")
        self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_2.setGeometry(QtCore.QRect(90, 120, 121, 31))
        self.pushButton_2.setObjectName("pushButton_2")
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setGeometry(QtCore.QRect(20, 200, 321, 291))
        self.groupBox_2.setObjectName("groupBox_2")
        self.comboBox_4 = QtWidgets.QComboBox(self.groupBox_2)
        self.comboBox_4.setGeometry(QtCore.QRect(230, 30, 69, 22))
        self.comboBox_4.setObjectName("comboBox_4")
        self.comboBox_4.addItem("")
        self.comboBox_4.addItem("")
        self.comboBox_4.addItem("")
        self.comboBox_4.addItem("")
        self.label_25 = QtWidgets.QLabel(self.groupBox_2)
        self.label_25.setGeometry(QtCore.QRect(10, 30, 91, 21))
        self.label_25.setStyleSheet("background-color: rgb(85, 170, 255);")
        self.label_25.setAlignment(QtCore.Qt.AlignCenter)
        self.label_25.setObjectName("label_25")
        self.label_26 = QtWidgets.QLabel(self.groupBox_2)
        self.label_26.setGeometry(QtCore.QRect(10, 70, 91, 21))
        self.label_26.setStyleSheet("background-color: rgb(85, 170, 255);")
        self.label_26.setAlignment(QtCore.Qt.AlignCenter)
        self.label_26.setObjectName("label_26")
        self.comboBox_5 = QtWidgets.QComboBox(self.groupBox_2)
        self.comboBox_5.setGeometry(QtCore.QRect(230, 70, 69, 22))
        self.comboBox_5.setObjectName("comboBox_5")
        self.comboBox_5.addItem("")
        self.comboBox_5.addItem("")
        self.label_27 = QtWidgets.QLabel(self.groupBox_2)
        self.label_27.setGeometry(QtCore.QRect(10, 110, 91, 21))
        self.label_27.setStyleSheet("background-color: rgb(85, 170, 255);")
        self.label_27.setAlignment(QtCore.Qt.AlignCenter)
        self.label_27.setObjectName("label_27")
        self.comboBox_6 = QtWidgets.QComboBox(self.groupBox_2)
        self.comboBox_6.setGeometry(QtCore.QRect(230, 110, 69, 22))
        self.comboBox_6.setObjectName("comboBox_6")
        self.comboBox_6.addItem("")
        self.comboBox_6.addItem("")
        self.comboBox_6.addItem("")
        self.comboBox_6.addItem("")
        self.comboBox_6.addItem("")
        self.label_28 = QtWidgets.QLabel(self.groupBox_2)
        self.label_28.setGeometry(QtCore.QRect(10, 150, 91, 21))
        self.label_28.setStyleSheet("background-color: rgb(85, 170, 255);")
        self.label_28.setAlignment(QtCore.Qt.AlignCenter)
        self.label_28.setObjectName("label_28")
        self.comboBox_7 = QtWidgets.QComboBox(self.groupBox_2)
        self.comboBox_7.setGeometry(QtCore.QRect(230, 150, 81, 22))
        self.comboBox_7.setObjectName("comboBox_7")
        self.comboBox_7.addItem("")
        self.comboBox_7.addItem("")
        self.comboBox_7.addItem("")
        self.comboBox_7.addItem("")
        self.label_29 = QtWidgets.QLabel(self.groupBox_2)
        self.label_29.setGeometry(QtCore.QRect(10, 190, 201, 21))
        self.label_29.setStyleSheet("background-color: rgb(85, 170, 255);")
        self.label_29.setAlignment(QtCore.Qt.AlignCenter)
        self.label_29.setObjectName("label_29")
        self.comboBox_8 = QtWidgets.QComboBox(self.groupBox_2)
        self.comboBox_8.setGeometry(QtCore.QRect(230, 190, 81, 22))
        self.comboBox_8.setObjectName("comboBox_8")
        self.comboBox_8.addItem("")
        self.pushButton = QtWidgets.QPushButton(self.groupBox_2)
        self.pushButton.setGeometry(QtCore.QRect(50, 230, 201, 31))
        self.pushButton.setObjectName("pushButton")
        self.label_30 = QtWidgets.QLabel(self.groupBox_2)
        self.label_30.setGeometry(QtCore.QRect(270, 230, 31, 31))
        self.label_30.setStyleSheet("border-image: url(:/BACK/107zi.jpg);")
        self.label_30.setText("")
        self.label_30.setAlignment(QtCore.Qt.AlignCenter)
        self.label_30.setObjectName("label_30")
        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(380, 30, 521, 341))
        self.textBrowser.setObjectName("textBrowser")
        self.groupBox_3 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_3.setGeometry(QtCore.QRect(20, 500, 321, 91))
        self.groupBox_3.setObjectName("groupBox_3")
        self.radioButton = QtWidgets.QRadioButton(self.groupBox_3)
        self.radioButton.setGeometry(QtCore.QRect(20, 20, 89, 16))
        self.radioButton.setObjectName("radioButton")
        self.radioButton_2 = QtWidgets.QRadioButton(self.groupBox_3)
        self.radioButton_2.setGeometry(QtCore.QRect(160, 20, 89, 16))
        self.radioButton_2.setObjectName("radioButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.groupBox_3)
        self.pushButton_3.setGeometry(QtCore.QRect(30, 50, 75, 23))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.groupBox_3)
        self.pushButton_4.setGeometry(QtCore.QRect(160, 50, 75, 23))
        self.pushButton_4.setObjectName("pushButton_4")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(380, 390, 521, 201))
        self.lineEdit.setObjectName("lineEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 914, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.toolBar = QtWidgets.QToolBar(MainWindow)
        self.toolBar.setObjectName("toolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
        self.action = QtWidgets.QAction(MainWindow)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/BACK/Desktop/img/file.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.action.setIcon(icon)
        self.action.setObjectName("action")
        self.action_2 = QtWidgets.QAction(MainWindow)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(":/BACK/Desktop/img/set.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.action_2.setIcon(icon1)
        self.action_2.setObjectName("action_2")
        self.action_3 = QtWidgets.QAction(MainWindow)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(":/BACK/Desktop/img/help.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.action_3.setIcon(icon2)
        self.action_3.setObjectName("action_3")
        self.action_4 = QtWidgets.QAction(MainWindow)
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap(":/BACK/Desktop/img/quit(1).jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.action_4.setIcon(icon3)
        self.action_4.setObjectName("action_4")
        self.toolBar.addAction(self.action)
        self.toolBar.addAction(self.action_2)
        self.toolBar.addAction(self.action_3)
        self.toolBar.addAction(self.action_4)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "串口工具v1.0"))
        self.groupBox.setTitle(_translate("MainWindow", "Basic Serial settings"))
        self.comboBox.setItemText(0, _translate("MainWindow", "Choose at session start"))
        self.label_23.setText(_translate("MainWindow", "Serial port:"))
        self.label_24.setText(_translate("MainWindow", "Speed (bps):"))
        self.comboBox_3.setItemText(0, _translate("MainWindow", "115200"))
        self.comboBox_3.setItemText(1, _translate("MainWindow", "1200"))
        self.comboBox_3.setItemText(2, _translate("MainWindow", "2400"))
        self.comboBox_3.setItemText(3, _translate("MainWindow", "9600"))
        self.comboBox_3.setItemText(4, _translate("MainWindow", "新建项目"))
        self.pushButton_2.setText(_translate("MainWindow", "Open Serial"))
        self.groupBox_2.setTitle(_translate("MainWindow", "Advanced Serial settings"))
        self.comboBox_4.setItemText(0, _translate("MainWindow", "8"))
        self.comboBox_4.setItemText(1, _translate("MainWindow", "7"))
        self.comboBox_4.setItemText(2, _translate("MainWindow", "6"))
        self.comboBox_4.setItemText(3, _translate("MainWindow", "5"))
        self.label_25.setText(_translate("MainWindow", "Data bits:"))
        self.label_26.setText(_translate("MainWindow", "Stop bits:"))
        self.comboBox_5.setItemText(0, _translate("MainWindow", "1"))
        self.comboBox_5.setItemText(1, _translate("MainWindow", "2"))
        self.label_27.setText(_translate("MainWindow", "Parity:"))
        self.comboBox_6.setItemText(0, _translate("MainWindow", "None"))
        self.comboBox_6.setItemText(1, _translate("MainWindow", "Odd"))
        self.comboBox_6.setItemText(2, _translate("MainWindow", "Even"))
        self.comboBox_6.setItemText(3, _translate("MainWindow", "Mark"))
        self.comboBox_6.setItemText(4, _translate("MainWindow", "Space"))
        self.label_28.setText(_translate("MainWindow", "Flow control:"))
        self.comboBox_7.setItemText(0, _translate("MainWindow", "Xon/Xoff"))
        self.comboBox_7.setItemText(1, _translate("MainWindow", "None"))
        self.comboBox_7.setItemText(2, _translate("MainWindow", "Dsr/Dtr"))
        self.comboBox_7.setItemText(3, _translate("MainWindow", "Rts/Cts"))
        self.label_29.setText(_translate("MainWindow", "execute macro as session start:"))
        self.comboBox_8.setItemText(0, _translate("MainWindow", "None"))
        self.pushButton.setText(_translate("MainWindow", "Reset defaults"))
        self.groupBox_3.setTitle(_translate("MainWindow", "Serial Send settings"))
        self.radioButton.setText(_translate("MainWindow", "ASCII"))
        self.radioButton_2.setText(_translate("MainWindow", "HEX"))
        self.pushButton_3.setText(_translate("MainWindow", "清除发送"))
        self.pushButton_4.setText(_translate("MainWindow", "发送数据"))
        self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
        self.action.setText(_translate("MainWindow", "文件"))
        self.action_2.setText(_translate("MainWindow", "设置"))
        self.action_3.setText(_translate("MainWindow", "帮助"))
        self.action_4.setText(_translate("MainWindow", "退出"))
import TUTU_rc

3. The main file code is as follows:

import os
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import time
from PyQt5.QtCore import Qt
import serial
import serial.tools.list_ports as serials
from PyQt5.QtGui import QIcon
import Serial_







if __name__ == '__main__':

    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Serial_.Ui_MainWindow()
    ui.setupUi(MainWindow)

    MainWindow.show()##显示

    sys.exit(app.exec_())##退出界面

2. Summary

This article will first talk about the production of the basic framework, and the next issue will talk about how to implement the functions.

Next article: [python] Make a serial port tool (Part 2)! _==PP's blog-CSDN blog

@Neng

Guess you like

Origin blog.csdn.net/pengneng123/article/details/131582373