Skip to content

Instantly share code, notes, and snippets.

@pidpawel
Created February 26, 2014 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pidpawel/9227655 to your computer and use it in GitHub Desktop.
Save pidpawel/9227655 to your computer and use it in GitHub Desktop.
#!/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