Files
TIMERS/old/timer_megasport.py

83 lines
2.1 KiB
Python

# import serial.tools.list_ports #pip install pyserial
import serial
from datetime import datetime
import json
# Configure the COM port
port = "COM8" # Replace with the appropriate COM port name
baudrate = 9600
# baudrate = 19200
PATH = "D:\\timer_basketball.json"
def save_log(name, data):
current_time = datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f")
message_with_time = f"[{current_time}] {data}"
with open(name, "a") as file:
file.write(message_with_time + "\n")
def read_logs():
import time
with open("logs_megasport_formatted.txt", "r") as file:
for line in file:
parts = line.strip().split("] ")
if len(parts) == 2:
timestamp = parts[0][1:]
data = parts[1]
parse(eval(data))
input()
# if len(eval(data)) > 30 and eval(data)[-7] == "00":
# time.sleep(0.1)
# else:
# time.sleep(1)
def parse(data_formatted):
print(len(data_formatted))
if len(data_formatted) > 8:
print(data_formatted)
with open(PATH, "r", encoding="utf-8") as f:
new_data = json.load(f)
new_data = new_data[0]
def main():
try:
# Open the COM port
ser = serial.Serial(port, baudrate=baudrate)
print("Serial connection established.")
while True:
line = ser.readline()
line = list(line)
line = [str(hex_value // 16) + str(hex_value % 16) for hex_value in line]
save_log("logs_balashikha_formatted.txt", line)
print(line)
except serial.SerialException as se:
print("Serial port error:", str(se))
except KeyboardInterrupt:
exit(1)
finally:
if ser.is_open:
ser.close()
print("Serial connection closed.")
if __name__ == "__main__":
# new_data = {
# "timeGFX": "0:00",
# }
# with open(PATH, "w", encoding="utf-8") as file:
# json.dump([new_data], file, ensure_ascii=False, indent=4)
main()
# read_logs()