import datetime
from pathlib import Path

import serial


ser = serial.Serial('/dev/ttyUSB0', 187500, parity=serial.PARITY_EVEN)

input_file_path = Path("recordings/b_2_2_tx.txt")
numbers = [i for i in range(255)]


def replay():
    with open(input_file_path) as input_file:
        lines = input_file.readlines()

        begin = datetime.datetime.now()
        count = 1
        for line in lines:
            if "bit" in line:
                continue
            if "error" in line:
                continue

            int_to_send = int(line.split("TX: ")[1])
            byte_to_send = int_to_send.to_bytes(int_to_send, "big")
            ser.write(byte_to_send)

            ser.read(1)

            # wait for 2 acks
            if (count % 3) == 0:
                ser.read(1)

            count += 1

        end = datetime.datetime.now()
        print(end - begin)


replay()

ser.close()
