Created
April 4, 2019 07:47
-
-
Save leopard627/bef5edc0462bdf4c1079d0d6e59d6a2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib as mpl | |
import matplotlib.pyplot as plt | |
import matplotlib.animation as animation | |
import serial | |
import platform | |
import time | |
import datetime | |
import csv | |
import re | |
print("Python version: " + platform.python_version()) | |
print("matplotlib version: " + mpl.__version__) | |
fig, ax = plt.subplots() | |
line, = ax.plot(np.random.rand(10)) | |
ax.set_ylim(-.1, .08) | |
xdata, ydata = [0]*100, [0]*100 | |
SerialIn = serial.Serial("com4",115200) | |
FILE = open('Pressure ' + time.strftime("%Y-%m-%d %H:%M") + '.csv','a') | |
pattern = re.compile( r'([0-9 .+-]*)' ) #Added | |
def update(data): | |
line.set_ydata(data) | |
return line, | |
def run(data): | |
global xdata, ydata | |
x,y = data | |
if (x == 0): | |
xdata = [0]*100 | |
ydata = [0]*100 | |
del xdata[0] | |
del ydata[0] | |
xdata.append(x) | |
ydata.append(y) | |
line.set_data(xdata, ydata) | |
return line, | |
def data_gen(): | |
x = 10 | |
while True: | |
if (x >= 9): | |
x = 0 | |
else: | |
x += 0.1 | |
SerialIn.write(b"P\r") | |
try: | |
inRaw = re.search( pattern, | |
SerialIn.read(SerialIn.in_waiting).decode('utf-8') ).group(0) | |
inInt = float(inRaw) | |
TIME = time.strftime('%H:%M:%S') | |
FILE.write("{0},{1}\r".format(TIME, inInt)) | |
FILE.flush() | |
print('['+time.strftime('%H:%M:%S')+']',inInt) | |
time.sleep(1-time.time()%1) | |
except: | |
inInt = 0 | |
yield x, inInt | |
ani = animation.FuncAnimation(fig, run, data_gen, interval=0, blit=True) | |
plt.show() | |
SerialIn.close() | |
FILE.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment