Schupp

flammtronik_animation

Feb 22nd, 2022
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. import tkinter as tk
  2. #import datetime as dt
  3. import matplotlib
  4. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  5. from matplotlib.figure import Figure
  6. import matplotlib.pyplot as plt
  7. import matplotlib.animation as animation
  8. import serial
  9. from tkinter import *
  10. import threading
  11. import numpy as np
  12. import re
  13. import collections
  14.  
  15. c=0
  16. temp = 0
  17. ozwei= 0.0
  18. xs = collections.deque(np.zeros(240))
  19. ys = collections.deque(np.zeros(240))
  20. y2s = collections.deque(np.zeros(240))
  21. #xs = []
  22. #ys = []
  23. #y2s = []
  24. def handle_data(data):
  25.     global c
  26.     global last
  27.     global updated
  28.     global temp
  29.     global ozwei
  30.     cc=data
  31.     i=0
  32.     c=c+1
  33.     varlo.set(c)
  34.     x = cc.split(" ")
  35.  
  36.     for xx in x:
  37.         i=i+1
  38.         #print(xx)
  39.         xxx = re.sub("[^\d\.]", "", xx)
  40.         if i == 6:
  41.             #print(xxx)
  42.             ozwei=float(xxx)
  43.             xxx=xxx.rjust(4)
  44.             varo.set(xxx)
  45.             #print(xxx)
  46.      
  47.         if i == 3:
  48.             temp=int(xxx)
  49.             xxx=xxx.rjust(4)
  50.             vart.set(xxx)
  51.  
  52.         root.update_idletasks()
  53.  
  54.  
  55. def read_from_port(ser):
  56.     global connected
  57.     while not connected:
  58.         #serin = ser.read()
  59.         connected = True
  60.  
  61.         while True:
  62.            #print("test")
  63.            reading = ser.readline().decode()
  64.            handle_data(reading)
  65.  
  66. last =0
  67. countdata=0;
  68.  
  69. def animate(i, xs, ys,y2s):
  70.  
  71.     global updated
  72.     global c
  73.     global ozwei
  74.     global temp
  75.     c=c+1
  76.     # Read temperature (Celsius) from TMP102
  77.     ax1.cla()
  78.     ax2.cla()
  79.     #xs.popleft();
  80.     #xs.append(c)
  81.     ys.popleft();
  82.     ys.append(temp)
  83.     y2s.popleft();
  84.     y2s.append(ozwei)
  85.  
  86.     line1 = ax1.plot( ys, label = 'AbgasTemperatur', color = 'red')
  87.     line2 = ax2.plot( y2s, label = 'Abgas O2', color = 'blue')
  88.  
  89.     ax1.scatter(len(ys)-1,ys[-1])
  90.     ax2.scatter(len(y2s)-1,y2s[-1])
  91.     ax1.set_ylim(0, 400)
  92.     ax2.set_ylim(0, 50)
  93. connected=False
  94. root = Tk()
  95. w, h = root.winfo_screenwidth(), root.winfo_screenheight()
  96. root.geometry("%dx%d+0+0" % (w, h-200))
  97. root.attributes('-fullscreen', True)
  98. varo = StringVar()
  99. varo.set('    ')
  100. vart = StringVar()
  101. vart.set('    ')
  102. varlo = StringVar()
  103. varlo.set('    ')
  104.  
  105. ol = Label(root, textvariable=varlo)
  106. tl = Label(root, text="Abgastemp")
  107. ol.config(font=("Courier", 44))
  108. tl.config(font=("Courier", 44))
  109. ol.pack()
  110. o = Label(root, textvariable=varo,bg="lightblue")
  111. t = Label(root, textvariable=vart,bg="orangered")
  112. o.config(font=("Courier", int(round(h/4))))
  113. t.config(font=("Courier", int(round(h/4))))
  114. o.pack()
  115. t.pack()
  116.  
  117. o.place(relx=0.5, rely=0.5, anchor="nw")
  118. ol.place(x=0, y=0)
  119. t.place(relx=0.5, rely=0.5, anchor="sw")
  120. # Create figure for plotting
  121. fig = plt.figure()
  122. ax = fig.add_subplot(1, 1, 1)
  123. fig, ax1 = plt.subplots(figsize = (6, 4))
  124. ax2 = ax1.twinx()
  125.  
  126. line1 = ax1.plot(xs, ys, label = 'Abgas Temperatur', color = 'red')
  127. line2 = ax2.plot(xs, y2s, label = 'Abgas O2', color = 'blue')
  128.  
  129. lines = line1 + line2
  130. labels = [line.get_label() for line in lines]
  131. ax1.legend(lines, labels, loc = 'upper right')
  132.  
  133. ax1.set_ylim(0, 400)
  134. ax2.set_ylim(0, 50)
  135.  
  136. ax1.tick_params(axis = 'x', which = 'both', top = False)
  137. ax1.tick_params(axis = 'y', which = 'both', right = False, colors = 'red')
  138. ax2.tick_params(axis = 'y', which = 'both', right = True, labelright = True, left = False, labelleft = False, colors = 'blue')
  139.  
  140. plt.setp(ax1.xaxis.get_majorticklabels(), rotation = 45)
  141.  
  142. ax1.set_xlabel('Date')
  143. ax1.set_ylabel('Abgas Temperatur')
  144. ax2.set_ylabel('Abgas O2')
  145.  
  146. ax1.yaxis.label.set_color('red')
  147. ax2.yaxis.label.set_color('blue')
  148.  
  149. ax2.spines['left'].set_color('red')
  150. ax2.spines['right'].set_color('blue')
  151.  
  152. plt.tight_layout()
  153.  
  154.  
  155. line2 = FigureCanvasTkAgg(fig, root)
  156. line2.get_tk_widget().pack(side=tk.LEFT, fill=tk.BOTH)
  157. try:
  158.     ser = serial.Serial("COM9", 38400)
  159. except:
  160.     try:
  161.         ser = serial.Serial("/dev/ttyUSB0", 38400)
  162.     except:ser = serial.Serial("/dev/ttyACM0", 38400)
  163.  
  164. thread = threading.Thread(target=read_from_port, args=(ser,))
  165. thread.start()
  166. ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys,y2s), interval=500)
  167. plt.draw()
  168. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment