第一步:装pyqt5和pyserial库
pip install pyqt5 pyserial
第二步:完整代码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QComboBox, \
QGridLayout, QLineEdit, QTextEdit
from PyQt5.QtCore import QIODevice
from PyQt5.QtSerialPort import QSerialPort, QSerialPortInfo
class SerialDebugHelper(QMainWindow):
def __init__(self):
super(SerialDebugHelper, self).__init__()
self.serial = QSerialPort()
self.serial_port_info_list = QSerialPortInfo.availablePorts()
self.initUI()
def initUI(self):
self.setWindowTitle('串口调试助手')
self.setGeometry(300, 300, 600, 400)
layout = QGridLayout()
self.port_label = QLabel('串口:')
self.baudrate_label = QLabel('波特率:')
self.port_combo_box = QComboBox()
self.baudrate_combo_box = QComboBox()
self.refresh_button = QPushButton('刷新')
self.open_button = QPushButton('打开串口')
self.close_button = QPushButton('关闭串口')
self.send_button = QPushButton('发送')
self.clear_button = QPushButton('清除')
self.send_line_edit = QLineEdit()
self.received_text_edit = QTextEdit()
self.refresh_serial_ports()
self.baudrate_combo_box.addItems(['300', '1200', '2400', '4800', '9600', '19200', '38400', '57600', '115200'])
self.baudrate_combo_box.setCurrentText('9600') # Default to 9600
layout.addWidget(self.port_label, 0, 0)
layout.addWidget(self.port_combo_box, 0, 1)
layout.addWidget(self.baudrate_label, 1, 0)
layout.addWidget(self.baudrate_combo_box, 1, 1)
layout.addWidget(self.refresh_button, 0, 2)
layout.addWidget(self.open_button, 1, 2)
layout.addWidget(self.close_button, 1, 3)
layout.addWidget(self.received_text_edit, 2, 0, 1, 4)
layout.addWidget(self.send_line_edit, 3, 0, 1, 3)
layout.addWidget(self.send_button, 3, 3)
layout.addWidget(self.clear_button, 4, 3)
self.refresh_button.clicked.connect(self.refresh_serial_ports)
self.open_button.clicked.connect(self.open_serial_port)
self.close_button.clicked.connect(self.close_serial_port)
self.send_button.clicked.connect(self.send_data)
self.clear_button.clicked.connect(self.clear_received_data)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
def refresh_serial_ports(self):
self.port_combo_box.clear()
self.serial_port_info_list = QSerialPortInfo.availablePorts()
for port in self.serial_port_info_list:
self.port_combo_box.addItem(port.portName())
def send_data(self):
if self.serial.isOpen():
data = self.send_line_edit.text().encode()
self.serial.write(data)
self.send_line_edit.clear()
def clear_received_data(self):
self.received_text_edit.clear()
def on_serial_read(self):
bytes_to_read = self.serial.bytesAvailable()
text = self.serial.read(bytes_to_read).decode()
self.received_text_edit.insertPlainText(text)
def open_serial_port(self):
port_name = self.port_combo_box.currentText()
baud_rate = int(self.baudrate_combo_box.currentText())
self.serial.setPortName(port_name)
self.serial.setBaudRate(baud_rate)
self.serial.setDataBits(QSerialPort.Data8)
self.serial.setParity(QSerialPort.NoParity)
self.serial.setStopBits(QSerialPort.OneStop)
self.serial.setFlowControl(QSerialPort.NoFlowControl)
if self.serial.open(QIODevice.ReadWrite):
self.serial.readyRead.connect(self.on_serial_read)
self.open_button.setEnabled(False)
self.close_button.setEnabled(True)
def close_serial_port(self):
if self.serial.isOpen():
self.serial.close()
self.open_button.setEnabled(True)
self.close_button.setEnabled(False)
def closeEvent(self, event):
self.close_serial_port()
event.accept()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWin = SerialDebugHelper()
mainWin.show()
sys.exit(app.exec_())
第三步:运行结果
你可以用单片机或含串口的外设去测试,也可以下一个虚拟串口,附上网盘链接如下:
链接:https://pan.baidu.com/s/1V6w7vNGvMen6tspPJQcKiw
提取码:1bss
然后选择添加虚拟串口1、串口2,并且用一个现成的串口调试助手去测试;在你运行的串口助手选择串口号1,现成的串口调试助手选择串口2,并且二者波特率选择一致,就可以互相通讯了。