返回

巧用Python和PyQt5打造串口助手:零基础入门教程

后端

打造功能强大的串口助手:用 Python 和 PyQt5 轻松实现

什么是串口助手?

串口助手是一种用于与串口设备通信的强大软件工具。它允许您通过计算机与串口设备交换数据,从而轻松实现对设备的控制和数据传输。串口助手通常具备以下功能:

  • 串口检测: 识别并列出计算机可用的所有串口设备。
  • 串口参数配置: 设置波特率、数据位、停止位、校验位等串口通信参数。
  • 数据传输: 发送数据至串口设备并接收设备发回的数据。
  • 数据显示: 将发送和接收的数据以文本或其他格式清晰地显示在界面上。

Python 和 PyQt5 的介绍

Python 是一种广泛应用的编程语言,以其简单易学、用途广泛以及丰富的库著称。PyQt5 是一个 Python 图形用户界面(GUI)库,可帮助您轻松创建美观、交互式的高级图形应用程序。

使用 Python 和 PyQt5 构建串口助手

以下是如何使用 Python 和 PyQt5 构建一个串口助手:

步骤 1:安装必备库

首先,使用以下命令安装 Python 和 PyQt5 库:

pip install python
pip install PyQt5

步骤 2:创建 Python 脚本

接下来,创建一个 Python 脚本来实现串口助手功能。您可以参考以下代码:

import sys
import serial
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QTextEdit, QLabel, QComboBox, QSpinBox

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("串口助手")
        self.resize(800, 600)

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        self.main_layout = QVBoxLayout()
        self.central_widget.setLayout(self.main_layout)

        # 串口配置控件
        self.port_label = QLabel("串口号:")
        self.port_combo_box = QComboBox()
        self.port_combo_box.addItems(["COM1", "COM2", "COM3", ...])
        self.main_layout.addWidget(self.port_label)
        self.main_layout.addWidget(self.port_combo_box)

        self.baudrate_label = QLabel("波特率:")
        self.baudrate_spin_box = QSpinBox()
        self.baudrate_spin_box.setRange(9600, 115200)
        self.baudrate_spin_box.setValue(9600)
        self.main_layout.addWidget(self.baudrate_label)
        self.main_layout.addWidget(self.baudrate_spin_box)

        self.data_bits_label = QLabel("数据位:")
        self.data_bits_spin_box = QSpinBox()
        self.data_bits_spin_box.setRange(5, 8)
        self.data_bits_spin_box.setValue(8)
        self.main_layout.addWidget(self.data_bits_label)
        self.main_layout.addWidget(self.data_bits_spin_box)

        self.stop_bits_label = QLabel("停止位:")
        self.stop_bits_spin_box = QSpinBox()
        self.stop_bits_spin_box.setRange(1, 2)
        self.stop_bits_spin_box.setValue(1)
        self.main_layout.addWidget(self.stop_bits_label)
        self.main_layout.addWidget(self.stop_bits_spin_box)

        self.parity_label = QLabel("校验位:")
        self.parity_combo_box = QComboBox()
        self.parity_combo_box.addItems(["无", "奇校验", "偶校验"])
        self.main_layout.addWidget(self.parity_label)
        self.main_layout.addWidget(self.parity_combo_box)

        # 数据传输控件
        self.send_data_button = QPushButton("发送数据")
        self.send_data_button.clicked.connect(self.send_data)
        self.main_layout.addWidget(self.send_data_button)

        self.receive_data_text_edit = QTextEdit()
        self.receive_data_text_edit.setReadOnly(True)
        self.main_layout.addWidget(self.receive_data_text_edit)

        # 初始化串口对象
        self.serial_port = None

    def send_data(self):
        data = self.send_data_text_edit.toPlainText()
        self.serial_port.write(data.encode("utf-8"))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

步骤 3:运行 Python 脚本

使用以下命令运行 Python 脚本:

python serial_port_assistant.py

步骤 4:使用串口助手

  1. 选择要连接的串口号。
  2. 设置波特率、数据位、停止位、校验位等串口通信参数。
  3. 在发送数据文本框中输入要发送的数据。
  4. 单击“发送数据”按钮发送数据。
  5. 在接收数据文本框中查看接收到的数据。

常见问题解答

1. 如何选择正确的串口号?

在串口助手的主界面上,您可以看到一个下拉菜单,其中列出了计算机上可用的串口号。选择与您要连接的串口设备对应的串口号。

2. 如何设置正确的波特率?

波特率是指数据在串口上传输的速度。大多数串口设备都支持 9600 波特率,因此这是大多数应用的良好默认值。

3. 什么是数据位、停止位和校验位?

数据位表示每个字符中发送的数据位数。停止位表示字符之间的停止位数。校验位用于检测数据传输过程中的错误。

4. 如何发送数据?

在发送数据文本框中输入要发送的数据,然后单击“发送数据”按钮。

5. 如何接收数据?

接收到的数据将显示在接收数据文本框中。您可以滚动查看以前接收到的数据。

结论

使用 Python 和 PyQt5 构建串口助手可以让您轻松与串口设备进行通信。借助本文中提供的步骤和代码,您可以创建自己的串口助手,满足您的特定需求。对于任何其他问题或需要,请随时参考本文或联系我。