Created
August 13, 2012 22:15
-
-
Save pdvyas/3344454 to your computer and use it in GitHub Desktop.
Serial value plotting
This file contains 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.pyplot as plt | |
import matplotlib.animation as animation | |
import serial | |
def list_shift(a,x): | |
del a[0] | |
a.append(x) | |
ser = serial.Serial( | |
port='/dev/ttyUSB0', | |
baudrate=57600, | |
bytesize=serial.EIGHTBITS, | |
parity=serial.PARITY_NONE, | |
stopbits=serial.STOPBITS_ONE, | |
timeout=0.25, | |
xonxoff=0, | |
rtscts=0, | |
interCharTimeout=None | |
) | |
fig = plt.figure() | |
ax1 = fig.add_subplot(111) | |
ax2 = fig.add_subplot(111) | |
ax3 = fig.add_subplot(111) | |
ax4 = fig.add_subplot(111) | |
line1, = ax1.plot(np.arange(10)) | |
line2, = ax2.plot(np.arange(10)) | |
line3, = ax3.plot(np.arange(10)) | |
line4, = ax4.plot(np.arange(10)) | |
ax1.set_ylim(0, 800) | |
ax2.set_ylim(0, 800) | |
ax3.set_ylim(0, 800) | |
ax4.set_ylim(0, 800) | |
y_data1 = range(10) | |
y_data2 = range(10) | |
y_data3 = range(10) | |
y_data4 = range(10) | |
def update(data): | |
try: | |
list_shift(y_data1,float(data[0])) | |
list_shift(y_data2,float(data[1])) | |
list_shift(y_data3,float(data[2])) | |
list_shift(y_data4,float(data[3])) | |
line1.set_ydata(y_data1) | |
line2.set_ydata(y_data2) | |
line3.set_ydata(y_data3) | |
line4.set_ydata(y_data4) | |
except Exception: | |
print "something fucked" | |
def data_gen(): | |
while True: yield [ x.strip() for x in ser.readline().split(';') ] | |
ani = animation.FuncAnimation(fig, update, data_gen, interval=250) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment