串口叫做串行接口,也称串行通信接口,按电气标准及协议来分包括RS-232-C、RS-422、RS485、USB等。 RS-232-C、RS-422与RS-485标准只对接口的电气特性做出规定,不涉及接插件、电缆或协议。USB是近几年发展起来的新型接口标准,主要应用于高速数据传输领域。

工作中,经常要用到串口来做数据的读写,今天我们来自己做一个串口工具,这一节,我们先把基础的框架先搭建好。

一.环境配置

1.这边我就直接使用的 Qt Designer 来设计串口界面的布局。

(1)先来制作我们串口的端口选择,以及波特率,串口的开关等:

这边用到了groupBox、label、comboBox、pushButton几个控件。

(2)接下来制作我们的串口高级设置部分:

(3)然后就是串口数据发送部分:

这边做了两种,ASCII、HEX类型。

(4)包括数据发送、接收文本框:

(6)工具栏制作:

包括了,文件、设置、帮助、退出四个功能。

(7)最终布局如下:

2.生成对应的 .py 文件

附源码:

# -*- 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, QtWidgetsclass 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.translateMainWindow.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.主文件代码如下:

import osimport sysfrom PyQt5.QtWidgets import QApplication, QMainWindowimport timefrom PyQt5.QtCore import Qtimport serialimport serial.tools.list_ports as serialsfrom PyQt5.QtGui import QIconimport Serial_if __name__ == '__main__':app = QApplication(sys.argv)MainWindow = QMainWindow()ui = Serial_.Ui_MainWindow()ui.setupUi(MainWindow)MainWindow.show()##显示sys.exit(app.exec_())##退出界面

二.总结

这一篇先说基础框架的制作,下一期来说说如何功能的实现。

@Neng