当前位置:   article > 正文

Python打发无聊时光:14.用PyQt创建一个简易的串口调试助手_串口调试助手代码

串口调试助手代码

第一步:装pyqt5和pyserial库

pip install pyqt5 pyserial

第二步:完整代码

  1. import sys
  2. from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QComboBox, \
  3. QGridLayout, QLineEdit, QTextEdit
  4. from PyQt5.QtCore import QIODevice
  5. from PyQt5.QtSerialPort import QSerialPort, QSerialPortInfo
  6. class SerialDebugHelper(QMainWindow):
  7. def __init__(self):
  8. super(SerialDebugHelper, self).__init__()
  9. self.serial = QSerialPort()
  10. self.serial_port_info_list = QSerialPortInfo.availablePorts()
  11. self.initUI()
  12. def initUI(self):
  13. self.setWindowTitle('串口调试助手')
  14. self.setGeometry(300, 300, 600, 400)
  15. layout = QGridLayout()
  16. self.port_label = QLabel('串口:')
  17. self.baudrate_label = QLabel('波特率:')
  18. self.port_combo_box = QComboBox()
  19. self.baudrate_combo_box = QComboBox()
  20. self.refresh_button = QPushButton('刷新')
  21. self.open_button = QPushButton('打开串口')
  22. self.close_button = QPushButton('关闭串口')
  23. self.send_button = QPushButton('发送')
  24. self.clear_button = QPushButton('清除')
  25. self.send_line_edit = QLineEdit()
  26. self.received_text_edit = QTextEdit()
  27. self.refresh_serial_ports()
  28. self.baudrate_combo_box.addItems(['300', '1200', '2400', '4800', '9600', '19200', '38400', '57600', '115200'])
  29. self.baudrate_combo_box.setCurrentText('9600') # Default to 9600
  30. layout.addWidget(self.port_label, 0, 0)
  31. layout.addWidget(self.port_combo_box, 0, 1)
  32. layout.addWidget(self.baudrate_label, 1, 0)
  33. layout.addWidget(self.baudrate_combo_box, 1, 1)
  34. layout.addWidget(self.refresh_button, 0, 2)
  35. layout.addWidget(self.open_button, 1, 2)
  36. layout.addWidget(self.close_button, 1, 3)
  37. layout.addWidget(self.received_text_edit, 2, 0, 1, 4)
  38. layout.addWidget(self.send_line_edit, 3, 0, 1, 3)
  39. layout.addWidget(self.send_button, 3, 3)
  40. layout.addWidget(self.clear_button, 4, 3)
  41. self.refresh_button.clicked.connect(self.refresh_serial_ports)
  42. self.open_button.clicked.connect(self.open_serial_port)
  43. self.close_button.clicked.connect(self.close_serial_port)
  44. self.send_button.clicked.connect(self.send_data)
  45. self.clear_button.clicked.connect(self.clear_received_data)
  46. central_widget = QWidget()
  47. central_widget.setLayout(layout)
  48. self.setCentralWidget(central_widget)
  49. def refresh_serial_ports(self):
  50. self.port_combo_box.clear()
  51. self.serial_port_info_list = QSerialPortInfo.availablePorts()
  52. for port in self.serial_port_info_list:
  53. self.port_combo_box.addItem(port.portName())
  54. def send_data(self):
  55. if self.serial.isOpen():
  56. data = self.send_line_edit.text().encode()
  57. self.serial.write(data)
  58. self.send_line_edit.clear()
  59. def clear_received_data(self):
  60. self.received_text_edit.clear()
  61. def on_serial_read(self):
  62. bytes_to_read = self.serial.bytesAvailable()
  63. text = self.serial.read(bytes_to_read).decode()
  64. self.received_text_edit.insertPlainText(text)
  65. def open_serial_port(self):
  66. port_name = self.port_combo_box.currentText()
  67. baud_rate = int(self.baudrate_combo_box.currentText())
  68. self.serial.setPortName(port_name)
  69. self.serial.setBaudRate(baud_rate)
  70. self.serial.setDataBits(QSerialPort.Data8)
  71. self.serial.setParity(QSerialPort.NoParity)
  72. self.serial.setStopBits(QSerialPort.OneStop)
  73. self.serial.setFlowControl(QSerialPort.NoFlowControl)
  74. if self.serial.open(QIODevice.ReadWrite):
  75. self.serial.readyRead.connect(self.on_serial_read)
  76. self.open_button.setEnabled(False)
  77. self.close_button.setEnabled(True)
  78. def close_serial_port(self):
  79. if self.serial.isOpen():
  80. self.serial.close()
  81. self.open_button.setEnabled(True)
  82. self.close_button.setEnabled(False)
  83. def closeEvent(self, event):
  84. self.close_serial_port()
  85. event.accept()
  86. if __name__ == "__main__":
  87. app = QApplication(sys.argv)
  88. mainWin = SerialDebugHelper()
  89. mainWin.show()
  90. sys.exit(app.exec_())

第三步:运行结果

你可以用单片机或含串口的外设去测试,也可以下一个虚拟串口,附上网盘链接如下:

链接:https://pan.baidu.com/s/1V6w7vNGvMen6tspPJQcKiw 
提取码:1bss 
然后选择添加虚拟串口1、串口2,并且用一个现成的串口调试助手去测试;在你运行的串口助手选择串口号1,现成的串口调试助手选择串口2,并且二者波特率选择一致,就可以互相通讯了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/764518
推荐阅读
相关标签
  

闽ICP备14008679号