module: ihex

changelog

dependencies

documentation

installation

source-file

file: http://darcs.erazor-zone.de/python/ihex/ihex.py

"""
  ihex- Objects
      copyright   Alexander Krause <alexander.krause@erazor-zone.de>
      date        2006.06.11
      version     0.0.1
"""
import re
 
class ihex_record:
  "simple record, has address and data"
  def __init__(self,address,data):
    self.address=address
    self.data=data
 
class ihex:
  def __init__(self,filename):
    self.mrecords=[]
 
    if filename!='':
      self.readFile(filename)
 
  def clear(self):
    "remove all records"
    self.mrecords=[]
 
  def readFile(self,filename):
    "read hex file and add our records"
    fileObj=open(filename,'r',1)
 
    prog = re.compile('^:([0-9a-fA-F]{2})([0-9a-fA-F]{4})([0-1]{2})([0-9a-fA-F]*)([0-9a-fA-F]{2})')
 
    self.clear()
    cAddress=-1
 
    for line in fileObj:
      #1        - byte-count  1 byte
      #2        - address     2 bytes
      #3        - type        1 byte
      #4..n-1   - data
      #n        - checksum
      result=prog.match(line)
      byte_count,address,type,checksum=int(result.group(1),16),int(result.group(2),16),int(result.group(3),16),int(result.group(5),16)
 
      myChecksum=byte_count+((address>>8)&0xff)+(address&0xff)+type
 
      data=[]
 
      for cIndex in range(0,len(result.group(4)),2):
        cByte=int(result.group(4)[cIndex:cIndex+2],16)
	myChecksum+=cByte
        data.append(cByte)
 
      myChecksum=0x100-myChecksum&0xff
      if len(data)!=byte_count:
        print "Bytecount (%i!=%i) does not match!" % (len(data),byte_count)
 
      if myChecksum!=checksum:
        print "Checksum mismatch!"
 
      if type==0:
        if cAddress==address:
	  self.mrecords[-1].data+=data
	else:
          self.mrecords.append(ihex_record(address,data))
 
      cAddress=address+len(data)
 
    fileObj.close()
 
wiki/projects/python/ihex.txt · Last modified: 2006/06/11 12:15 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki