为什么我的Pyqt5绘图线程不起作用?

时间:2018-10-02 09:33:52

标签: python pyqt qthread pyqtgraph

我有一个pyqt5 GUI。我通过以太网接收大量数据,之后必须对其进行绘图。我有一个循环,但是当我在绘制GUi的另一部分时,它变得很慢,不会立即响应。因此,我尝试使用Qthread,但是现在我的图冻结了,我再也没有得到图形。我打印照片以查看程序在哪里,但我从来没有将插槽之一连接到线程信号上。我将不胜感激任何建议或帮助。提前致谢。这是程序的一部分。我有更多功能,图形部分是tabs.py。

import tabs
from PyQt5.QtGui import QTabWidget, QApplication
import struct
import socket
import sys
import time
import numpy as np
from PyQt5.QtGui import QPainter, QFont, QColor, QPen, QBrush
from PyQt5.QtCore import QTimer, pyqtSlot, pyqtSignal,  QThread
import PyQt5.QtCore
import pyqtgraph
from openpyxl import Workbook, load_workbook
import datetime

sck=0
count=0
r=1
row=1

class ThreadPlot(QThread):

    Threadraw= pyqtSignal(int, np.ndarray, tuple,int, tuple)

    def __init__(self,x,dato,sensors,plot,dac):
        QThread.__init__(self)
        self.x=x
        print(self.x)
        self.dato= dato
        print("este es el dato",self.dato)
        self.sensors= sensors
        print("este es el sensors",self.sensors)
        self.plot= plot
        print("este es el plot",self.plot)
        self.dac=dac
        print("este es el dac",self.dac)     

    def run(self):
        print("estoy en el thread")
        time.sleep(5)
        self.Threadraw.emit(self.x, self.dato, self.sensors, self.plot, 
        self.dac)
        print("ya emiti la sennal")

class Zedboard:
    def __init__(self, address, port):
        self.address = address
        self.port = port
        self.sock = None

    def connect(self,n):
        global sck
        if (n==0):

            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            server_address = (self.address, self.port)
            print("connecting to %s port %s" % server_address, file=sys.stderr) 
            self.sock.connect(server_address)
        if (n==1):

            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            server_address = (self.address, self.port)
            print("connecting to %s port %s" % server_address, file=sys.stderr) 
            self.sock.connect(server_address)   
            sck=0


    def set_delay(self, delay):
        data_pay = 'mem_write,'+str(0x43C00010)+','+ str(delay)+','
        #print(str.encode(data_pay))
        self.sock.send(str.encode(data_pay))
        delay= 0.3
        time.sleep(delay)

    def set_edge(self, edge):
        data_pay = 'mem_write,'+str(0x43C0000c)+','+ str(edge)+','
        self.sock.send(str.encode(data_pay))
        delay= 0.3
        time.sleep(delay)

    def set_level(self, level):
        data_pay = 'mem_write,'+str(0x43C00008)+','+ str(level)+','
        #print(str.encode(data_pay))
        self.sock.send(str.encode(data_pay))
        delay= 0.3
        time.sleep(delay)

    def set_DAC(self, dac_value,auto,press, temp):
        send=0
        data_pay = 'dac_write,'+str(auto)+','+ str(dac_value)+','+ str(press)+','+ str(temp)+','+ str(send)
        #print(str.encode(data_pay))
        self.sock.send(str.encode(data_pay))
        delay= 0.3
        time.sleep(delay) 

    def set_DF(self, dec_fac):
        data_pay = 'dec_factor,'+str(dec_fac)+ ','
        print('mande el dec factor')
        #print(str.encode(data_pay))
        self.sock.send(str.encode(data_pay))
        delay= 0.1
        time.sleep(delay)

    def set_thr_ts(self,threshold):
        data_pay = 'thr_write,'+str(threshold)+','
        self.sock.send(str.encode(data_pay))
        print("mande el thr",threshold)
        delay= 0.3
        time.sleep(delay)

    def set_CNT(self, count):
        data_pay = 'gen_histo,'+str(count)+ ','
        print('mande el dec count')
        #print(str.encode(data_pay))
        self.sock.send(str.encode(data_pay))
        delay= 0.1
        time.sleep(delay) 

    def stop(self):
        data_pay ='stop,' #stop
        self.sock.send(str.encode(data_pay))
        delay= 0.1
        time.sleep(delay) 

    def cerrar(self):
        data_pay ='quit,' #stop
        self.sock.send(str.encode(data_pay))
        delay= 0.1
        time.sleep(delay)     

#Revisar estoooooooooooooooooooooooooooooooooooooo
    def get_data(self, size=131072):
        data_pay = 'mem_read,'
        size = 131072
        global sy
        sy=0
        self.sock.send(str.encode(data_pay))
        final_data=0
        while final_data < size:
            data_rec = self.sock.recv(131072)
            final_data += len(data_rec)
        print('len data_rec: ',len(data_rec))
        delay= 0.21
        time.sleep(delay)
        amount_received = 0
        amount_expected = 20     
        while amount_received < amount_expected:
            sy = self.sock.recv(20)
            amount_received += len(sy)
        print('sensors len =',len(sy))
        sensors=struct.unpack('!ddf',sy)
        print(sensors)
        delay= 0.21
        time.sleep(delay)
        val = self.sock.recv(4)
        value_dac=struct.unpack('I',val)
        print('este es el dac', value_dac)
        delay= 0.21
        time.sleep(delay)
        data_seg = self.sock.recv(8)
        print('len data_rec: ',len(data_seg))
        data_cruda=struct.unpack('II',data_seg)
        print('este es data1', data_cruda[1])
        nanosegundos=(data_cruda[1]/1000000000)#*4
        print('este es data0', data_cruda[0])
        print('esta es segundos =',nanosegundos)
        data_seg=nanosegundos+data_cruda[0]
        print('esta es la data_rec =',data_seg) 
        return data_rec, sensors, value_dac, data_seg

