只有当我的应用程序从串行端口接收数据时,我才遇到调用我的方法之一的问题。
我熟悉信号和插槽的概念,但一般情况下会返回错误。
这是我的尝试:
from PyQt5 import uic
from PyQt5.QtWidgets import QWidget, QApplication
import sys
from PyQt5.QtSerialPort import QSerialPort
from PyQt5.QtCore import QIODevice, QObject, pyqtSignal
class MyWindow(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self)
uic.loadUi("widget.ui", self)
self.lcdNumber.display(24)
self.pushButton_2.setAutoRepeat(True)
# "general" approach, which works exellent
self.pushButton_2.pressed.connect(self.slider_one_plus)
self.serial = QSerialPort(self)
# how can I connect signal readyRead() to my slot do_after_data_recieved
# self.connect(???)
# QIODevice.readyRead(self).connect(???)
def do_after_data_recieved(self):
print('Data has been recieved!')
def configure_serial_ports_parameters(self):
PORT = 'COM3'
self.serial.setPortName(port=PORT)
if self.serial.open(QIODevice.ReadWrite):
self.serial.setBaudRate(115200)
self.serial.readyRead.connect(self.on_serial_read)
# clear the text
self.clear()
# Send a Control-C
self.serial.write(b'\x03')
else:
print("Cannot connect to device on port {}".format(PORT))
raise IOError("Cannot connect to device on port {}".format(PORT))
据我了解,readyReady信号是从QIODevice继承的,我需要某种方式从我在代码中实现的QIODevice发出信号,但是 如何将信号readyRead()连接到插槽do_after_data_recieved?
操作系统-我认为并不重要,但就我而言,它是Windows 8.1 Python 3.7 库-PyQt5
任何帮助将不胜感激,谢谢!