From 674f45cdbabeaf93de84687255af212def08e354 Mon Sep 17 00:00:00 2001 From: OSchwarzbach <2122623@stud.hs-mannheim.de> Date: Wed, 19 Apr 2023 11:27:48 +0200 Subject: [PATCH] use with BIN_Daten_ausgabe.ino --- binToString.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 binToString.py diff --git a/binToString.py b/binToString.py new file mode 100644 index 0000000..c605a2e --- /dev/null +++ b/binToString.py @@ -0,0 +1,48 @@ +import os +import serial +import string +import time + +def is_hex(char): + return char in string.hexdigits + +def convert_to_binary_string(filename): + # Open the file in binary mode + with open(filename, 'rb') as f: + # Read the contents of the file as bytes + content = f.read() + + # Convert the bytes to a string of binary digits + binary_str = ''.join(format(byte, '08b') for byte in content) + + # Write the binary string back to the file + with open(filename, 'w') as f: + f.write(binary_str) + +ser = serial.Serial('COM3', 115200) # change port to input port from arduino +filename = 'baud115200_0_2_HEX' + +start_time = time.time() # start time of read + +with open(filename, 'wb') as file: + while os.path.getsize(filename) < 12500000: # change to the desired file size in bits + if ser.in_waiting > 0: + data = ser.read(ser.in_waiting) # reading the data input from COM port + print(data) + file.write(data) + file.flush() # flush data to write + +# Convert the contents of the file to a binary string +convert_to_binary_string(filename) + +end_time = time.time() # end time of read +elapsed_time = end_time - start_time + +seconds = int(elapsed_time) +milliseconds = int((elapsed_time % 1) * 100) + +new_filename = f"{filename.split('.')[0]}_TimeInSeconds_{seconds}_{milliseconds}.bin" # filename in format filename_seconds_milliseconds as txt with needed time to finish read + +os.rename(filename, new_filename) # change filename to new filename + +print(f"time needed in seconds: {elapsed_time:.2f}. New filename: {new_filename}.") # console write