comparison python/hexfile.py @ 233:d3dccf12ca88

Added hexfile tests
author Windel Bouwman
date Sun, 14 Jul 2013 12:28:23 +0200
parents ed230e947dc6
children 6259856841a0
comparison
equal deleted inserted replaced
232:e621e3ba78d2 233:d3dccf12ca88
1 import os 1 import os
2 2
3 class HexFileException(Exception): 3 class HexFileException(Exception):
4 pass 4 pass
5 5
6 def parseHexLine(line): 6 def parseHexLine(line):
7 """ Parses a hexfile line into three parts """ 7 """ Parses a hexfile line into three parts """
8 line = line[1:] # Remove ':' 8 line = line[1:] # Remove ':'
9 nums = bytes.fromhex(line) 9 nums = bytes.fromhex(line)
10 bytecount = nums[0] 10 bytecount = nums[0]
11 if len(nums) != bytecount + 5: 11 if len(nums) != bytecount + 5:
12 raise HexFileException('byte count field incorrect') 12 raise HexFileException('byte count field incorrect')
13 crc = sum(nums) 13 crc = sum(nums)
14 if (crc & 0xFF) != 0: 14 if (crc & 0xFF) != 0:
15 raise HexFileException('crc incorrect') 15 raise HexFileException('crc incorrect')
16 address = nums[1] * 256 + nums[2] 16 address = nums[1] * 256 + nums[2]
17 typ = nums[3] 17 typ = nums[3]
18 data = nums[4:-1] 18 data = nums[4:-1]
19 return (address, typ, data) 19 return (address, typ, data)
20
20 21
21 class HexFile: 22 class HexFile:
22 """ Represents an intel hexfile """ 23 """ Represents an intel hexfile """
23 def __init__(self, filename=None): 24 def __init__(self, filename=None):
24 self.regions = [] 25 self.regions = []
25 self.startAddress = 0 26 self.startAddress = 0
26 self.filename = None
27 if filename:
28 self.load(filename)
29 27
30 def load(self, filename): 28 def load(self, f):
31 with open(filename, 'r') as f:
32 endOfFile = False 29 endOfFile = False
33 offset = 0 30 offset = 0
34 startAddress = 0 31 startAddress = 0
35 curAddress = 0 32 curAddress = 0
36 curData = bytearray() 33 curData = bytearray()
64 raise HexFileException('record type {0} not implemented'.format(typ)) 61 raise HexFileException('record type {0} not implemented'.format(typ))
65 print(hex(address), typ, data) 62 print(hex(address), typ, data)
66 # After all lines: 63 # After all lines:
67 if curData: 64 if curData:
68 self.regions.append(HexFileRegion(startAddress, bytes(curData))) 65 self.regions.append(HexFileRegion(startAddress, bytes(curData)))
69 # Store the filename: 66
70 self.filename = filename 67 def __repr__(self):
71 def __repr__(self): 68 i = []
72 i = [] 69 i.append(super().__repr__())
73 i.append(super().__repr__()) 70 i.append('Start address {0}'.format(hex(self.startAddress)))
74 i.append('Start address {0}'.format(hex(self.startAddress))) 71 for r in self.regions:
75 i.append('Filename: {0}'.format(self.filename)) 72 i.append(str(r))
76 for r in self.regions: 73 return os.linesep.join(i)
77 i.append(str(r)) 74
78 return os.linesep.join(i) 75 def save(self, f):
79 def save(self, filename): 76 for r in self.regions:
80 with open(filename, 'w') as f: 77 offset = 0
81 for line in f: 78 while offset < len(r.data):
82 pass 79 f.write('a')
83 80 offset += 16
81
82
84 class HexFileRegion: 83 class HexFileRegion:
85 def __init__(self, address, data = bytes()): 84 def __init__(self, address, data = bytes()):
86 self.address = address 85 self.address = address
87 self.data = data 86 self.data = data
88 def __repr__(self): 87 def __repr__(self):
89 return 'Region at 0x{0:X} of {1} bytes'.format(self.address, len(self.data)) 88 return 'Region at 0x{0:X} of {1} bytes'.format(self.address, len(self.data))
90 89
90
91 if __name__ == '__main__': 91 if __name__ == '__main__':
92 h = HexFile() 92 h = HexFile()
93 print(h) 93 print(h)
94 """ Test hexfile implementation with some hexfile """ 94 """ Test hexfile implementation with some hexfile """
95 h1 = HexFile('audio.hex') 95 h1 = HexFile()
96 with open('audio.hex', 'r') as f:
97 h1.load(f)
96 print(h1) 98 print(h1)
97 99