Python3+PyQT5+Pyserial 实现串口工具方法
时间: 2024-01-12 21:05:35 浏览: 123
使用Python3+PyQT5+Pyserial 实现简单的串口工具方法
以下是使用Python3、PyQT5和Pyserial实现串口工具的方法:
1. 安装Pyserial和PyQT5库
在终端或命令行中输入以下命令来安装Pyserial和PyQT5:
```
pip install pyserial
pip install pyqt5
```
2. 创建GUI界面
使用PyQT5创建一个简单的GUI界面。在这个界面中,你需要添加一个下拉菜单来选择串口、一个文本框来显示串口数据和一些按钮来打开/关闭串口和发送数据。
下面是一个简单的PyQT5串口工具界面:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QComboBox, QTextEdit, QPushButton, QHBoxLayout, QVBoxLayout, QWidget
class SerialTool(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 设置窗口大小
self.setGeometry(100, 100, 400, 300)
# 设置窗口标题
self.setWindowTitle('Serial Tool')
# 添加下拉菜单
self.portComboBox = QComboBox(self)
self.portComboBox.move(10, 10)
self.portComboBox.resize(180, 30)
# 添加文本框
self.textEdit = QTextEdit(self)
self.textEdit.move(10, 50)
self.textEdit.resize(380, 180)
# 添加按钮
self.openButton = QPushButton('Open', self)
self.openButton.move(10, 240)
self.openButton.resize(80, 30)
self.closeButton = QPushButton('Close', self)
self.closeButton.move(100, 240)
self.closeButton.resize(80, 30)
self.sendButton = QPushButton('Send', self)
self.sendButton.move(190, 240)
self.sendButton.resize(80, 30)
self.clearButton = QPushButton('Clear', self)
self.clearButton.move(280, 240)
self.clearButton.resize(80, 30)
# 添加布局管理器
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(self.openButton)
hbox.addWidget(self.closeButton)
hbox.addWidget(self.sendButton)
hbox.addWidget(self.clearButton)
vbox = QVBoxLayout()
vbox.addWidget(self.portComboBox)
vbox.addLayout(hbox)
widget = QWidget()
widget.setLayout(vbox)
self.setCentralWidget(widget)
```
3. 实现串口操作
使用Pyserial库来创建串口连接。在打开串口之前,你需要获取可用的串口列表并将它们添加到下拉菜单中。在发送数据之前,你需要从文本框中获取数据。
下面是一个简单的Pyserial串口操作代码:
```python
import serial
class SerialTool(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.initSerial()
def initSerial(self):
# 获取可用串口列表
ports = serial.tools.list_ports.comports()
# 将可用串口添加到下拉菜单中
for port in ports:
self.portComboBox.addItem(port.device)
# 创建串口连接
self.ser = serial.Serial()
def openSerial(self):
# 设置串口参数
port = self.portComboBox.currentText()
baudrate = 9600
bytesize = serial.EIGHTBITS
parity = serial.PARITY_NONE
stopbits = serial.STOPBITS_ONE
self.ser.port = port
self.ser.baudrate = baudrate
self.ser.bytesize = bytesize
self.ser.parity = parity
self.ser.stopbits = stopbits
# 打开串口连接
try:
self.ser.open()
self.ser.flushInput()
self.ser.flushOutput()
self.textEdit.append('Serial port is open.')
except Exception as e:
self.textEdit.append(str(e))
def closeSerial(self):
# 关闭串口连接
try:
self.ser.close()
self.textEdit.append('Serial port is closed.')
except Exception as e:
self.textEdit.append(str(e))
def sendSerial(self):
# 发送数据
data = self.textEdit.toPlainText()
try:
self.ser.write(data.encode())
self.textEdit.clear()
self.textEdit.append('Data is sent: ' + data)
except Exception as e:
self.textEdit.append(str(e))
```
4. 运行程序
将GUI界面和串口操作代码组合起来,并在主函数中运行程序。
下面是一个完整的PyQT5串口工具代码:
```python
import sys
import serial
from PyQt5.QtWidgets import QApplication, QMainWindow, QComboBox, QTextEdit, QPushButton, QHBoxLayout, QVBoxLayout, QWidget
class SerialTool(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.initSerial()
def initUI(self):
# 设置窗口大小
self.setGeometry(100, 100, 400, 300)
# 设置窗口标题
self.setWindowTitle('Serial Tool')
# 添加下拉菜单
self.portComboBox = QComboBox(self)
self.portComboBox.move(10, 10)
self.portComboBox.resize(180, 30)
# 添加文本框
self.textEdit = QTextEdit(self)
self.textEdit.move(10, 50)
self.textEdit.resize(380, 180)
# 添加按钮
self.openButton = QPushButton('Open', self)
self.openButton.move(10, 240)
self.openButton.resize(80, 30)
self.closeButton = QPushButton('Close', self)
self.closeButton.move(100, 240)
self.closeButton.resize(80, 30)
self.sendButton = QPushButton('Send', self)
self.sendButton.move(190, 240)
self.sendButton.resize(80, 30)
self.clearButton = QPushButton('Clear', self)
self.clearButton.move(280, 240)
self.clearButton.resize(80, 30)
# 添加布局管理器
hbox = QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(self.openButton)
hbox.addWidget(self.closeButton)
hbox.addWidget(self.sendButton)
hbox.addWidget(self.clearButton)
vbox = QVBoxLayout()
vbox.addWidget(self.portComboBox)
vbox.addLayout(hbox)
widget = QWidget()
widget.setLayout(vbox)
self.setCentralWidget(widget)
# 绑定按钮事件
self.openButton.clicked.connect(self.openSerial)
self.closeButton.clicked.connect(self.closeSerial)
self.sendButton.clicked.connect(self.sendSerial)
self.clearButton.clicked.connect(self.textEdit.clear)
def initSerial(self):
# 获取可用串口列表
ports = serial.tools.list_ports.comports()
# 将可用串口添加到下拉菜单中
for port in ports:
self.portComboBox.addItem(port.device)
# 创建串口连接
self.ser = serial.Serial()
def openSerial(self):
# 设置串口参数
port = self.portComboBox.currentText()
baudrate = 9600
bytesize = serial.EIGHTBITS
parity = serial.PARITY_NONE
stopbits = serial.STOPBITS_ONE
self.ser.port = port
self.ser.baudrate = baudrate
self.ser.bytesize = bytesize
self.ser.parity = parity
self.ser.stopbits = stopbits
# 打开串口连接
try:
self.ser.open()
self.ser.flushInput()
self.ser.flushOutput()
self.textEdit.append('Serial port is open.')
except Exception as e:
self.textEdit.append(str(e))
def closeSerial(self):
# 关闭串口连接
try:
self.ser.close()
self.textEdit.append('Serial port is closed.')
except Exception as e:
self.textEdit.append(str(e))
def sendSerial(self):
# 发送数据
data = self.textEdit.toPlainText()
try:
self.ser.write(data.encode())
self.textEdit.clear()
self.textEdit.append('Data is sent: ' + data)
except Exception as e:
self.textEdit.append(str(e))
if __name__ == '__main__':
app = QApplication(sys.argv)
serialTool = SerialTool()
serialTool.show()
sys.exit(app.exec_())
```
运行程序后,你应该可以打开串口并发送数据了。
阅读全文