In [242]:
%matplotlib notebook
import serial
import numpy as np
import sys
from matplotlib import pyplot as plt

In [243]:
ser = serial.Serial('/dev/ttyUSB0', 115200 ,timeout=1)

In [244]:
plt.ion() 
fig1 = plt.figure()
start_time = 0
timepoints = []
ydata = []
yrange = [-4.0,4.0]
view_time = 10 # seconds of data to view at once
duration = 24000 # total seconds to collect data
#fig1 = plt.figure()
#fig1.suptitle('live updated data', fontsize='18', fontweight='bold')
plt.xlabel('time, seconds', fontsize='14', fontstyle='italic')
plt.ylabel('potential, volts', fontsize='14', fontstyle='italic')
plt.axes().grid(True)
line1, = plt.plot(ydata,marker='o',markersize=2,markerfacecolor='red')
plt.ylim(yrange)
plt.xlim([0,view_time])
line1.set_xdata(timepoints)
line1.set_ydata(ydata)



In [245]:
ser.flushInput()
with open('radio_sun.log', 'a') as sun:
    while True:
        ser.reset_input_buffer()
        data = ser.readline().rstrip()
        sample = str(data,'utf-8') 
        #print(sample)
        inputs = sample.split()
        if len(inputs) == 2:
            t = float(inputs[0])
            v = float(inputs[1])
            #print(t,v)
            ydata.append(v)
            timepoints.append(t)
            #print(ydata,timepoints)
            current_time = t
            #line1, = plt.plot(ydata,marker='o',markersize=4,markerfacecolor='red')
            line1.set_xdata(timepoints)
            line1.set_ydata(ydata)
            # slide the viewing frame along
            if current_time > view_time:
                plt.xlim([current_time-view_time,current_time])
            fig1.canvas.draw()
            sun.write(sample+'\n')
            sun.flush()


--------------------------------------------------------
error                  Traceback (most recent call last)
<ipython-input-245-2a4ed72fee84> in <module>()
      2 with open('radio_sun.log', 'a') as sun:
      3     while True:
----> 4         ser.reset_input_buffer()
      5         data = ser.readline().rstrip()
      6         sample = str(data,'utf-8')

/usr/local/lib/python3.4/dist-packages/serial/serialposix.py in reset_input_buffer(self)
    593         if not self.is_open:
    594             raise portNotOpenError
--> 595         termios.tcflush(self.fd, termios.TCIFLUSH)
    596 
    597     def reset_output_buffer(self):

error: (5, 'Input/output error')

In [ ]: