资源简介

基于pyqt5的串口通讯助手程序。可实现串口检测,串口参数设置,数据发送和接受,可设为16进制数据。

资源截图

代码片段和文件信息

import sys
import serial
import serial.tools.list_ports
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtCore import QTimer
from Serial.ui_demo_1 import Ui_Form


class Serial(QtWidgets.QWidget Ui_Form):
    def __init__(self):
        super(Serial self).__init__()
        self.setupUi(self)
        self.init()
        self.setWindowtitle(“串口助手“)
        self.ser = serial.Serial()
        self.port_check()

        # 接收数据和发送数据数目置零
        self.data_num_received = 0
        self.lineEdit.setText(str(self.data_num_received))
        self.data_num_sended = 0
        self.lineEdit_2.setText(str(self.data_num_sended))

    def init(self):
        # 串口检测按钮
        self.s1__box_1.clicked.connect(self.port_check)

        # 串口信息显示
        self.s1__box_2.currentTextChanged.connect(self.port_imf)

        # 打开串口按钮
        self.open_button.clicked.connect(self.port_open)

        # 关闭串口按钮
        self.close_button.clicked.connect(self.port_close)

        # 发送数据按钮
        self.s3__send_button.clicked.connect(self.data_send)

        # 定时发送数据
        self.timer_send = QTimer()
        self.timer_send.timeout.connect(self.data_send)
        self.timer_send_cb.stateChanged.connect(self.data_send_timer)

        # 定时器接收数据
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.data_receive)

        # 清除发送窗口
        self.s3__clear_button.clicked.connect(self.send_data_clear)

        # 清除接收窗口
        self.s2__clear_button.clicked.connect(self.receive_data_clear)

    # 串口检测
    def port_check(self):
        # 检测所有存在的串口,将信息存储在字典中
        self.Com_Dict = {}
        port_list = list(serial.tools.list_ports.comports())
        self.s1__box_2.clear()
        for port in port_list:
            self.Com_Dict[“%s“ % port[0]] = “%s“ % port[1]
            self.s1__box_2.addItem(port[0])
        if len(self.Com_Dict) == 0:
            self.state_label.setText(“ 无串口“)

    # 串口信息
    def port_imf(self):
        # 显示选定的串口的详细信息
        imf_s = self.s1__box_2.currentText()
        if imf_s != ““:
            self.state_label.setText(self.Com_Dict[self.s1__box_2.currentText()])

    # 打开串口
    def port_open(self):
        self.ser.port = self.s1__box_2.currentText()
        self.ser.baudrate = int(self.s1__box_3.currentText())
        self.ser.bytesize = int(self.s1__box_4.currentText())
        self.ser.stopbits = int(self.s1__box_6.currentText())
        self.ser.parity = self.s1__box_5.currentText()

        try:
            self.ser.open()
        except:
            QMessageBox.critical(self “Port Error“ “此串口不能被打开!“)
            return None

        # 打开串口接收定时器,周期为2ms
        self.timer.start(2)

        if self.ser.isOpen():
            self.open_button.setEnabled(False)
            self.close_button.setEnabled(True)
            self.formGroupBox1.settitle(“串口状态(已开启)“)

    # 关闭串口
    def port_close(self):
        self.timer.stop()
        self.timer_send.stop()
        try:
            self.ser.close()
        except

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       6694  2018-07-26 16:59  Serial\pyserial_demo.py

     文件      12570  2018-04-18 20:18  Serial\ui_demo_1.py

     文件      12193  2018-04-18 20:18  Serial\ui_demo_1.ui

     文件       6577  2018-07-26 16:51  Serial\__pycache__\ui_demo_1.cpython-36.pyc

     目录          0  2018-07-26 16:51  Serial\__pycache__

     目录          0  2018-07-26 16:59  Serial

----------- ---------  ---------- -----  ----

                38034                    6


评论

共有 条评论