The task is as follows: it is necessary to write a program that will read the values from the com-port and build on them a graph in real time. Somehow implemented, BUT it turns out a delay of at least 3-4 seconds.
I came to the conclusion that I do not have time to take values from the buffer. You can draw a graph in several streams, but since I'm not strong in python. Addressed to you.
Please provide examples of multi-threaded drawing graphics with a detailed description.
import matplotlib.pyplot as plt plt.ion() class SerialRead(): com = 0 def __call__(self): import serial self.com = serial.Serial('/dev/ttyACM0', 1200) def readValue(self): #import time #time.sleep(0.2) #self.com.flushInput() ERR = False try: line = self.com.readline() except: ERR = True print("ERR::Ресурс временно недоступен!") if ERR != True: try: value = float(line) except: print("ERR::Wrong value at serial port!") ERR = True if ERR != True: return value c = SerialRead() c() class DynamicUpdate(): #Suppose we know the x range min_y = 0 max_y = 5.1 def on_launch(self): import numpy as np #Set up plot plt.rcParams['toolbar'] = 'None' self.figure, self.ax = plt.subplots() self.lines, = self.ax.plot([],[], 'r') #Autoscale on unknown axis and known lims on the other #self.ax.set_autoscaley_on(True) #self.ax.set_xlim(self.min_x, self.max_x) self.ax.set_autoscaley_on(True) self.ax.set_ylim(self.min_y, self.max_y) #Other stuff self.ax.grid() ... def on_running(self, xdata, ydata): import numpy as np #Update data (with the new _and_ the old points) self.lines.set_xdata(xdata) self.lines.set_ydata(ydata) #Need both of these in order to rescale self.ax.relim() self.ax.autoscale_view() #We need to draw *and* flush self.figure.canvas.draw() self.figure.canvas.flush_events() ######################################### x = max(xdata) if x > 10000: self.ax.set_xlim(x, x - 10000) else: self.ax.set_xlim(0, x) ######################################### #Example def __call__(self): import numpy as np import time import matplotlib.ticker as ticker import random self.on_launch() xdata = [] ydata = [] while True: OLD_VALUE = c.readValue() xdata.append(int(round(time.time() * 1000))) ydata.append(OLD_VALUE) self.on_running(xdata, ydata) #time.sleep(1) return xdata, ydata d = DynamicUpdate() d()
on_running
on eachon_running
piece of data? (Operations likeautoscale_view
orflush_events
do not look very cheap from the name.) - VladD