2023-04-14 13:31:54 +02:00
import os
import serial
2023-04-14 15:32:13 +02:00
import time
2023-04-14 13:31:54 +02:00
2023-04-14 15:32:13 +02:00
ser = serial . Serial ( ' COM3 ' , 9600 ) # change port to input port from arduino
filename = ' mitNeumann_0_5 '
start_time = time . time ( ) # start time of read
2023-04-14 13:31:54 +02:00
with open ( filename , ' wb ' ) as file :
2023-04-14 15:32:13 +02:00
while os . path . getsize ( filename ) < 100000 : # change to the desired file size in bits
2023-04-14 13:31:54 +02:00
if ser . in_waiting > 0 :
2023-04-14 15:32:13 +02:00
data = ser . read ( ser . in_waiting ) # reading the data input from COM port
2023-04-14 13:31:54 +02:00
print ( data )
file . write ( data )
2023-04-14 15:32:13 +02:00
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
2023-04-14 13:41:23 +02:00
2023-04-14 15:32:13 +02:00
print ( f " time needed in seconds: { elapsed_time : .2f } . New filename: { new_filename } . " ) # console write