Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python2
import pylab as plt
import collections
import random
import time
import serial
maxsamples = 1000
Y1 = collections.deque([0] * maxsamples)
Y2 = collections.deque([0] * maxsamples)
Y3 = collections.deque([0] * maxsamples)
plt.ion()
plt.figure(figsize=(20,10))
graph1 = plt.plot(Y1)[0]
graph2 = plt.plot(Y2)[0]
graph3 = plt.plot(Y3)[0]
ser = serial.Serial('/dev/ttyUSB0', 19200, bytesize=8, parity='N', stopbits=1, timeout=None)
old = bytearray()
while True:
try:
new = ser.read(21)
except serial.serialutil.SerialException:
time.sleep(0.1)
continue
old += new
if '\n' in old:
(line, old) = old.split('\n', 1)
else:
continue
print line
parts = line.split(' ')
if len(parts) != 4:
continue
try:
y1 = float(parts[0])/10.0
y2 = float(parts[2])/100.0
y3 = float(parts[3])/100.0
except ValueError:
continue
Y1.append(y1)
Y1.popleft()
Y2.append(y2)
Y2.popleft()
Y3.append(y3)
Y3.popleft()
graph1.set_ydata(Y1)
graph2.set_ydata(Y2)
graph3.set_ydata(Y3)
plt.draw()
plt.axis([0,maxsamples, 15,100])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.