2023-04-24 09:31:47 +02:00
import os
import serial
import time
2023-04-24 13:13:39 +02:00
ser = serial . Serial ( ' /dev/ttyACM0 ' , 115200 ) # change port to input port from arduino
filename = ' BSI_Testdaten '
2023-04-24 09:31:47 +02:00
start_time = time . time ( ) # start time of read
with open ( filename + ' .bin ' , ' wb ' ) as file :
2023-04-24 13:13:39 +02:00
while os . path . getsize ( filename + ' .bin ' ) < 128000000 : # change to the desired file size in bits/8 (1,000,000 Bits = 125000)
2023-04-24 09:31:47 +02:00
if ser . in_waiting > 0 :
data = ser . read ( ser . in_waiting ) # reading the data input from COM port
2023-04-24 13:13:39 +02:00
#print(data)
2023-04-24 09:31:47 +02:00
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 } _TimeInSeconds_ { seconds } _ { milliseconds } .bin " # filename in format filename_seconds_milliseconds as txt with needed time to finish read
os . rename ( filename + ' .bin ' , new_filename ) # change filename to new filename
# Open the file in binary mode
with open ( new_filename , ' rb ' ) as f :
# Read the contents of the file as bytes
content = f . read ( )
# Convert the bytes to a string of hexadecimal digits
hex_str = content . hex ( )
# Convert the bytes to a string of binary digits
2023-04-24 13:13:39 +02:00
#binary_str = ''.join(format(byte, '08b') for byte in content)
2023-04-24 09:31:47 +02:00
# Write the binary string back to the file
2023-04-24 13:13:39 +02:00
#with open(new_filename.split('.')[0] + '_asTXT.txt', 'w') as f:
#f.write(binary_str)
2023-04-24 09:31:47 +02:00
print ( f " time needed in seconds: { elapsed_time : .2f } . New filename: { new_filename } . " ) # console write
2023-04-24 13:13:39 +02:00