How to plot six real-time different plots

Receive 1000 messages per second from the port, translate it into the desired format plot 6 different graphs. I can't find a solution that will can plot 1000 points per second each graph. The main problem is an old computer without a video card with a small amount of RAM. Since the hardware is weak, I use 2 threads and main application, the first one reads data from the com port and stores it in the queue, the second thread reads the data from the queue and convert a total 7 global arrays, in main application I have a timer witch 100 ms update plots. I have one common X and 6 different Y. The first problem I encountered was the structure for storing data and update plots, I need to store the received data for all the time. The second problem is graphs jerking and freezing. My device connects via usb port and create virtual com port. When i connect with device i should send "start" message,in message i pass the number of messages that i want to recive. when i use app ComPort Tools i can send any number of messages and device will return all messages, but when i use pyserial library i can recive no more 14000 messages(if i ask less, it will return te exact number)

6 plots:

    self.L_R = pg.PlotWidget()
    self.F_R = pg.PlotWidget()
    self.F_L = pg.PlotWidget()
    self.aVR = pg.PlotWidget()
    self.aVL = pg.PlotWidget()
    self.aVF = pg.PlotWidget()

7 global arrays and queue

    L_R = array.array('f')
    F_R = array.array('f')
    F_L = array.array('f')
    aVR = array.array('f')
    aVL = array.array('f')
    aVF = array.array('f')
    X   = array.array('f')

    DIRTY_DATA = queue.Queue()

Read com port:

def read_port (self):
     self.serial_port.write(self.conect_message)
     time.sleep (0.3)
     response = self.serial_port.read(9).hex()
     self.parser = PARSER (self.frequency)
     self.parser.start()

        self.serial_port.write(self.start_message)

        while self.send_messages == True:

            if self.serial_port.in_waiting == 0:
                break

            else:
                response = self.serial_port.read(900)
                DIRTY_DATA.put(response)

        self.send_messages = False
        self.record = False
        self.serial_port.write(self.disconnect_message)
        response = self.serial_port.read(9).hex()
        self.signal.emit(4)

Convert and save data:

def run(self):
    LAST_TIME = ""
    while True:
        if DIRTY_DATA.empty()!=True:
            points = DIRTY_DATA.get().hex()
            for j in range(0, len(points)-36, 36):
                point = points[j:j+36]
                if point != self.serial_is_connect and point!= self.serial_is_start and point != self.serial_is_disconnect:
                    TIME =  point[2:6]
                    f_r = (int(point[8:12],16) - 511.5) / 511.5
                    f_l = (int(point[14:18],16) - 511.5) / 511.5

                    f_r += (int(point[26:30], 16) - 511.5) / 511.5
                    f_l += (int(point[32:], 16) - 511.5) / 511.5

                    f_r/=2
                    f_l/=2
                    if LAST_TIME == "":
                        LAST_TIME = TIME
                    self.x += self.step*2


                    if LAST_TIME != TIME:
                        l_r = f_r - f_l
                        avr = - (f_r + l_r) / 2
                        avl = (l_r - f_r) / 2
                        avf = f_l / 2
                    else:
                        l_r = 1
                        avr = 1
                        avl = 1
                        avf = 1

                    L_R.append(l_r)
                    F_R.append(f_r)
                    F_L.append(f_l)
                    aVR.append(avr)
                    aVL.append(avl)
                    aVF.append(avf)
                    X.append(self.x)

And ploting data:

def draw(self):
    global ITER, L_R,F_R, F_L,aVR,aVL, aVF,X

    self.L_R.plot(X, L_R, pen="k")

    self.F_R.plot(X, F_R, pen="k")

    self.F_L.plot(X, F_L, pen="k")

    self.aVR.plot(X, aVR, pen="k")

    self.aVL.plot(X, aVL, pen="k")

    self.aVF.plot(X, aVF, pen="k")
    


Read more here: https://stackoverflow.com/questions/66320993/how-to-plot-six-real-time-different-plots

Content Attribution

This content was originally published by leonid gvozdev at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: