From 42994a7dd7f7721f5e58273ee02bca892a402a9b Mon Sep 17 00:00:00 2001 From: Ryan Xu Date: Thu, 15 Dec 2022 15:22:33 -0800 Subject: [PATCH] Add headers to recorded data --- read_arduino.py | 8 ++++++++ serial_plotter.py | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/read_arduino.py b/read_arduino.py index 207e280..3c000b7 100644 --- a/read_arduino.py +++ b/read_arduino.py @@ -5,6 +5,7 @@ from datetime import datetime import serial, time, json import serial.tools.list_ports import numpy as np +import csv # constant settings SENSORS_MAX = 4 # maximum sensor ports @@ -27,6 +28,13 @@ def read(): raise ValueError("Port range is 0-3!") if "- No Device -" in port: # generate random value + + # open the file and add a line of header to it, then close + f = open(file_name, "a", newline="", encoding="utf-8") + writer = csv.writer(f) + header = ['Time', 'Resistance'] + writer.writerow(header) + f.close() while True: dat_list = np.random.randint(0, v_in * 1000, SENSORS_MAX) # create a randomized voltage data # take only the nonzero indices, and truncated to two decimal places to "filter" out some hardware errors diff --git a/serial_plotter.py b/serial_plotter.py index 33f4ae8..d6be973 100644 --- a/serial_plotter.py +++ b/serial_plotter.py @@ -5,6 +5,7 @@ import numpy as np import matplotlib import matplotlib.pyplot as plt import decimal +from itertools import islice matplotlib.use("WXAgg") # for JetBrains IDE to force use wxPython as backend UI for plotting @@ -55,7 +56,7 @@ class SerialPlotter: plt.cla() # clear previous frame # read the last line from the .csv file, the data start from the second column so omit index #0 file = open(self.settings["file_name"], "r") - ndata = np.array([np.asarray(line.split(", ")[1:], dtype=np.float32) for line in file]) + ndata = np.array([np.asarray(line.split(", ")[1:], dtype=np.float32) for line in islice(file, 1, None)])#read from the second row of the file ignoring headers if len(ndata) > 0: row = ndata[-1] i = 0