#!/usr/bin/env python3 import matplotlib.pyplot as plt import numpy as np import serial import sys,csv,time #initialize serial port ser = serial.Serial() ser.port = "COM3" # ser.port = '/dev/ttyS2' #Arduino serial port ser.baudrate = 115200 ser.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 plotting fig = plt.figure() ax = fig.add_subplot(1, 1, 1) xs = [] # time ys = [] # shoult temp rs = [] # is temp ''' plot_window = 60 x_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 = 0 while 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 '''