def data_read( self): try : num = self.ser.inwaiting(print( ' recive num is' , num)eXeept : self - port__close(return None ifnum > 0: data - self.ser .read( num)num - len( data) self.plainTextEdit.appendPlainText("<< "+data.decode( "utf-8'))else:
时间: 2024-02-26 14:53:28 浏览: 78
这段代码是一个 PyQt5 应用程序中的一个函数。它会尝试读取串行通信(serial communication)接收缓冲区中的数据。首先,它会使用 inwaiting() 方法获取接收缓冲区中的字节数,然后将这个字节数打印出来。如果接收缓冲区中有数据,就使用 read() 方法读取数据并将其解码成字符串,并将这个字符串显示在文本框中(plainTextEdit)。如果接收缓冲区中没有数据,则什么也不做。这段代码应该是用于处理设备发送给应用程序的数据。
相关问题
self.main_ui.pushButton_2.clicked.connect(self.thing1) 没反应是为什么 class Worker(QtCore.QThread): sinOut = pyqtSignal(str) def __init__(self, parent=None): super(Worker, self).__init__(parent) # 设置工作状态与初始num数值 self.working = True self.num = 0 #def __del__(self): # 线程状态改变与线程终止 #self.working = False #self.wait() def stop(self): #线程状态改变与线程终止 self.working = False self.wait() def run(self): self.working = True while self.working == True: #file_str = 'File index{0}'.format(self.num) self.num += 1 # 发射信号 #self.sinOut.emit(file_str) self.sinOut.emit('1') # 线程休眠2秒 self.msleep(5) class parentWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.main_ui = JQ.Ui_MainWindow() self.main_ui.setupUi(self) self.thread1 = Worker() self.main_ui.pushButton_2.clicked.connect(self.thing1) self.thread1.sinOut.connect(self.printt) def thing1(self): #self.main_ui.pushButton.setEnabled(False) print('9999999999') self.thread1.start() print('123') #self.sleep(2) def printt(self): print('7777') def ok(): print('ok') # def hourstest(): # thread1 = Worker() # thread1.start() # com = JQ.Ui_MainWindow().comboBox_2.currentText() # ser = serial.Serial('com3', 1200, timeout=1) # data = bytes.fromhex( # '68 01 00 20 00 00 00 00 00 34 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 F4 16 ') # ser.write(data) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) widget = QtWidgets.QMainWindow() widget = parentWindow() ui = JQ.Ui_MainWindow() # 这是原py中的类,因人而异哦 ui.setupUi(widget) ui.discoverSerial() #串口发现 #ui.pushButton_2.clicked.connect(widget.thing1) widget.show() sys.exit(app.exec_())
首先,你的连接语句应该是正确的。但是,你的槽函数`thing1`中启动了一个线程并且没有等待该线程完成。因此,当你再次点击`pushButton_2`时,会创建一个新的线程,导致出现问题。
你需要在`thing1`函数中添加`self.thread1.wait()`语句来等待线程完成。这样,当你再次点击`pushButton_2`时,它会等待线程完成后再启动一个新的线程。
另外,你在`ok`函数中定义了一个serial对象,但是没有使用它。如果你想使用该对象,你需要在该函数中添加相关代码。
以下是修改后的代码:
```
class parentWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.main_ui = JQ.Ui_MainWindow()
self.main_ui.setupUi(self)
self.thread1 = Worker()
self.thread1.sinOut.connect(self.printt)
self.main_ui.pushButton_2.clicked.connect(self.thing1)
def thing1(self):
print('9999999999')
self.thread1.start()
self.thread1.wait() # 等待线程完成
print('123')
def printt(self):
print('7777')
def ok():
print('ok')
com = JQ.Ui_MainWindow().comboBox_2.currentText()
ser = serial.Serial('com3', 1200, timeout=1)
data = bytes.fromhex('68 01 00 20 00 00 00 00 00 34 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 F4 16 ')
ser.write(data)
```
这段代码的pushButton_2 为什么触发不了线程 class Worker(QtCore.QThread): sinOut = pyqtSignal(str) def __init__(self, parent=None): super(Worker, self).__init__(parent) # 设置工作状态与初始num数值 self.working = True self.num = 0 #def __del__(self): # 线程状态改变与线程终止 #self.working = False #self.wait() def stop(self): #线程状态改变与线程终止 self.working = False self.wait() def run(self): self.working = True while self.working == True: #file_str = 'File index{0}'.format(self.num) self.num += 1 # 发射信号 #self.sinOut.emit(file_str) self.sinOut.emit('1') # 线程休眠2秒 self.msleep(5) class parentWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.main_ui = JQ.Ui_MainWindow() self.main_ui.setupUi(self) self.thread1 = Worker() self.main_ui.pushButton_2.clicked.connect(self.thing1) self.thread1.sinOut.connect(self.printt) def thing1(self): #self.main_ui.pushButton.setEnabled(False) print('9999999999') self.thread1.start() print('123') #self.sleep(2) def printt(self): print('7777') def ok(): print('ok') # def hourstest(): # thread1 = Worker() # thread1.start() # com = JQ.Ui_MainWindow().comboBox_2.currentText() # ser = serial.Serial('com3', 1200, timeout=1) # data = bytes.fromhex( # '68 01 00 20 00 00 00 00 00 34 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 F4 16 ') # ser.write(data) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) widget = QtWidgets.QMainWindow() widget = parentWindow() ui = JQ.Ui_MainWindow() # 这是原py中的类,因人而异哦 ui.setupUi(widget) ui.discoverSerial() #串口发现 #ui.pushButton_2.clicked.connect(widget.thing1) widget.show() sys.exit(app.exec_())
在你的代码中,`pushButton_2` 被连接到了 `thing1` 函数,而 `thing1` 函数中又启动了 `thread1` 线程。但是,在 `thing1` 函数中,你没有设置任何条件来等待线程结束,而是直接执行下一条语句,即打印 `123`。因此,在 `thing1` 函数执行完 `print('123')` 后,主线程就会继续往下执行,而 `thread1` 线程也会继续执行,导致你看不到 `printt` 函数的输出。
为了让主线程等待 `thread1` 线程执行完毕后再继续执行,你可以在 `thing1` 函数中加入 `thread1.wait()` 语句,这样主线程就会等待 `thread1` 线程执行完毕后再继续执行。修改后的代码如下:
```
class parentWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.main_ui = JQ.Ui_MainWindow()
self.main_ui.setupUi(self)
self.thread1 = Worker()
self.main_ui.pushButton_2.clicked.connect(self.thing1)
self.thread1.sinOut.connect(self.printt)
def thing1(self):
print('9999999999')
self.thread1.start()
self.thread1.wait() # 等待 thread1 线程执行完毕
print('123')
def printt(self):
print('7777')
```
这样,当你点击 `pushButton_2` 触发 `thing1` 函数时,主线程会等待 `thread1` 线程执行完毕后再继续执行,你就可以看到 `printt` 函数的输出了。
阅读全文