pyqt5 串口助手运行点击打开串口报错Process finished with exit code -1073740791 (0xC0000409)

喜你入骨 提交于 2020-02-19 14:18:13

pyqt5 串口助手运行点击打开串口报错Process finished with exit code -1073740791 (0xC0000409)
请大神帮忙解决一下,谢谢!

插入真实USB转串口可以正常读取到串口端口
其他按键都可以正常实现功能,唯有打开串口按键不能点击:
在这里插入图片描述
在这里插入图片描述
代码如下:

-- coding: utf-8 --

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

#跟随窗口自适应
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)

class Pyqt5_Serial(QtWidgets.QWidget, Ui_Form):

def __init__(self):
    super(Pyqt5_Serial, self).__init__()
    self.setupUi(self)
    self.init()
    self.setWindowTitle("串口调试页面")
    self.ser = serial.Serial()
    self.port_check()

#   接收数据和发送数据数目置零
    self.data_num_received = 0
    self.Data_num_received_le.setText(str(self.data_num_received))
    self.data_num_sended = 0
    self.Data_num_sended_le.setText(str(self.data_num_sended))
#
def init(self):
    # 串口检测按钮
    self.Port_check_btn.clicked.connect(self.port_check)

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

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

    # 关闭串口按钮
    self.Port_close_btn.clicked.connect(self.port_close)
#     # 发送数据按钮
    self.Data_send_btn.clicked.connect(self.data_send)

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

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

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

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

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

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

# 打开串口
def port_open(self):
    self.ser.port = self.Select_port_cb.currentText()
    self.ser.baudrate = int(self.Baudrate_cb.currentText())
    self.ser.bytesize = int(self.Bytesize_cb.currentText())
    self.ser.stopbits = int(self.Stopbits_cb.currentText())
    self.ser.parity = self.Parity_cb.currentText()

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

    # 打开串口接收定时器,周期为2ms
    self.timer.start(2)
    if self.ser.isOpen():
        self.Port_open_btn.setEnabled(False)
        self.Port_close_btn.setEnabled(True)
        self.groupBox_2.setTitle("串口状态(已开启)")
    else:
        self.groupBox_2.setTitle("串口状态(未开启)")
# 关闭串口
def port_close(self):
    self.timer.stop()
    self.timer_send.stop()
    try:
        self.ser.close()
    except:
        pass
    self.Port_open_btn.setEnabled(True)
    self.Port_close_btn.setEnabled(False)
    self.Data_num_sended_le.setEnabled(True)
    # 接收数据和发送数据数目置零
    self.data_num_received = 0
    self.Data_num_received_le.setText(str(self.data_num_received))
    self.data_num_sended = 0
    self.Data_num_sended_le.setText(str(self.data_num_sended))
    self.groupBox_2.setTitle("串口状态(已关闭)")

# 发送数据
def data_send(self):
    if self.ser.isOpen():
        input_s = self.Send_text.toPlainText()
        print (input_s)
        if input_s != "":
            # 非空字符串
            if self.Hex_send_checkbox.isChecked():
                # hex发送
                input_s = input_s.strip()
                send_list = []
                while input_s != '':
                    try:
                        num = int(input_s[0:2], 16)
                    except ValueError:
                        QMessageBox.critical(self, 'wrong data', '请输入十六进制数据,以空格分开!')
                        return None
                    input_s = input_s[2:].strip()
                    send_list.append(num)
                input_s = bytes(send_list)
                print (input_s)
            else:
                # ascii发送
                input_s = (input_s + '\r\n').encode('utf-8')

            num = self.ser.write(input_s)
            self.data_num_sended += num
            self.Data_num_sended_le.setText(str(self.data_num_sended))
    else:
        pass

# 接收数据
def data_receive(self):
    try:
        num = self.ser.inWaiting()
    except:
        self.port_close()
        return None
    if num > 0:
        data = self.ser.read(num)
        num = len(data)
        # hex显示
        if self.Hex_receive_checkbox.checkState():
            out_s = ''
            for i in range(0, len(data)):
                out_s = out_s + '{:02X}'.format(data[i]) + ' '
            self.Receive_text.insertPlainText(out_s)
        else:
            # 串口接收到的字符串为b'123',要转化成unicode字符串才能输出到窗口中去
            self.Receive_text.insertPlainText(data.decode('utf-8'))

        # 统计接收字符的数量
        self.data_num_received += num
        self.Data_num_received_le.setText(str(self.data_num_received))

        # 获取到text光标
        textCursor = self.Receive_text.textCursor()
        # 滚动到底部
        textCursor.movePosition(textCursor.End)
        # 设置光标到text中去
        self.Receive_text.setTextCursor(textCursor)
    else:
        pass
