Python Forum
Read microcontroller eeprom and feed into python to find checksums.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read microcontroller eeprom and feed into python to find checksums.
#3
Thank you for the reply.

The package has helped a lot, I can now find the checksums and log the start and end of the data blocks.
Can anyone give me some pointers of speeding up this script. I am reading a 2048kb file and calculating all data blocks with their checksums. However it is so so slow. Running the script on the file for 24hrs and the script only read through 23% of the file, I don't really want to be running the computer for 4 days on each file if I can cut down the time.
My coding is as below:

#!/usr/bin/python

import os
import sys
import crcengine

def progress(count, total, suffix=''):
    bar_len = 60
    filled_len = int(round(bar_len * count / float(total)))

    percents = round(100.0 * count / float(total), 1)
    bar = '=' * filled_len + '-' * (bar_len - filled_len)

    sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', suffix))
    sys.stdout.flush()


def main(argv):
	inputfile = sys.argv[1]
	inputfilesize = os.path.getsize(sys.argv[1])
	outputfile = sys.argv[1]+'.txt'
	woutputfiledata = open(outputfile, 'a+')

	print ('Input file is:' + inputfile)
	print ('Finding checksums...')

	f1 = open(inputfile, "rb")   	# open argv[1] file for binary reading
									#
	block = 1						# used to keep track of found blocks - increments
	previousPos = 0					# starts at zero - increments by amount of bytes found in each block
	currentPos = 0					# current byte position within the file
	bytelist = []					# bytes currently read
	message = b''					# hex string passed to the crcengine function
	chkmessage = b''				# calculated checksum of bytelist and message used to see if it equal to the last two bytes
	CRCcheck = ['00','00']			# last two bytes of bytelist - compared to the checksum of bytelist[-2]
	CRCcheckMessage = 'CRC=0x'			# CRCcheckMessage to compare to chkmessage
	crc_algorithm = crcengine.new('crc16-ccitt-false')	# set up crc algorithm
	successcounter = 0
	
	while 1:
		byte_s = f1.read(1)		# read 1 byte from the file
		if not byte_s:			# if no bytes read quit
			f1.close()
			break
			
		bytelist.append(byte_s)  # add read byte to bytelist
		
		while len(bytelist) < 3: 	# check theres at least 3 bytes
			byte_s = f1.read(1)		# last two are used as the checksum comparitor
			currentPos +=1			# increase currentPos to keep track of bytes read and position within file
			bytelist.append(byte_s) # then add the byte to bytelist
			
		CRCcheck[0] = bytelist[-2]	# add last but one byte read to the first position of the checksum list
		CRCcheck[1] = bytelist[-1]	# add last bite read to the second position of the checksum list
		message = message + bytelist[-3]	# add all bytes bar the last two to the byte string
		result = crc_algorithm(message)		# send the byte string and store the returned checksum int into result
		chkmessage ='CRC=0x{:04x}'.format(result)	# convert checksum message into a 2 byte hex string
		
		CRCcheckMessage = CRCcheckMessage + CRCcheck[0].hex()
		CRCcheckMessage = CRCcheckMessage + CRCcheck[1].hex()
		
		if CRCcheckMessage == chkmessage:
			woutputfiledata.write('BLK: '+ str(block)+'\n')
			woutputfiledata.write('SOB: '+ '0x{:08x}'.format(previousPos)+'\n')
			woutputfiledata.write('EOB: '+ '0x{:08x}'.format(currentPos-2)+'\n')
			woutputfiledata.write('CKS: '+ chkmessage+'\n')
			woutputfiledata.write('\n')
			block += 1
			successcounter += 1
			previousPos = currentPos + 1
			bytelist.clear()
			
		CRCcheckMessage = 'CRC=0x'
		currentPos +=1
		progress(currentPos, inputfilesize)

if __name__ == "__main__":
	main(sys.argv[1:])
I will only ever have to read a file once, when I have all the checksums I will be able to just mod a particular block and recalculate that blocks checksum.

The script ouputs to a txt file like below:

BLK: 1
SOB: 0x00000000
EOB: 0x000084fb
CKS: CRC=0x1a49

BLK: 2
SOB: 0x000084fe
EOB: 0x00039b34
CKS: CRC=0x4444

BLK: 3
SOB: 0x00039b37
EOB: 0x0006d4cb
CKS: CRC=0xffff

BLK: 4
SOB: 0x0006d4ce
EOB: 0x000754cc
CKS: CRC=0xffff

This is the last file I tried and it seems to be working from manually checking the file locations. I just need to be able to run through the file quicker.
Reply


Messages In This Thread
RE: Read microcontroller eeprom and feed into python to find checksums. - by mikeak2001 - Mar-25-2020, 04:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to feed python script of a file absolut 6 1,185 Jan-11-2025, 07:03 AM
Last Post: Gribouillis
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,334 May-13-2024, 05:57 AM
Last Post: Larz60+
Exclamation Multi-Threaded Camera Feed issue Khajababa69 0 1,514 May-05-2024, 09:44 PM
Last Post: Khajababa69
  Draw bounding boxes on live feed Jerome 0 1,274 Jan-20-2024, 10:50 PM
Last Post: Jerome
  read a text file, find all integers, append to list oldtrafford 12 10,228 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  How to parse a live feed in Python? Daring_T 2 8,668 Jan-20-2022, 04:17 AM
Last Post: Daring_T
  Feed List items with Integer euras 9 5,807 May-19-2021, 07:45 PM
Last Post: snippsat
  splitting UAV/sat images to smaller pieces in order to feed a CNN hobbyist 0 2,026 Dec-08-2020, 11:48 AM
Last Post: hobbyist
  Rotation Effect on live Webcam Feed Leziiy 0 2,124 Sep-12-2020, 04:25 PM
Last Post: Leziiy
  Replace every 20th space with line feed T_Lafferty 3 3,502 Jul-19-2020, 10:37 PM
Last Post: T_Lafferty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020