diff --git a/DSO138mini_data.py b/DSO138mini_data.py index 20b9e66..af23dbb 100644 --- a/DSO138mini_data.py +++ b/DSO138mini_data.py @@ -10,35 +10,58 @@ TODO: Format plot with parameters. ''' -if (len(sys.argv) == 1 or sys.argv[1] == "-h" or sys.argv[1] == "--help"): - print("Usage: python DSO138mini.py [COM port] [baud rate] [output file]") +if len(sys.argv) != 2 and len(sys.argv) != 4: + print("Invalid number of arguments. Use -h or --help for more information.") + sys.exit() +# setting up the arguments +if (sys.argv[1] == "-h" or sys.argv[1] == "--help"): + print( + "Usage: python DSO138mini.py [COM port] [baudrate (default=15200)] [output file]") print("Example: python DSO138mini.py COM3 115200 data.txt") sys.exit() -if len(sys.argv) != 4: - print("Invalid number of arguments. Use -h or --help for more information.") +if sys.platform == "linux" or sys.platform == "linux2": + COM = "/dev/tty" + sys.argv[1] +elif sys.platform == "win32": + COM = sys.argv[1] if sys.argv[1].startswith("COM") else "COM" + sys.argv[1] +else: + print("Unsupported platform") sys.exit() -COM = sys.argv[1] if sys.argv[1].startswith("COM") else "COM" + sys.argv[1] +# setting up the parameters BAUD = int(sys.argv[2]) if sys.argv[2].isnumeric() else 115200 -filename = sys.argv[3] if sys.argv[3].endswith( - ".txt") else sys.argv[3] + ".txt" -save_file_param = open(filename+"_param.json", "w") -save_file_data = open(filename+"_data.json", "w") +FILENAME = sys.argv[3] class DSO138mini: - def __init__(self, COM, BAUD, FILENAME="data.txt"): + def __init__(self, COM, BAUD, FILENAME="DSO138"): self.COM = COM self.BAUD = BAUD self.FILENAME = FILENAME def connect(self): - self.ser = serial.Serial(port=self.COM, baudrate=self.BAUD, timeout=2) + + # initialize serial connection + try: + self.ser = serial.Serial( + port=self.COM, baudrate=self.BAUD, timeout=1) + except serial.SerialException: + print(f"Faild to open {COM} . Exiting...") + sys.exit() + except KeyboardInterrupt: + print("Interrupted by User, Exiting...") + sys.exit() + + if (self.ser.is_open): + print("Connected to " + self.COM) def save_data(self): - count = 0 - oci_para = {} + # open files + save_file_param = open(self.FILENAME + "_param.json", "w") + save_file_data = open(self.FILENAME + "_data.json", "w") + count = 0 # counter for lines DSO138 sends 1043 lines of data + oci_para = {} # firtst 19 lines are parameters + # rest of the data is plot data (1023 lines) 3 columns (seq, time, voltage) plot_data = [] try: while True: @@ -61,18 +84,23 @@ def save_data(self): break print(data, flush=True) else: - print("waiting for data...", end="\r", flush=True) + print("waiting for data...", end="\r") + except KeyboardInterrupt: self.ser.close() json.dump(oci_para, save_file_param) json.dump(plot_data, save_file_data) save_file_data.close() save_file_param.close() - print("Data saved to " + filename) + print("Data saved to " + self.FILENAME) sys.exit() - + except serial.SerialException: + print("Serial connection lost. Exiting...") + sys.exit() + return oci_para, plot_data + # TODO: Format plot with parameters. def plot_data(self, plot_data, oci_para): plot_data = np.array(plot_data) plt.plot(plot_data[:, 1], plot_data[:, 2]) @@ -91,10 +119,10 @@ def plot_data(self, plot_data, oci_para): def main(): - dso = DSO138mini(COM, BAUD, filename) + dso = DSO138mini(COM, BAUD, FILENAME) dso.connect() oci_para, plot_data = dso.save_data() - print("Data saved to " + filename) + print("Data saved to " + FILENAME) # dso.plot_data(plot_data, oci_para) return oci_para, plot_data