1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import os Trans = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F') class DataFrame(object): DataType = 0 Length = 0 Position = 0 Data = []
def __init__(self,DataType_,Length_,Position_,Data_): self.DataType=DataType_ self.Length=Length_ self.Position=Position_ self.Data=Data_ def __print_hex(self,x): return '%c%c'%(Trans[(x&0xF0)>>4],Trans[x&0x0F])
def __str__(self): s = ':' Calc = 0 Calc = Calc+(self.Length) s = s+self.__print_hex(self.Length) Calc = Calc+(self.Position>>8) Calc = Calc+(self.Position&0xFF) s = s+self.__print_hex(self.Position>>8)+self.__print_hex(self.Position&0xFF) Calc = Calc+(self.DataType) s = s+self.__print_hex(self.DataType) for x in self.Data: s = s+self.__print_hex(x) Calc = Calc + x Calc = 0x100 - Calc % 256 s = s+self.__print_hex(Calc&0xFF) return s
if __name__ == '__main__': os.mkdir('EEPROM.Hexs') os.chdir('EEPROM.Hexs') with open('test.hex','w') as f: Frame1 = DataFrame(0,2,0,[x,y]) Frame2 = DataFrame(1,0,0,[]) f.write(str(Frame1)+'\n') f.write(str(Frame2)+'\n')
|