#Ver si de veras me carga la GUI
class Application(QTabWidget, tabs.Ui_TabWidget): 
    def __init__(self, zedboard):
        QTabWidget.__init__(self)
        self.setupUi(self)
        self.zedboard = zedboard
         #self.hopethread= ThreadDraw()
        self.setUpCallbacks()
        self.zedboard.connect(0)

    def setUpCallbacks(self):
        self.PBtnSend.clicked.connect(self.sendParameters)
        self.PBtnData.clicked.connect(self.getData)
        self.pushButton.clicked.connect(self.getHistogramData)
        self.TSBtn.clicked.connect(self.getTime)
        self.DACBtn.clicked.connect(self.get_DAC)
        self.HBtn.clicked.connect(self.get_HistPL)
        self.StopBtn.clicked.connect(self.Stop)

    def getData(self, size=8): #131072
        global sck, count,r
        sck=1
        wa=load_workbook("test1.xlsx")
        ws = wa.active
        while sck:
            max_row=ws.max_row
            dac=[0]
            y,sensors,dac,seg = self.zedboard.get_data()
            print(str(len(y))+ "estoy aqui")
            x=0
            data = np.fromstring(y[0:131072], np.int32)
            print(sensors)
            self.graph.clear()
            plot=0
            print(type(data))
            print(type(x))
            print(type(sensors))
            print(type(dac))
            print(type(plot))
            self.hopethread=ThreadPlot(x,data,sensors, plot,dac)
 self.hopethread.Threadraw.connect(self.dibujar,PyQt5.QtCore.Qt.DirectConnection )
            print("pase la llamada de dibujar")
            self.hopethread.start() 
            self.hopethread.wait()
            #self.dibujar(x,data,sensors,plot,dac)
#            ts=datetime.datetime.now().strftime('%H:%M:%S')         
#            ws = wa.active
#            r=max_row+1
#            ws.cell(row=r, column=1).value = sensors[2]
#            ws.cell(row=r, column=2).value = sensors[0]
#            ws.cell(row=r, column=3).value = sensors[1]
#            ws.cell(row=r, column=4).value = dac[0]
#            ws.cell(row=r, column=5).value = ts
#            ws.cell(row=r, column=6).value = seg
#            wa.save("test1.xlsx")
#           


    def dibujar(self,x,dato,sensors,plot,dac):
        print("entre en dibujar")
        curve = self.graph.plot()
        temp2=sensors[0]
        print(temp2)
        temp1=sensors[2]
        print(temp1)
        press= sensors[1]
        print(press)
        value_dac= dac[0]
        print('este es el dac', value_dac)
        print('este es el dato', dato)
        self.Sensors.setValue((temp1),(temp2),(press),(value_dac))
        if plot==1:
            curve.setData(x,dato,pen=pyqtgraph.mkPen(color=QColor(253, 118, 0),style=PyQt5.QtCore.Qt.SolidLine, width=2),symbolBrush=(253, 118, 0)) 
        elif plot==0:
            curve.setData(dato[0:32764], pen=pyqtgraph.mkPen(width=2))
            self.graph.setYRange(0, 3500, padding=0)
        elif plot==2:
            curve.setData(x,dato, pen=pyqtgraph.mkPen(color=QColor(253, 118, 0),style=PyQt5.QtCore.Qt.SolidLine, width=2),symbolBrush=(253, 118, 0))
        elif plot==3:
            curve.setData(x[0:16377], dato[0:16376], stepMode=True, fillLevel=0, brush=(0, 0, 255, 80),pen=pyqtgraph.mkPen(color=QColor(253, 118, 0), width=2))
            self.graph.enableAutoRange(axis=None, enable=True, x=None, y=None)
        QApplication.processEvents()  



    def sendParameters(self):

        global sck, sy
        sy=0
        if sck==0:

            dac_value=self.Spdac.value()
            auto = self.cboxdac.currentText()
            if auto == 'Normal':
                 auto = 1
            else:
                auto = 0
            press=self.Sppr.value()
            print(press)
            temp=self.Spte.value()
            print(temp)
            self.zedboard.set_DAC(dac_value,auto,press,temp)
            edge = self.cboxedge.currentText()
            if edge == 'Falling Edge':
                 edge = 0
            if edge == 'Auto':
                edge = 2
            else:
                edge = 1
            self.zedboard.set_edge(edge)
            level = self.Splevel.value()
            self.zedboard.set_level(level)
            print(level)
            dec_fac=self.SpDF.value()
            print(dec_fac)
            self.zedboard.set_DF(dec_fac)
            threshold=self.SpTh.value()
            print('este es th',threshold)
            self.zedboard.set_thr_ts(threshold)
            delay=self.SpDelay.value()
            print(delay)
            self.zedboard.set_delay(delay)

    def Stop(self):
        global sck
        print("voy a cerrar")
        self.zedboard.stop()
        sck=0

    def closeEvent(self, event):
        global sck
        print("voy a cerrar")
        self.zedboard.cerrar()
        sck=0
        event.accept()


if __name__ == '__main__':
    appi = QApplication([])
    zed = Zedboard('169.254.1.10', 7)
    app = Application(zed)
    app.show()
    appi.exec_()


0 个答案:

没有答案