#
# 定时发送数据
def data_send_timer(self):
    if self.Data_send_timer_checkbox.isChecked():
        self.timer_send.start(int(self.Loop_time_le.text()))
        self.Loop_time_le.setEnabled(False)
    else:
        self.timer_send.stop()
        self.Loop_time_le.setEnabled(True)

# 清除显示
def send_data_clear(self):
    self.Send_text.setText("")

def receive_data_clear(self):
    self.Receive_text.setText("")

if name == ‘main’:
app = QtWidgets.QApplication(sys.argv)
myshow = Pyqt5_Serial()
myshow.show()
sys.exit(app.exec_())

UI生成PY文件

-- coding: utf-8 --

Form implementation generated from reading ui file ‘Serial.ui’

Created by: PyQt5 UI code generator 5.13.0

WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(“Form”)
Form.resize(653, 460)
self.Send_data_clear_btn = QtWidgets.QPushButton(Form)
self.Send_data_clear_btn.setGeometry(QtCore.QRect(570, 280, 56, 17))
self.Send_data_clear_btn.setObjectName(“Send_data_clear_btn”)
self.Hex_send_checkbox = QtWidgets.QCheckBox(Form)
self.Hex_send_checkbox.setGeometry(QtCore.QRect(570, 230, 54, 13))
self.Hex_send_checkbox.setObjectName(“Hex_send_checkbox”)
self.Loop_time_le = QtWidgets.QLineEdit(Form)
self.Loop_time_le.setGeometry(QtCore.QRect(290, 431, 61, 16))
self.Loop_time_le.setObjectName(“Loop_time_le”)
self.Data_send_timer_checkbox = QtWidgets.QCheckBox(Form)
self.Data_send_timer_checkbox.setGeometry(QtCore.QRect(232, 432, 54, 16))
self.Data_send_timer_checkbox.setObjectName(“Data_send_timer_checkbox”)
self.Hex_receive_checkbox = QtWidgets.QCheckBox(Form)
self.Hex_receive_checkbox.setGeometry(QtCore.QRect(570, 50, 54, 13))
self.Hex_receive_checkbox.setObjectName(“Hex_receive_checkbox”)
self.label_14 = QtWidgets.QLabel(Form)
self.label_14.setGeometry(QtCore.QRect(360, 430, 23, 16))
self.label_14.setObjectName(“label_14”)
self.Receive_data_clear_btn = QtWidgets.QPushButton(Form)
self.Receive_data_clear_btn.setGeometry(QtCore.QRect(570, 90, 56, 17))
self.Receive_data_clear_btn.setObjectName(“Receive_data_clear_btn”)
self.Data_send_btn = QtWidgets.QPushButton(Form)
self.Data_send_btn.setGeometry(QtCore.QRect(570, 250, 56, 17))
self.Data_send_btn.setObjectName(“Data_send_btn”)
self.groupBox = QtWidgets.QGroupBox(Form)
self.groupBox.setGeometry(QtCore.QRect(20, 10, 131, 351))
self.groupBox.setObjectName(“groupBox”)
self.widget = QtWidgets.QWidget(self.groupBox)
self.widget.setGeometry(QtCore.QRect(0, 10, 131, 341))
self.widget.setStyleSheet("")
self.widget.setObjectName(“widget”)
self.gridLayout = QtWidgets.QGridLayout(self.widget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setSpacing(0)
self.gridLayout.setObjectName(“gridLayout”)
self.Port_check_btn = QtWidgets.QPushButton(self.widget)
self.Port_check_btn.setObjectName(“Port_check_btn”)
self.gridLayout.addWidget(self.Port_check_btn, 1, 2, 1, 1)
self.label_3 = QtWidgets.QLabel(self.widget)
self.label_3.setObjectName(“label_3”)
self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
self.label_8 = QtWidgets.QLabel(self.widget)
self.label_8.setObjectName(“label_8”)
self.gridLayout.addWidget(self.label_8, 7, 0, 1, 1)
self.state_label = QtWidgets.QLabel(self.widget)
self.state_label.setMaximumSize(QtCore.QSize(16777215, 30))
self.state_label.setObjectName(“state_label”)
self.gridLayout.addWidget(self.state_label, 3, 0, 1, 3)
self.Select_port_cb = QtWidgets.QComboBox(self.widget)
self.Select_port_cb.setObjectName(“Select_port_cb”)
self.gridLayout.addWidget(self.Select_port_cb, 2, 2, 1, 1)
self.Stopbits_cb = QtWidgets.QComboBox(self.widget)
self.Stopbits_cb.setObjectName(“Stopbits_cb”)
self.Stopbits_cb.addItem("")
self.Stopbits_cb.addItem("")
self.gridLayout.addWidget(self.Stopbits_cb, 7, 2, 1, 1)
self.Baudrate_cb = QtWidgets.QComboBox(self.widget)
self.Baudrate_cb.setObjectName(“Baudrate_cb”)
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.Baudrate_cb.addItem("")
self.gridLayout.addWidget(self.Baudrate_cb, 4, 2, 1, 1)
self.Port_open_btn = QtWidgets.QPushButton(self.widget)
self.Port_open_btn.setCheckable(False)
self.Port_open_btn.setChecked(False)
self.Port_open_btn.setObjectName(“Port_open_btn”)
self.gridLayout.addWidget(self.Port_open_btn, 8, 0, 1, 3)
self.Port_close_btn = QtWidgets.QPushButton(self.widget)
self.Port_close_btn.setObjectName(“Port_close_btn”)
self.gridLayout.addWidget(self.Port_close_btn, 9, 0, 1, 3)
self.label_6 = QtWidgets.QLabel(self.widget)
self.label_6.setObjectName(“label_6”)
self.gridLayout.addWidget(self.label_6, 5, 0, 1, 1)
self.Parity_cb = QtWidgets.QComboBox(self.widget)
self.Parity_cb.setObjectName(“Parity_cb”)
self.Parity_cb.addItem("")
self.Parity_cb.addItem("")
self.gridLayout.addWidget(self.Parity_cb, 6, 2, 1, 1)
self.Bytesize_cb = QtWidgets.QComboBox(self.widget)
self.Bytesize_cb.setObjectName(“Bytesize_cb”)
self.Bytesize_cb.addItem("")
self.Bytesize_cb.addItem("")
self.Bytesize_cb.addItem("")
self.Bytesize_cb.addItem("")
self.gridLayout.addWidget(self.Bytesize_cb, 5, 2, 1, 1)
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setMaximumSize(QtCore.QSize(16777215, 30))
self.label_2.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)
self.label_2.setObjectName(“label_2”)
self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
self.label_5 = QtWidgets.QLabel(self.widget)
self.label_5.setObjectName(“label_5”)
self.gridLayout.addWidget(self.label_5, 4, 0, 1, 1)
self.label_7 = QtWidgets.QLabel(self.widget)
self.label_7.setObjectName(“label_7”)
self.gridLayout.addWidget(self.label_7, 6, 0, 1, 1)
self.groupBox_2 = QtWidgets.QGroupBox(Form)
self.groupBox_2.setGeometry(QtCore.QRect(20, 370, 131, 81))
self.groupBox_2.setObjectName(“groupBox_2”)
self.widget_4 = QtWidgets.QWidget(self.groupBox_2)
self.widget_4.setGeometry(QtCore.QRect(0, 10, 131, 81))
self.widget_4.setStyleSheet("")
self.widget_4.setObjectName(“widget_4”)
self.gridLayout_2 = QtWidgets.QGridLayout(self.widget_4)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setObjectName(“gridLayout_2”)
self.Data_num_sended_le = QtWidgets.QLineEdit(self.widget_4)
self.Data_num_sended_le.setObjectName(“Data_num_sended_le”)
self.gridLayout_2.addWidget(self.Data_num_sended_le, 2, 2, 1, 1)
self.label_10 = QtWidgets.QLabel(self.widget_4)
self.label_10.setObjectName(“label_10”)
self.gridLayout_2.addWidget(self.label_10, 1, 0, 1, 1)
self.label_11 = QtWidgets.QLabel(self.widget_4)
self.label_11.setObjectName(“label_11”)
self.gridLayout_2.addWidget(self.label_11, 2, 0, 1, 1)
self.Data_num_received_le = QtWidgets.QLineEdit(self.widget_4)
self.Data_num_received_le.setObjectName(“Data_num_received_le”)
self.gridLayout_2.addWidget(self.Data_num_received_le, 1, 2, 1, 1)
self.groupBox_4 = QtWidgets.QGroupBox(Form)
self.groupBox_4.setGeometry(QtCore.QRect(160, 340, 401, 81))
self.groupBox_4.setObjectName(“groupBox_4”)
self.Send_text = QtWidgets.QTextEdit(self.groupBox_4)
self.Send_text.setGeometry(QtCore.QRect(13, 10, 381, 64))
self.Send_text.setObjectName(“Send_text”)
self.groupBox_3 = QtWidgets.QGroupBox(Form)
self.groupBox_3.setGeometry(QtCore.QRect(160, 10, 401, 321))
self.groupBox_3.setObjectName(“groupBox_3”)
self.Receive_text = QtWidgets.QTextEdit(self.groupBox_3)
self.Receive_text.setGeometry(QtCore.QRect(10, 13, 381, 301))
self.Receive_text.setObjectName(“Receive_text”)

    self.retranslateUi(Form)
    self.Send_data_clear_btn.clicked.connect(Form.send_data_clear)
    self.Receive_data_clear_btn.clicked.connect(Form.receive_data_clear)
    self.Data_send_btn.clicked.connect(Form.data_send)
    self.Data_send_timer_checkbox.clicked.connect(Form.data_send_timer)
    self.Select_port_cb.currentTextChanged['QString'].connect(Form.port_imf)
    self.Port_check_btn.clicked.connect(Form.port_check)
    self.Port_open_btn.clicked.connect(Form.port_open)
    self.Port_close_btn.clicked.connect(Form.port_close)
    QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Form"))
    self.Send_data_clear_btn.setText(_translate("Form", "清除"))
    self.Hex_send_checkbox.setText(_translate("Form", "Hex发送"))
    self.Loop_time_le.setText(_translate("Form", "1000"))
    self.Data_send_timer_checkbox.setText(_translate("Form", "定时发送"))
    self.Hex_receive_checkbox.setText(_translate("Form", "Hex接收"))
    self.label_14.setText(_translate("Form", "ms/次"))
    self.Receive_data_clear_btn.setText(_translate("Form", "清除"))
    self.Data_send_btn.setText(_translate("Form", "发送"))
    self.groupBox.setTitle(_translate("Form", "串口设置"))
    self.Port_check_btn.setText(_translate("Form", "端口检测"))
    self.label_3.setText(_translate("Form", "串口选择:"))
    self.label_8.setText(_translate("Form", "停止位:"))
    self.state_label.setText(_translate("Form", "Serial port(COM1-COM10)"))
    self.Stopbits_cb.setItemText(0, _translate("Form", "0"))
    self.Stopbits_cb.setItemText(1, _translate("Form", "1"))
    self.Baudrate_cb.setItemText(0, _translate("Form", "9600"))
    self.Baudrate_cb.setItemText(1, _translate("Form", "115200"))
    self.Baudrate_cb.setItemText(2, _translate("Form", "2400"))
    self.Baudrate_cb.setItemText(3, _translate("Form", "4800"))
    self.Baudrate_cb.setItemText(4, _translate("Form", "14400"))
    self.Baudrate_cb.setItemText(5, _translate("Form", "19200"))
    self.Baudrate_cb.setItemText(6, _translate("Form", "38400"))
    self.Baudrate_cb.setItemText(7, _translate("Form", "57600"))
    self.Baudrate_cb.setItemText(8, _translate("Form", "76800"))
    self.Baudrate_cb.setItemText(9, _translate("Form", "12800"))
    self.Baudrate_cb.setItemText(10, _translate("Form", "230400"))
    self.Baudrate_cb.setItemText(11, _translate("Form", "460800"))
    self.Port_open_btn.setText(_translate("Form", "打开串口"))
    self.Port_close_btn.setText(_translate("Form", "关闭串口"))
    self.label_6.setText(_translate("Form", "数据位:"))
    self.Parity_cb.setItemText(0, _translate("Form", "N"))
    self.Parity_cb.setItemText(1, _translate("Form", "1"))
    self.Bytesize_cb.setItemText(0, _translate("Form", "8"))
    self.Bytesize_cb.setItemText(1, _translate("Form", "7"))
    self.Bytesize_cb.setItemText(2, _translate("Form", "6"))
    self.Bytesize_cb.setItemText(3, _translate("Form", "5"))
    self.label_2.setText(_translate("Form", "串口检测:"))
    self.label_5.setText(_translate("Form", "波特率:"))
    self.label_7.setText(_translate("Form", "校验位:"))
    self.groupBox_2.setTitle(_translate("Form", "串口状态"))
    self.label_10.setText(_translate("Form", "已接收:"))
    self.label_11.setText(_translate("Form", "已发送:"))
    self.groupBox_4.setTitle(_translate("Form", "发送区"))
    self.Send_text.setHtml(_translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"

“<meta name=“qrichtext” content=“1” /><style type=“text/css”>\n”
“p, li { white-space: pre-wrap; }\n”
“<body style=” font-family:‘SimSun’; font-size:9pt; font-weight:400; font-style:normal;">\n"
“<p style=” margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">123456

"))
self.groupBox_3.setTitle(_translate(“Form”, “接收区”))

if name == “main”:
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!