Code: Alles auswählen
# coding: utf-8
#-----z.T. verwendete Module-----------------
import cb
import time
import struct
import sys
import console
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
console.set_color()
#----- Programm für das Lesen von 'line' und Darstellen der Kurven und derer Animation
style.use('fivethirtyeight')
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
tz =''
zeile = ''
line = ''
def animate(i):
x = 0
sp = 0
global line
#global line #line =''
titel =''
tim = []
tc0 = []
tc1 = []
tx0 = []
tx1 = []
sol = []
ist = []
hot = []
fan = []
ovn = []
reflow = []
# Bluetooth-Verbindung herstellen und zeilenweise Log-Daten lesen (von hier)
mngr = MLT_BT05_Manager()
cb.set_central_delegate(mngr)
print('Scanning for peripherals...')
cb.scan_for_peripherals()
# bis hier als ersatz von lines = open('file','r') .read() und for line in lines:....
if line != []:
print('ANI=',line) #-Test-Ausgabe, ob eine line übergeben wurde
# ab hier die Interpretation des line-Inhaltes zum Aufbau der Grafik
if len(line) == 39:
titel = line
sp = 217
if len(line) == 48:
titel = line
sp = 217
if len(line) == 47:
titel = line
sp = 136
if 'AMTECH' in line: sp = 217
if len(line) == 44:
titel = line
sp = 183
if len(line) >= 75: # Log-Daten, Zeit, Temperaturen usw.
x = x + 1
zeile = line[0:67]
if len(zeile) > 1:
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 = zeile.split(',')
tim.append(float(s1))
tc0.append(float(s2))
tc1.append(float(s3))
tx0.append(float(s4))
tx1.append(float(s5))
sol.append(float(s6))
ist.append(float(s7))
hot.append(float(s8))
fan.append(float(s9))
ovn.append(float(s10))
reflow.append(float(sp))
ax1.clear()
ax1.plot(tim, hot, 'r', linewidth=0.1, label='Heizung/B/')
ax1.plot(tim, fan, 'b', linewidth=0.1, label='Ventilator/B/')
ax1.plot(tim, ovn, linewidth=0.5, label='Ofen-Temp.')
ax1.plot(tim, tc0, linewidth=2.5, label='TC0-links')
ax1.plot(tim, tc1, linewidth=2.5, label='TC1-rechts')
ax1.plot(tim, sol, linewidth=2.5, label='Soll-Temp.')
ax1.plot(tim, ist, linewidth=2.5, label='Ist-Temp.')
ax1.plot(tim, tx0, linewidth=2.5, label='ExTC2')
ax1.plot(tim, tx1, linewidth=2.5, label='ExTC3')
ax1.plot(tim, reflow, linewidth=1.5, label='Schmelzpunkt')
plt.xlabel('Zeit [s]', fontsize=10) # Bezeichnung der X-Achse
plt.ylabel('Temperatur [°C]', fontsize=10) # Bezeichnung der Y-Achse
plt.title(titel, fontsize=12) # Plot-Titel
plt.legend(loc=2, prop={'size': 8}) # Legende einblenden
# plt.xlim(0,380)
# plt.ylim(0,250)
plt.xticks([0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360],fontsize=10)#,[0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360])
plt.yticks([0,25,50,75,100,125,150,175,200,225,250],fontsize=10)#,[0,25,50,75,100,125,150,175,200,225,250])
#-----Ende von def animate()-------
#ani = animation.FuncAnimation(fig, animate, interval=5000)
#plt.show()
tz = ''
line = ''
#---Programm zur Bluetooth-Verbindung und zeilenweise lesen
class MLT_BT05_Manager (object):
def __init__(self):
self.peripheral = None
print('\n Init')
def did_discover_peripheral(self, p):
if p.name == 'MLT-BT05' and not self.peripheral:
print('Discovered ' + p.name)
self.peripheral = p
cb.connect_peripheral(p)
def did_connect_peripheral(self, p):
print('Connected Peripheral ' + p.name)
p.discover_services()
def did_fail_to_connect_peripheral(self, p, error):
print('Failed to connect: %s' % (error,))
def did_disconnect_peripheral(self, p, error):
global line
print('*Disconnected, error: %s' % (error,))
self.peripheral = None
def did_discover_services(self, p, error):
global line
for s in p.services:
if s.uuid =='FFE0':
p.discover_characteristics(s)
def did_discover_characteristics(self, s, error):
global line
for c in s.characteristics:
if c.uuid == 'FFE1':
self.peripheral.set_notify_value(c, True)
def did_update_value(self, c, error):
global tz
global zeile
global line
input = (c.value) # <class 'bytes'>
lt1 = input.decode('ascii')
if '\r\n' not in lt1 or len(lt1) <3: # Teilzeilen zusammensetzen! Neue Zeile?
tz += lt1
else:
zeile = tz[2:]
tz = lt1
print('BT',zeile) # Test-Ausgabe, ob Bluetooth-Zeile gelesen wurde
line = zeile
#return line
#return line
'''
#----Ursprüngliche Aufruf-Zeilen des obigen Bluetooth-Moduls
mngr = MLT_BT05_Manager()
cb.set_central_delegate(mngr)
print('Scanning for peripherals...')
cb.scan_for_peripherals()
try:
while True: pass
except KeyboardInterrupt:
cb.reset()
'''
#----------
ani = animation.FuncAnimation(fig, animate, interval=5000)
plt.show()
..
Ich habe in Programmteil Bluetooth zu viele sinn- und wirkungslose 'global line' in die einzelnen Funktionen eingeführt und dabei vergessen, sie in dieser Ausgabe zu löschen. Also bitte ignorieren!