Plot Not Updating Qt Gui And Pyqtgraph Python
Solution 1:
It is recommended not to modify the code provided by Qt Designer since if you want to modify the design you will have to rewrite your code, besides the Ui_MainWindow class is not a widget but a class that is used to fill some widget, so I recommend deleting the changes .
On the other hand is not suitable to run with a timer with a range of 0, since it does not allow updating the GUI, on the other hand the reading of the serial is blocking, so a possible solution is to use QThread, and use a signal to update the data in the GUI thread.
classUi_MainWindow(object):
    ...
classArduinoThread(QtCore.QThread):
    dataChanged = QtCore.pyqtSignal(str)
    def__init__(self, *args, **kwargs):
        QtCore.QThread.__init__(self, *args, **kwargs)
        self.raw = serial.Serial('com4', 9600)
        self.raw.close()
        self.raw.open()
    defrun(self):
        whileTrue:
            datos_array = self.raw.readline().decode().split(',')
            if datos_array:
                datos = datos_array[0]
            self.dataChanged.emit(datos)
            QtCore.QThread.msleep(10)
classMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def__init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.BotonInicio.clicked.connect(self.start)
        self.m = 0defupdate_plot(self, dato):
        if self.m > 99:
            self.y1 = np.zeros(100, dtype=float)
            self.m = 0else:
            self.y1[self.m] = dato
            self.m += 1
        self.curva1.setData(self.y1)
    defstart(self):
        thread = ArduinoThread(self)
        thread.dataChanged.connect(self.update_plot)
        thread.start()
        self.win = pg.GraphicsWindow()
        self.win.setWindowTitle('Datos de arduino')
        self.p1 = self.win.addPlot()
        self.p1.setYRange(0, 1024, padding=0)
        self.curva1 = self.p1.plot()
        self.y1 = np.zeros(100, dtype=float)
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
As you can see, I have made some changes to improve the application as verifications, use variables when necessary, etc.
Although a way compatible with Qt is to use QSerialPort that emits the readyRead signal when there is a new data:
from PyQt5 import QtCore, QtGui, QtWidgets, QtSerialPort
....
classMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def__init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.BotonInicio.clicked.connect(self.start)
    defupdate_plot(self):
        while self.ser.canReadLine():
            line = bytearray(self.ser.readLine()).decode()
            self.processLine(line)
    defprocessLine(self, line):
        datos_array = line.strip().split(',')
        if datos_array:
            dato = datos_array[0]
            if self.m > 99:
                self.y1 = np.zeros(100, dtype=float)
                self.m = 0else:
                self.y1[self.m] = dato
                self.m += 1
            self.curva1.setData(self.y1)
    defstart(self):
        self.ser = QtSerialPort.QSerialPort("com4", self)
        self.ser.setBaudRate(9600)
        self.ser.readyRead.connect(self.update_plot)
        self.ser.open(QtSerialPort.QSerialPort.ReadOnly)
        self.win = pg.GraphicsWindow()
        self.win.setWindowTitle('Datos de arduino')
        self.p1 = self.win.addPlot()
        self.p1.setYRange(0, 1024, padding=0)
        self.curva1 = self.p1.plot()
        self.y1 = np.zeros(100, dtype=float)
if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
Post a Comment for "Plot Not Updating Qt Gui And Pyqtgraph Python"