| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 | #!/usr/bin/env python3import matplotlib.pyplot as pltimport numpy as npimport serialimport sys,csv,time#initialize serial portser = serial.Serial()ser.port = "COM3"# ser.port = '/dev/ttyS2' #Arduino serial portser.baudrate = 115200ser.timeout = 10 #specify timeout when using readline()ser.open()ser.flushInput()if ser.is_open==True:	print("\nAll right, serial port now open. Configuration:\n")	print(ser, "\n") #print serial parameters'''# Create figure for plottingfig = plt.figure()ax = fig.add_subplot(1, 1, 1)xs = [] # time ys = [] # shoult temprs = [] # is temp'''plot_window = 60x_var = np.array(np.zeros([plot_window]))y1_var = np.array(np.zeros([plot_window]))y2_var = np.array(np.zeros([plot_window]))plt.ion()fig, ax = plt.subplots()# plt.axis([None, None, 0, 120])line1, = ax.plot(x_var, y1_var)line2, = ax.plot(x_var, y2_var)count = 0while True:    while ser.inWaiting()==0:        plt.pause(.1)    line=ser.readline()    try:        count = count + 1        if len(line) < 3:            continue                line = line.decode()        print(line.rstrip("\n"))        line_as_list = line.split(',')        if len(line_as_list) < 3:            continue        i = int(line_as_list[0].strip())        should_float = float(line_as_list[1].strip())        is_float = float(line_as_list[2].strip())                if len(sys.argv) > 1:            with open(sys.argv[1],"a") as f:                writer = csv.writer(f,delimiter=",")                writer.writerow([i, should_float, is_float])        x_var = np.append(x_var, i)        x_var = x_var[1:plot_window+1]        y1_var = np.append(y1_var, should_float)        y1_var = y1_var[1:plot_window+1]        y2_var = np.append(y2_var, is_float)        y2_var = y2_var[1:plot_window+1]        # print(x_var)        # print(y1_var)        # print(y2_var)        line1.set_xdata(x_var[-count:])        line2.set_xdata(x_var[-count:])        line1.set_ydata(y1_var[-count:])        line2.set_ydata(y2_var[-count:])        ax.relim()        ax.autoscale_view()        fig.canvas.draw()        fig.canvas.flush_events()    except Exception as ex:        print(ex)        continue    '''    # Draw x and y lists    ax.clear()    ax.plot(xs, ys, label="Temp Dial")    ax.plot(xs, rs, label="Temp Sensor")    # Format plot    plt.xticks(rotation=45, ha='right')    plt.subplots_adjust(bottom=0.30)    plt.title('This is how I roll...')    plt.ylabel('Relative frequency')    plt.legend()    plt.axis([None, None, 0, 120])    #plt.axis([1, 100, 0, 1.1]) #Use for 100 trial demo'''
 |