2024-07-12 23:30:20 +00:00
import serial
import os
2024-07-13 21:34:14 +00:00
import time
chunksize = 4096
2024-07-12 23:30:20 +00:00
2024-07-13 21:34:14 +00:00
#LOG0002
#32 = 124 kbps
#128 = 263 kbps
#4096 = 416 kbps
#32768 = 427 kbps
#65536 = failed
serialport = serial . Serial ( port = ' /dev/ttyACM0 ' , baudrate = 115200 , timeout = 1 )
2024-07-12 23:30:20 +00:00
def establish_connection ( ) :
serialport . write ( " \n " . encode ( ) )
serialport . write ( " echo off \n " . encode ( ) )
while len ( serialport . readline ( ) ) > 0 :
continue
serialport . write ( " test \n " . encode ( ) )
response = serialport . readline ( )
hrresponse = response . rstrip ( ) . decode ( ' ascii ' )
if hrresponse != " OK " :
print ( " Unexpected test response: " + str ( response ) )
exit ( )
def get_filenames ( ) :
filenames = [ ]
serialport . write ( " ls \n " . encode ( ) )
while True :
response = serialport . readline ( )
hrresponse = response . rstrip ( ) . decode ( ' ascii ' )
if ( len ( response ) ) > 0 :
#print(hrresponse)
filenames . append ( hrresponse )
else :
break
return filenames
2024-07-13 21:34:14 +00:00
def get_filesize ( filename ) :
filesize = 0
serialport . write ( ( " sizeof " + str ( filename ) + " \n " ) . encode ( ) )
response = serialport . readline ( )
hrresponse = response . rstrip ( ) . decode ( ' ascii ' )
if ( len ( response ) ) > 0 :
filesize = int ( hrresponse )
2024-07-12 23:30:20 +00:00
2024-07-13 21:34:14 +00:00
return filesize
def copy_file ( source , destination , expectedsize ) :
os . makedirs ( os . path . dirname ( writefilename ) , exist_ok = True )
transferstarttime = time . time ( )
chunkstarttime = time . time ( )
last_print = time . time ( )
2024-07-12 23:30:20 +00:00
with open ( writefilename , ' wb ' ) as writer :
2024-07-13 21:34:14 +00:00
serialport . write ( ( " chunksize " + str ( chunksize ) + " \n " ) . encode ( ) )
serialport . write ( ( " get " + filename + " \n " ) . encode ( ) )
2024-07-12 23:30:20 +00:00
2024-07-13 21:34:14 +00:00
acc_datalen = 0
2024-07-12 23:30:20 +00:00
while True :
2024-07-13 21:34:14 +00:00
'''
2024-07-12 23:30:20 +00:00
response = serialport . readline ( )
hrresponse = response . rstrip ( ) . decode ( ' ascii ' )
if ( len ( response ) ) > 0 :
#print(hrresponse)
writer . write ( response )
else :
break
2024-07-13 21:34:14 +00:00
'''
data = serialport . read ( chunksize )
if ( len ( data ) ) > 0 : #data received
#print("received "+str(len(data))+" bytes")
#hrresponse=data.rstrip().decode('ascii')
#print(hrresponse)
acc_datalen + = len ( data )
checksum = ( sum ( data ) & 0xFF )
checksumarray = bytearray ( [ checksum ] )
writer . write ( data )
serialport . write ( checksumarray ) #request next chunk by sending checksum of last chunk
else :
break
if ( time . time ( ) - last_print > 0.5 ) :
last_print = time . time ( )
chunkduration = time . time ( ) - chunkstarttime
chunkstarttime = time . time ( )
progress = acc_datalen / expectedsize
print ( str ( round ( progress * 100 , 0 ) ) + " % \t " + str ( round ( chunkduration * 1000 , 3 ) ) + " ms for " + str ( len ( data ) ) + " Byte \t = " + str ( round ( ( len ( data ) / chunkduration ) / 1000 , 3 ) ) + " kB/s " )
fileduration = time . time ( ) - transferstarttime
file_stats = os . stat ( writefilename )
print ( " Finished transfer of " + str ( acc_datalen ) + " B or " + str ( file_stats . st_size ) + " (os) Byte in " + str ( fileduration ) + " s \t = " + str ( round ( file_stats . st_size / fileduration / 1000 , 3 ) ) + " kB/s " )
return acc_datalen
2024-07-12 23:30:20 +00:00
def log_off ( ) :
serialport . write ( ( " log off \n " ) . encode ( ) )
response = serialport . readline ( )
hrresponse = response . rstrip ( ) . decode ( ' ascii ' )
if ( not hrresponse . startswith ( " Log disabled " ) ) :
print ( " Unexpected response: " + str ( response ) )
exit ( )
def delete_file ( filename ) :
serialport . write ( ( " rm " + filename + " \n " ) . encode ( ) )
response = serialport . readline ( )
hrresponse = response . rstrip ( ) . decode ( ' ascii ' )
if hrresponse != " OK " :
print ( " Unexpected response: " + str ( response ) )
exit ( )
if serialport . isOpen ( ) :
establish_connection ( )
#Get File List
filenames = get_filenames ( )
#Copy all Files
2024-07-13 21:34:14 +00:00
failed = 0
2024-07-12 23:30:20 +00:00
for filename in filenames :
print ( " Reading file " + filename )
2024-07-13 21:34:14 +00:00
expectedsize = get_filesize ( filename )
print ( " Expecting " + str ( expectedsize ) + " Byte " )
2024-07-12 23:30:20 +00:00
writefilename = ' sdcard/ ' + filename
2024-07-13 21:34:14 +00:00
receivedsize = copy_file ( filename , writefilename , expectedsize )
if ( expectedsize != receivedsize ) :
print ( " Warning: Filesize does not match! " )
failed + = 1
print ( " " )
print ( str ( len ( filenames ) ) + " Files copied with " + str ( failed ) + " failed " )
2024-07-12 23:30:20 +00:00
#Delete all files
'''
log_off ( )
for filename in filenames :
delete_file ( filename )
'''
serialport . write ( " echo on \n " . encode ( ) )
2024-07-13 21:34:14 +00:00
serialport . close ( )