Files
hpos-data/scripts/auto_check_led.py
2024-01-07 23:06:14 +05:30

100 lines
3.3 KiB
Python

import serial
import pandas as pd
import os.path
from datetime import datetime
import math
from tqdm import tqdm
import re
import sys
class Device:
def __init__(self, accuracy, precision, port, baud_rate):
self.accuracy = accuracy
self.precision = precision
self.port = port
self.baud_rate = 9600
def perform_hpos_test(repeat_no, solution, df):
try:
port = sys.argv[1]
baud_rate = 9600
deviceId = 'HCV-000-3004'
data = ""
data_list = []
if not os.path.exists(port):
raise FileNotFoundError(f"Port {port} not found.")
with serial.Serial(port, baud_rate, timeout=1, parity=serial.PARITY_NONE) as ser:
ser.write(b'B')
while True:
data += ser.readline().decode("utf-8")
# print(data.split('\r'))
# if 'SNE' in data:
# ser.write(b'B')
if 'BC' in data:
ser.write(b'P')
if 'REND' in data:
# print(data)
pattern = re.compile(r'(LB[1-4])\s+([\d.]+)')
matches = pattern.findall(data)
if matches:
lb1_value = matches[0][1]
lb2_value = matches[1][1]
lb3_value = matches[2][1]
lb4_value = matches[3][1]
# print("LB1:", lb1_value)
# print("LB2:", lb2_value)
# print("LB3:", lb3_value)
# print("LB4:", lb4_value)
else:
print("Pattern not found.")
data_list.append({"sol": solution + '-' + str(repeat_no),
"led1Buffer": lb1_value,
"led2Buffer": lb2_value,
"led3Buffer": lb3_value,
"led4Buffer": lb4_value,
"deviceId": deviceId,
"time": datetime.today().strftime('%d-%m-%y %H:%M:%S')})
break
return data_list
except serial.serialutil.SerialException:
print("Device is not connected or resource busy!")
raise
except FileNotFoundError as e:
print(e)
raise
except Exception as err:
print("Error:", err)
raise
finally:
if ser is not None:
ser.close()
if __name__ == "__main__":
data_file = 'data.xlsx'
if os.path.exists(data_file):
df = pd.read_excel(data_file)
else:
df = pd.DataFrame(columns=["sol", "led1Buffer", "led2Buffer", "led3Buffer", "led4Buffer", "deviceId", "time"])
# device = Device(0.01, 0.001, '/dev/cu.usbserial-23APB124', 9600)
solution = "buffer" #input("solution name: ")
repeats = 100 #int(input("number of repeats: "))
for repeat in tqdm(range(0, repeats)):
data_list = perform_hpos_test(repeat, solution, df)
df = pd.concat([df, pd.DataFrame(data_list)], ignore_index=True)
writer = pd.ExcelWriter(data_file, engine = 'openpyxl')
df.to_excel(writer, sheet_name = 'data', index=False)
writer.close()