import serial import pandas as pd import os.path from datetime import datetime import math import time from tqdm import tqdm class Device: def __init__(self, accuracy, precision): self.accuracy = accuracy self.precision = precision def perform_hpos_test(repeat_no): try: data_file = 'data.xlsx' if os.path.exists(data_file): df = pd.read_excel('data.xlsx') else: df = pd.DataFrame(columns=["sol", "427_buffer_intensity", "555_buffer_intensity", "427_sample_intensity", "555_sample_intensity", "427_absorbance", "555_absorbance", "absorbance_ratio", "time"]) time.sleep(5) with serial.Serial('/dev/cu.usbserial-1420', 115200, timeout=1, parity=serial.PARITY_NONE) as ser: x = ser.read() s = ser.read(10) line = ser.readline() print(x) print(s) print(line) # perform_blanking = input("Do you want to perform blanking?(Y/N)") ser.write(b'B') while True: x = ser.read() b = ser.read(100) line = ser.readline() print(b) if 'Buffer Completed' in b.decode("utf-8"): ser.write(b'S') while True: x = ser.read() s = ser.read(100) line = ser.readline() print(s) if 'Sample Completed' in s.decode("utf-8"): ser.write(b'P') while True: x = ser.read() r = ser.read(100) line = ser.readline() print(r) if 'RESULT' in r.decode("utf-8") or 'REND' in r.decode('utf-8'): result = r.decode("utf-8").split(' ') df = pd.concat([df, pd.DataFrame([{"sol": solution + '-' + str(repeat_no), "427_buffer_intensity": result[3], "555_buffer_intensity": result[3], "427_sample_intensity": result[5], "555_sample_intensity": result[6], "427_absorbance": math.log10(float(result[3]) / float(result[5])), "555_absorbance": math.log10(float(result[4]) / float(result[6])), "absorbance_ratio": math.log10(float(result[3]) / float(result[5])) / math.log10(float(result[4]) / float(result[6])), "time": datetime.today().strftime('%d-%m-%y %H:%M:%S')}])], ignore_index = True) print(df) writer = pd.ExcelWriter(data_file, engine = 'openpyxl') df.to_excel(writer, sheet_name = 'data', index=False) writer.close() break break break except serial.serialutil.SerialException: print("device is not connected!") exit() except Exception as err: print("error!", err) exit() if __name__ == "__main__": d = Device(0.01, 0.001) solution = input("solution name: ") repeats = int(input("number of repeats: ")) for repeat in tqdm(range(0, repeats)): perform_hpos_test(repeat)