GMTROM/Archiv/WriteFromCOMToTxt.py

29 lines
1.0 KiB
Python
Raw Permalink Normal View History

2023-06-13 09:12:48 +02:00
import os
import serial
import time
ser = serial.Serial('COM3', 9600) # change port to input port from arduino
filename = 'mitNeumann_0_5'
start_time = time.time() # start time of read
with open(filename, 'wb') as file:
while os.path.getsize(filename) < 100000: # 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
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}.txt" # 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