所以这是我的问题,我每5秒读取一次串行电缆的数据,并将其存储在一个CSV文件中。我也把这些数据整理成一个列表。我想做的是获取变量5、7和9,并将它们显示在我的GUI中,其中有qtextbox。。。我该怎么做?在
变量列表将位于一个名为listvalues的值中。我想调用5、7和9,让它们显示在PyQt窗口中各自的文本框中。在
这是我的代码:
from PyQt4 import QtGui
import sys
import masimo
import csv
import time
import datetime as DT
import threading
from threading import Thread
import serial
import os
os.chdir(r"C:\Users\SpO2\Desktop\Data")
time = time.strftime("%d %b %Y %H%M%S")
location = r'%s.csv' % time
outputfile = open(location, mode='x', newline='')
outputWriter = csv.writer(outputfile)
outputWriter.writerow(["start"])
outputfile.close()
port = "COM4"
class ExampleApp(QtGui.QMainWindow, masimo.Ui_MainWindow):
def __init__(self, parent=None):
super(self.__class__, self).__init__()
self.setupUi(self)
def SerialRead():
delay1 = DT.datetime.now()
ser = serial.Serial(port, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
out = ser.read(167)
reading = str(out)
plaintext1 = reading.replace(' ', ', ')
plaintext = plaintext1.replace('=', ', ')
listvalue = plaintext.split(", ")
ser.close()
outputfile = open(location, mode='a', newline='')
outputWriter = csv.writer(outputfile)
outputWriter.writerow([plaintext])
outputfile.close()
delay2 = DT.datetime.now()
differencetime = (delay2 - delay1).total_seconds()
writedelay = int(5)
restart = (writedelay - differencetime)
threading.Timer(restart, SerialRead).start()
def main():
app = QtGui.QApplication(sys.argv)
form = ExampleApp()
QtGui.QApplication.processEvents()
form.show()
app.exec_()
if __name__ == '__main__':
Thread(target = SerialRead).start()
Thread(target = main).start()
我想你可以做两件事之一:
我可能会使用一个事件,以便您知道线程正在休眠,并且您不会在线程写入值时尝试读取值。在
在您的
ExampleApp
类中,您需要存储事件并处理超时:您的
^{pr2}$SerialRead
函数需要接受一个参数,即线程事件,并且需要在串行读取之后重新启动之前设置该事件:您的
main
函数还需要接受一个事件参数,以便传递给初始值设定项,例如app:最后,在
if __name__ == '__main__':
部分,需要创建线程事件并将其传递给线程调用:相关问题 更多 >
编程相关推荐