Schupp

Flammtronik-ani2

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