From 9cf553b582dee19b2042905a1b23307ab1f45512 Mon Sep 17 00:00:00 2001 From: udara vimarsha Date: Sun, 29 Sep 2024 13:49:45 +0530 Subject: [PATCH 1/3] Initial commit Completed Serial Read and Data Extractor for parameters and plot data --- DSO138mini_data.py | 103 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 DSO138mini_data.py diff --git a/DSO138mini_data.py b/DSO138mini_data.py new file mode 100644 index 0000000..20b9e66 --- /dev/null +++ b/DSO138mini_data.py @@ -0,0 +1,103 @@ +import serial +import sys +import json +import numpy as np +import matplotlib.pyplot as plt + +''' + Made for DSO 138 mini oscilloscope. Parameters and Plot data will saved to two separate JSON files. + return oci_paramerters and plot_data dictionary and a list respectively. + 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]") + 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.") + sys.exit() + +COM = sys.argv[1] if sys.argv[1].startswith("COM") else "COM" + sys.argv[1] +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") + + +class DSO138mini: + def __init__(self, COM, BAUD, FILENAME="data.txt"): + self.COM = COM + self.BAUD = BAUD + self.FILENAME = FILENAME + + def connect(self): + self.ser = serial.Serial(port=self.COM, baudrate=self.BAUD, timeout=2) + + def save_data(self): + count = 0 + oci_para = {} + plot_data = [] + try: + while True: + if (self.ser.in_waiting): + data = self.ser.readline().decode("utf-8").strip().split(",") + if (count < 19): + oci_para[data[0].strip()] = data[1].strip() + count += 1 + else: + plot_data.append( + [int(data[0]), int(data[1]), float(data[2])]) + count += 1 + + if (count == 1043): + 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() + break + print(data, flush=True) + else: + print("waiting for data...", end="\r", flush=True) + 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) + sys.exit() + + return oci_para, plot_data + + def plot_data(self, plot_data, oci_para): + plot_data = np.array(plot_data) + plt.plot(plot_data[:, 1], plot_data[:, 2]) + plt.xlabel('Time (ms)') + plt.ylabel('Voltage (V)') + plt.title("DSO138 mini") + x_plt = 5.5 + y_plt = 5.5 + for key, value in oci_para.items(): + plt.text(x_plt, y_plt, key + ": " + value, fontsize=12, + verticalalignment='top', horizontalalignment='left') + y_plt -= 0.5 + + plt.grid() + plt.show() + + +def main(): + dso = DSO138mini(COM, BAUD, filename) + dso.connect() + oci_para, plot_data = dso.save_data() + print("Data saved to " + filename) + # dso.plot_data(plot_data, oci_para) + return oci_para, plot_data + + +if __name__ == "__main__": + main() From 491f4589394f6c8f6e36e7ff81f21463f32cb54e Mon Sep 17 00:00:00 2001 From: udara vimarsha Date: Sun, 29 Sep 2024 14:50:10 +0530 Subject: [PATCH 2/3] Added support for Linux and Better Support --- DSO138mini_data.py | 64 +++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 18 deletions(-) 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 From 66c95526c18da6331d6cc0f4dc209e79f60662d8 Mon Sep 17 00:00:00 2001 From: Udara Vimarsha Date: Sun, 29 Sep 2024 15:12:57 +0530 Subject: [PATCH 3/3] Update README.md Updated Instruction set --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b4fad0..1e19888 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,20 @@ # DSO138_mini_Data_Extractor -Made for #DSO 138 mini oscilloscope#. Parameters and Plot data will saved to two separate JSON files. return oci_paramerters and plot_data dictionary and a list respectively. +Made for ```DSO 138 mini oscilloscope```. Parameters and Plot data will saved in two separate JSON files named ```_param.json & _data.json```. return oci_paramerters and plot_data dictionary and a list respectively. + +## Requirements +* UART Adapter and 3 jumper wires for Connection +* Windows or Linux PC + +## Setup +* Power up the device first. +* connect to the Serial Connection +* ```(PC) TXD -> RXD (HOST)```, +* ```(PC) RXD -> TXD (HOST)```, +* ```(PC) GND -> GND (HOST)``` + +## Run the Script in your work folder +* Usage: ```python DSO138mini.py -h ``` or ``` python DSO138mini.py --help ``` for help +* Usage: ```python DSO138mini.py ``` +* Example: ``` python DSO138mini.py COM9 115200 square_wave``` +* You will prompt that ```Waiting for data``` if the connection is success +* Just hold down the SEL button for 3s.