def maxim_ibutton_crc(data): crc = Crc(width=8, poly=0x31, reflect_in=True, xor_in=0x00, reflect_out=True, xor_out=0x00) return crc.bit_by_bit(data)
def check_string(opt): """ Return the calculated CRC sum of a string. """ error = False if opt.undefined_crc_parameters: sys.stderr.write("{0:s}: error: undefined parameters\n".format( sys.argv[0])) sys.exit(1) if opt.algorithm == 0: opt.algorithm = opt.algo_bit_by_bit | opt.algo_bit_by_bit_fast | opt.algo_table_driven alg = Crc(width=opt.width, poly=opt.poly, reflect_in=opt.reflect_in, xor_in=opt.xor_in, reflect_out=opt.reflect_out, xor_out=opt.xor_out, table_idx_width=opt.tbl_idx_width) crc = None if opt.algorithm & opt.algo_bit_by_bit: bbb_crc = alg.bit_by_bit(opt.check_string) if crc != None and bbb_crc != crc: error = True crc = bbb_crc if opt.algorithm & opt.algo_bit_by_bit_fast: bbf_crc = alg.bit_by_bit_fast(opt.check_string) if crc != None and bbf_crc != crc: error = True crc = bbf_crc if opt.algorithm & opt.algo_table_driven: # no point making the python implementation slower by using less than 8 bits as index. opt.tbl_idx_width = 8 tbl_crc = alg.table_driven(opt.check_string) if crc != None and tbl_crc != crc: error = True crc = tbl_crc if error: sys.stderr.write("{0:s}: error: different checksums!\n".format( sys.argv[0])) if opt.algorithm & opt.algo_bit_by_bit: sys.stderr.write( " bit-by-bit: {0:#x}\n".format(bbb_crc)) if opt.algorithm & opt.algo_bit_by_bit_fast: sys.stderr.write( " bit-by-bit-fast: {0:#x}\n".format(bbf_crc)) if opt.algorithm & opt.algo_table_driven: sys.stderr.write( " table_driven: {0:#x}\n".format(tbl_crc)) sys.exit(1) return crc
def check_string(opt): """ Return the calculated CRC sum of a string. """ error = False if opt.UndefinedCrcParameters: sys.stderr.write("%s: error: undefined parameters\n" % sys.argv[0]) sys.exit(1) #print(opt.Algorithm) if opt.Algorithm == 0: opt.Algorithm = opt.Algo_Bit_by_Bit | opt.Algo_Bit_by_Bit_Fast | opt.Algo_Table_Driven alg = Crc(width = opt.Width, poly = opt.Poly, reflect_in = opt.ReflectIn, xor_in = opt.XorIn, reflect_out = opt.ReflectOut, xor_out = opt.XorOut, table_idx_width = opt.TableIdxWidth) opt.Algorithm = opt.Algo_Table_Driven crc = None if opt.Algorithm & opt.Algo_Bit_by_Bit: #print( "bit_by_bit") bbb_crc = alg.bit_by_bit(opt.CheckString) if crc != None and bbb_crc != crc: error = True crc = bbb_crc if opt.Algorithm & opt.Algo_Bit_by_Bit_Fast: bbf_crc = alg.bit_by_bit_fast(opt.CheckString) #print( "bit_by_bit_fast") if crc != None and bbf_crc != crc: error = True crc = bbf_crc if opt.Algorithm & opt.Algo_Table_Driven: # no point making the python implementation slower by using less than 8 bits as index. opt.TableIdxWidth = 8 tbl_crc = alg.table_driven(opt.CheckString) if crc != None and tbl_crc != crc: error = True crc = tbl_crc if error: sys.stderr.write("%s: error: different checksums!\n" % sys.argv[0]) if opt.Algorithm & opt.Algo_Bit_by_Bit: sys.stderr.write(" bit-by-bit: 0x%x\n" % bbb_crc) if opt.Algorithm & opt.Algo_Bit_by_Bit_Fast: sys.stderr.write(" bit-by-bit-fast: 0x%x\n" % bbf_crc) if opt.Algorithm & opt.Algo_Table_Driven: sys.stderr.write(" table_driven: 0x%x\n" % tbl_crc) sys.exit(1) return crc
def check_string(opt): """ Return the calculated CRC sum of a string. """ error = False if opt.UndefinedCrcParameters: sys.stderr.write("%s: error: undefined parameters\n" % sys.argv[0]) sys.exit(1) if opt.Algorithm == 0: opt.Algorithm = opt.Algo_Bit_by_Bit | opt.Algo_Bit_by_Bit_Fast | opt.Algo_Table_Driven alg = Crc(width=opt.Width, poly=opt.Poly, reflect_in=opt.ReflectIn, xor_in=opt.XorIn, reflect_out=opt.ReflectOut, xor_out=opt.XorOut, table_idx_width=opt.TableIdxWidth) crc = None if opt.Algorithm & opt.Algo_Bit_by_Bit: bbb_crc = alg.bit_by_bit(opt.CheckString) if crc != None and bbb_crc != crc: error = True crc = bbb_crc if opt.Algorithm & opt.Algo_Bit_by_Bit_Fast: bbf_crc = alg.bit_by_bit_fast(opt.CheckString) if crc != None and bbf_crc != crc: error = True crc = bbf_crc if opt.Algorithm & opt.Algo_Table_Driven: # no point making the python implementation slower by using less than 8 bits as index. opt.TableIdxWidth = 8 tbl_crc = alg.table_driven(opt.CheckString) if crc != None and tbl_crc != crc: error = True crc = tbl_crc if error: sys.stderr.write("%s: error: different checksums!\n" % sys.argv[0]) if opt.Algorithm & opt.Algo_Bit_by_Bit: sys.stderr.write(" bit-by-bit: 0x%x\n" % bbb_crc) if opt.Algorithm & opt.Algo_Bit_by_Bit_Fast: sys.stderr.write(" bit-by-bit-fast: 0x%x\n" % bbf_crc) if opt.Algorithm & opt.Algo_Table_Driven: sys.stderr.write(" table_driven: 0x%x\n" % tbl_crc) sys.exit(1) return crc
def __get_crc(self, model, check_str='123456789', expected_crc=None): """ Get the CRC for a set of parameters from the Python reference implementation. """ if self.verbose: out_str = 'Crc(width = {width:d}, poly = {poly:#x}, reflect_in = {reflect_in}, xor_in = {xor_in:#x}, reflect_out = {reflect_out}, xor_out = {xor_out:#x})'.format( **model) if expected_crc is not None: out_str += ' [check = {0:#x}]'.format(expected_crc) print(out_str) alg = Crc(width=model['width'], poly=model['poly'], reflect_in=model['reflect_in'], xor_in=model['xor_in'], reflect_out=model['reflect_out'], xor_out=model['xor_out']) error = False crc = expected_crc if self.use_algo_bit_by_bit: bbb_crc = alg.bit_by_bit(check_str) if crc is None: crc = bbb_crc error = error or bbb_crc != crc if self.use_algo_bit_by_bit_fast: bbf_crc = alg.bit_by_bit_fast(check_str) if crc is None: crc = bbf_crc error = error or bbf_crc != crc if self.use_algo_table_driven: tbl_crc = alg.table_driven(check_str) if crc is None: crc = tbl_crc error = error or tbl_crc != crc if error: print('error: different checksums!') if expected_crc is not None: print(' check: {0:#x}'.format(expected_crc)) if self.use_algo_bit_by_bit: print(' bit-by-bit: {0:#x}'.format(bbb_crc)) if self.use_algo_bit_by_bit_fast: print(' bit-by-bit-fast: {0:#x}'.format(bbf_crc)) if self.use_algo_table_driven: print(' table_driven: {0:#x}'.format(tbl_crc)) return None return crc
def __get_crc(self, model, check_str="123456789", expected_crc=None): """ Get the CRC for a set of parameters from the Python reference implementation. """ if self.verbose: out_str = "Crc(width = %(width)d, poly = 0x%(poly)x, reflect_in = %(reflect_in)s, xor_in = 0x%(xor_in)x, reflect_out = %(reflect_out)s, xor_out = 0x%(xor_out)x)" % model if expected_crc is not None: out_str += " [check = 0x%x]" % expected_crc print(out_str) alg = Crc(width=model["width"], poly=model["poly"], reflect_in=model["reflect_in"], xor_in=model["xor_in"], reflect_out=model["reflect_out"], xor_out=model["xor_out"]) error = False crc = expected_crc if self.use_algo_bit_by_bit: bbb_crc = alg.bit_by_bit(check_str) if crc is None: crc = bbb_crc error = error or bbb_crc != crc if self.use_algo_bit_by_bit_fast: bbf_crc = alg.bit_by_bit_fast(check_str) if crc is None: crc = bbf_crc error = error or bbf_crc != crc if self.use_algo_table_driven: tbl_crc = alg.table_driven(check_str) if crc is None: crc = tbl_crc error = error or tbl_crc != crc if error: print("error: different checksums!") if expected_crc is not None: print(" check: 0x%x" % expected_crc) if self.use_algo_bit_by_bit: print(" bit-by-bit: 0x%x" % bbb_crc) if self.use_algo_bit_by_bit_fast: print(" bit-by-bit-fast: 0x%x" % bbf_crc) if self.use_algo_table_driven: print(" table_driven: 0x%x" % tbl_crc) return None return crc
def check_string(opt): """ Return the calculated CRC sum of a string. """ error = False if opt.undefined_crc_parameters: sys.stderr.write("{0:s}: error: undefined parameters\n".format(sys.argv[0])) sys.exit(1) if opt.algorithm == 0: opt.algorithm = opt.algo_bit_by_bit | opt.algo_bit_by_bit_fast | opt.algo_table_driven alg = Crc( width=opt.width, poly=opt.poly, reflect_in=opt.reflect_in, xor_in=opt.xor_in, reflect_out=opt.reflect_out, xor_out=opt.xor_out, table_idx_width=opt.tbl_idx_width) crc = None if opt.algorithm & opt.algo_bit_by_bit: bbb_crc = alg.bit_by_bit(opt.check_string) if crc != None and bbb_crc != crc: error = True crc = bbb_crc if opt.algorithm & opt.algo_bit_by_bit_fast: bbf_crc = alg.bit_by_bit_fast(opt.check_string) if crc != None and bbf_crc != crc: error = True crc = bbf_crc if opt.algorithm & opt.algo_table_driven: # no point making the python implementation slower by using less than 8 bits as index. opt.tbl_idx_width = 8 tbl_crc = alg.table_driven(opt.check_string) if crc != None and tbl_crc != crc: error = True crc = tbl_crc if error: sys.stderr.write("{0:s}: error: different checksums!\n".format(sys.argv[0])) if opt.algorithm & opt.algo_bit_by_bit: sys.stderr.write(" bit-by-bit: {0:#x}\n".format(bbb_crc)) if opt.algorithm & opt.algo_bit_by_bit_fast: sys.stderr.write(" bit-by-bit-fast: {0:#x}\n".format(bbf_crc)) if opt.algorithm & opt.algo_table_driven: sys.stderr.write(" table_driven: {0:#x}\n".format(tbl_crc)) sys.exit(1) return crc
def __get_crc(self, model, check_str = "123456789", expected_crc = None): """ Get the CRC for a set of parameters from the Python reference implementation. """ if self.verbose: out_str = "Crc(width = %(width)d, poly = 0x%(poly)x, reflect_in = %(reflect_in)s, xor_in = 0x%(xor_in)x, reflect_out = %(reflect_out)s, xor_out = 0x%(xor_out)x)" % model if expected_crc is not None: out_str += " [check = 0x%x]" % expected_crc print(out_str) alg = Crc(width = model["width"], poly = model["poly"], reflect_in = model["reflect_in"], xor_in = model["xor_in"], reflect_out = model["reflect_out"], xor_out = model["xor_out"]) error = False crc = expected_crc if self.use_algo_bit_by_bit: bbb_crc = alg.bit_by_bit(check_str) if crc is None: crc = bbb_crc error = error or bbb_crc != crc if self.use_algo_bit_by_bit_fast: bbf_crc = alg.bit_by_bit_fast(check_str) if crc is None: crc = bbf_crc error = error or bbf_crc != crc if self.use_algo_table_driven: tbl_crc = alg.table_driven(check_str) if crc is None: crc = tbl_crc error = error or tbl_crc != crc if error: print("error: different checksums!") if expected_crc is not None: print(" check: 0x%x" % expected_crc) if self.use_algo_bit_by_bit: print(" bit-by-bit: 0x%x" % bbb_crc) if self.use_algo_bit_by_bit_fast: print(" bit-by-bit-fast: 0x%x" % bbf_crc) if self.use_algo_table_driven: print(" table_driven: 0x%x" % tbl_crc) return None return crc
def __get_crc(self, model, check_str = '123456789', expected_crc = None): """ Get the CRC for a set of parameters from the Python reference implementation. """ if self.verbose: out_str = 'Crc(width = {width:d}, poly = {poly:#x}, reflect_in = {reflect_in}, xor_in = {xor_in:#x}, reflect_out = {reflect_out}, xor_out = {xor_out:#x})'.format(**model) if expected_crc is not None: out_str += ' [check = {0:#x}]'.format(expected_crc) print(out_str) alg = Crc(width = model['width'], poly = model['poly'], reflect_in = model['reflect_in'], xor_in = model['xor_in'], reflect_out = model['reflect_out'], xor_out = model['xor_out']) error = False crc = expected_crc if self.use_algo_bit_by_bit: bbb_crc = alg.bit_by_bit(check_str) if crc is None: crc = bbb_crc error = error or bbb_crc != crc if self.use_algo_bit_by_bit_fast: bbf_crc = alg.bit_by_bit_fast(check_str) if crc is None: crc = bbf_crc error = error or bbf_crc != crc if self.use_algo_table_driven: tbl_crc = alg.table_driven(check_str) if crc is None: crc = tbl_crc error = error or tbl_crc != crc if error: print('error: different checksums!') if expected_crc is not None: print(' check: {0:#x}'.format(expected_crc)) if self.use_algo_bit_by_bit: print(' bit-by-bit: {0:#x}'.format(bbb_crc)) if self.use_algo_bit_by_bit_fast: print(' bit-by-bit-fast: {0:#x}'.format(bbf_crc)) if self.use_algo_table_driven: print(' table_driven: {0:#x}'.format(tbl_crc)) return None return crc
class Honeywell3300ReadRegisterCmd: """Only class in this interface thus far. We are only reading from these units.""" def __init__(self, slaveAddy, functionCode, registerAddy, numRegisters): self.slaveAddy = slaveAddy self.functionCode = functionCode self.registerAddy = registerAddy self.numRegisters = numRegisters self.crc = Crc(width=16, poly=0x8005, reflect_in=True, xor_in=0xFFFF, reflect_out=True, xor_out=0x0000) self.rplyBytes = 0 self.rplyData = list() # list of 16 bit integer data def performCRC(self, data): return self.crc.bit_by_bit(data) def createPacket(self): buffer = struct.pack("B", self.slaveAddy) buffer += struct.pack("B", self.functionCode) buffer += struct.pack("BB", self.registerAddy[0], self.registerAddy[1]) buffer += struct.pack(">H", self.numRegisters) buffer += struct.pack(">H", self.performCRC(buffer)) return buffer def processReply(self, rplyBuf): startPos = rplyBuf.tell() rplySaveAddy = ord(rplyBuf.read(1)) rplyFunctionCode = ord(rplyBuf.read(1)) # TODO: addy and function code self.rplyBytes = ord(rplyBuf.read(1)) # TODO: test length against expected length for i in range(len(self.numRegisters)): self.rplyData.append(int(struct.unpack(">H", rplyBuf.read(2))))
datas = [ [0x4e00,0xffff,0x26ec,0x1409,0x1600,0xa018,0x3039,0x3132,0x3031,0x3131,0x3130,0x3039,0x3030,0x3132,0x3031,0x3030,0x3030,0x3131,0x3130,0x3039,0x3030,0x3132,0x3031,0x3030,0x3030,0x3131,0x3130,0x3039,0x3030,0x3132,0x3031,0x3030,0x3030,0xffff,0x225e,0x3433,0x3132,0x4f31,0x3537], [0x6400,0x0000,0x0000,0x0000,0x8080,0x8080,0x8080,0x0000,0x0000,0x0000], [0xe001,0xeb00,0x1301,0x0500,0x0e01,0x0500,0xbc02,0x9001,0x9001,0x9001,0x3c00,0x2800,0x4600,0x2800,0xa816,0x6810,0x4001,0x2b00,0x1e00,0x4f00,0x0200,0xbb00,0x8001,0x8000,0x3301,0x00c0,0x0005,0x9999,0x0100,0x0000,0x33b3,0xcd4c,0x0060,0x0601,0xae07,0x0a57,0x5c0f,0xc3b5,0xd763,0x4a0f,0x1f85,0x48a1,0xb81e,0x5c8f,0x3333,0x00c0,0xf628,0x6009,0xaec7,0x852b,0x5238,0xcd4c,0x3200,0xb80b,0x3700,0xd801,0x8804,0x2201,0x8002,0xe803,0x3400,0x0032,0x8901,0x0000,0x0000,0xcd66,0x4801,0x8300,0x4801,0x3d0a,0x0401,0x1400,0x6400,0xc201,0xbbfe,0x3c00,0x7102,0x7e01,0x0f00,0xdc05,0x0000,0x3403,0xdc05,0xcd4c,0x3333,0x9a19,0xdc05,0x2400,0xb81e,0x7b14,0x66e6,0xa4f0,0xffff,0xdb03,0x4821,0xd703,0xa410,0xf628,0x0000,0x4801,0x66e6,0x6f12,0x6f12,0x71fd,0x71fd,0xe7f3,0x0040,0x2500,0x0000,0x0000,0x0000,0x0000,0x5e80,0xa27f,0xb89e,0x856b,0xd501,0x0004,0x6602,0x8503,0x4200,0x2000,0x2405,0x3200,0x6400,0x3200,0x1900,0xf401,0x2918,0x0000,0x0800,0x0100,0x2602,0xa000,0x0a00,0xc800,0x3200,0x2800,0x0a00,0x1400,0xfa03,0x5613,0x8214,0x2800,0x0002,0x0000,0x9600,0x9600,0xd007,0x0000,0x2700,0x5000,0xb004,0x5400,0x1400,0x6400,0xfa00,0x220b,0xe204,0x5e01,0x6400,0xdc05,0x800c,0x2800,0x0a00,0x0500,0x1405,0x7300,0x7300,0xe103,0x1c00,0xee00,0x0000,0x0080,0xfe7f,0x0000,0x8900,0x8900,0x550d,0x0601,0x0601,0x2200,0x5500,0xab1a,0xca08,0x6400,0x6400,0x9001,0x2823] ] crcs = [0xC0F8,0x72F8,0x95EE] initialValue = 0x3132 # 4e00ffff26ec14091600a018303931323031313131303039303031323031303030303131313030393030313230313030303031313130303930303132303130303030ffff225e343331324f313537 # 6400000000000000808080808080000000000000 # e001eb00130105000e010500bc029001900190013c00280046002800a816681040012b001e004f000200bb0080018000330100c0000599990100000033b3cd4c00600601ae070a575c0fc3b5d7634a0f1f8548a1b81e5c8f333300c0f6286009aec7852b5238cd4c3200b80b3700d801880422018002e80334000032890100000000cd664801830048013d0a040114006400c201bbfe3c0071027e010f00dc0500003403dc05cd4c33339a19dc052400b81e7b1466e6a4f0ffffdb034821d703a410f6280000480166e66f126f1271fd71fde7f30040250000000000000000005e80a27fb89e856bd5010004660285034200200024053200640032001900f40129180000080001002602a0000a00c800320028000a001400fa035613821428000002000096009600d007000027005000b004540014006400fa00220be2045e016400dc05800c28000a000500140573007300e1031c00ee0000000080fe7f000089008900550d0601060122005500ab1aca086400640090012823 from crc_algorithms import Crc crcer = Crc(width = 16, poly = 0xaabd, reflect_in = False, xor_in = initialValue, reflect_out = False, xor_out = 0xd706) print("0x%x" % crcer.bit_by_bit(datas[0])) print("0x%x" % crcer.bit_by_bit(datas[1])) print("0x%x" % crcer.bit_by_bit(datas[2])) for data, crc in zip(datas, crcs): print("Looking for checksum of 0x%x in 0x%x bytes..." % (crc, len(data) * 2)) for poly in range(0x0000, 0xFFFF): crcer = Crc(width = 16, poly = poly, reflect_in = False, xor_in = initialValue, reflect_out = False, xor_out = 0xd706) res = crcer.bit_by_bit_fast(data) if res == crc: print("Solved for 0x%x! polynomial: 0x%x" % (crc, poly))
crcs = [0xC0F8, 0x72F8, 0x95EE] initialValue = 0x3132 # 4e00ffff26ec14091600a018303931323031313131303039303031323031303030303131313030393030313230313030303031313130303930303132303130303030ffff225e343331324f313537 # 6400000000000000808080808080000000000000 # e001eb00130105000e010500bc029001900190013c00280046002800a816681040012b001e004f000200bb0080018000330100c0000599990100000033b3cd4c00600601ae070a575c0fc3b5d7634a0f1f8548a1b81e5c8f333300c0f6286009aec7852b5238cd4c3200b80b3700d801880422018002e80334000032890100000000cd664801830048013d0a040114006400c201bbfe3c0071027e010f00dc0500003403dc05cd4c33339a19dc052400b81e7b1466e6a4f0ffffdb034821d703a410f6280000480166e66f126f1271fd71fde7f30040250000000000000000005e80a27fb89e856bd5010004660285034200200024053200640032001900f40129180000080001002602a0000a00c800320028000a001400fa035613821428000002000096009600d007000027005000b004540014006400fa00220be2045e016400dc05800c28000a000500140573007300e1031c00ee0000000080fe7f000089008900550d0601060122005500ab1aca086400640090012823 from crc_algorithms import Crc crcer = Crc(width=16, poly=0xaabd, reflect_in=False, xor_in=initialValue, reflect_out=False, xor_out=0xd706) print("0x%x" % crcer.bit_by_bit(datas[0])) print("0x%x" % crcer.bit_by_bit(datas[1])) print("0x%x" % crcer.bit_by_bit(datas[2])) for data, crc in zip(datas, crcs): print("Looking for checksum of 0x%x in 0x%x bytes..." % (crc, len(data) * 2)) for poly in range(0x0000, 0xFFFF): crcer = Crc(width=16, poly=poly, reflect_in=False, xor_in=initialValue, reflect_out=False, xor_out=0xd706) res = crcer.bit_by_bit_fast(data) if res == crc: