def reg_mda2(ls_phase, max_phy): frame = b'#' frame += b'\0\0\0' length = 4 * 2 + 4 * 8 frame += length.to_bytes(4, byteorder) '''add body''' frame += b'C' frame += b'\4' length = 16 frame += length.to_bytes(2, byteorder) frame += calc_adc_register(3, ls_phase[0], max_phy) frame += calc_adc_register(4, ls_phase[4], max_phy) frame += calc_adc_register(5, ls_phase[1], max_phy) frame += calc_adc_register(6, ls_phase[5], max_phy) frame += b'D' frame += b'\4' length = 16 frame += length.to_bytes(2, byteorder) frame += calc_adc_register(3, ls_phase[2], max_phy) frame += calc_adc_register(4, ls_phase[6], max_phy) frame += calc_adc_register(5, ls_phase[3], max_phy) frame += calc_adc_register(6, ls_phase[7], max_phy) '''add crc''' mycrc = CRCCCITT().calculate(frame) frame += mycrc.to_bytes(2, byteorder) return frame
def init_reg_all(): frame = b'#' frame += b'\0\0\0' length = 4 frame += length.to_bytes(4, byteorder) '''add body''' frame += b'I' frame += b'\0' length = 0 frame += length.to_bytes(2, byteorder) '''add crc''' mycrc = CRCCCITT().calculate(frame) frame += mycrc.to_bytes(2, byteorder) return frame
class CRCCCITTTest(unittest.TestCase): def setUp(self): self.crc_1 = CRCCCITT(version='XModem') self.crc_2 = CRCCCITT(version='FFFF') self.crc_3 = CRCCCITT(version='1D0F') def testNoVersionInit(self): print "Providing no version at initialization should result in an Exception" self.assertRaises(Exception, CRCCCITT(version=None)) def testWrongVersionInit(self): print "Providing wrong version at initialization should result in an Exception" self.assertRaises(Exception, CRCCCITT(version='WrongVersion')) def testNoneArgCalculate(self): print "Providing calculate method with argument set to None should result in an Exception" self.assertRaises(Exception, self.crc_1.calculate(None)) def testNoArgCalculate(self): print "Providing calculate method with no argument should return 0" self.assertEqual(self.crc_1.calculate(), 0) def testCalculateVersion3(self): print "Calculated CRC CCITT (0x1D0F) for 0123456789 should be 0x18A1" self.assertEqual(self.crc_3.calculate("0123456789"), int('0x18A1', 0)) def testCalculateVersion2(self): print "Calculated CRC CCITT (0xFFFF) for 0123456789 should be 0x7D61" self.assertEqual(self.crc_2.calculate("0123456789"), int('0x7D61', 0)) def testCalculateVersion1(self): print "Calculated CRC CCITT (XModem) for 0123456789 should be 0x9C58" self.assertEqual(self.crc_1.calculate("0123456789"), int('0x9C58', 0)) def testTableItem42(self): print "The precalculated table's item #42 should be 0x8528" self.assertEqual(self.crc_1.crc_ccitt_tab[42], '0x8528') def testTableItem10(self): print "The precalculated table's item #10 should be 0xa14a" self.assertEqual(self.crc_1.crc_ccitt_tab[10], '0xa14a') def testTableItems(self): print "After creating a CRC CCITT object we must have a precalculated table with 256 items" self.assertEqual(len(self.crc_1.crc_ccitt_tab), 256) def testTableNotEmpty(self): print "After creating a CRC CCITT object we must have a precalculated table not empty" self.assertIsNot(self.crc_1.crc_ccitt_tab, [])
class CRCCCITTTest(unittest.TestCase): def setUp(self): self.crc_1 = CRCCCITT(version = 'XModem') self.crc_2 = CRCCCITT(version = 'FFFF') self.crc_3 = CRCCCITT(version = '1D0F') def testNoVersionInit(self): print "Providing no version at initialization should result in an Exception" self.assertRaises(Exception, CRCCCITT(version = None)) def testWrongVersionInit(self): print "Providing wrong version at initialization should result in an Exception" self.assertRaises(Exception, CRCCCITT(version = 'WrongVersion')) def testNoneArgCalculate(self): print "Providing calculate method with argument set to None should result in an Exception" self.assertRaises(Exception, self.crc_1.calculate(None)) def testNoArgCalculate(self): print "Providing calculate method with no argument should return 0" self.assertEqual(self.crc_1.calculate(), 0) def testCalculateVersion3(self): print "Calculated CRC CCITT (0x1D0F) for 0123456789 should be 0x18A1" self.assertEqual(self.crc_3.calculate("0123456789"), int('0x18A1', 0)) def testCalculateVersion2(self): print "Calculated CRC CCITT (0xFFFF) for 0123456789 should be 0x7D61" self.assertEqual(self.crc_2.calculate("0123456789"), int('0x7D61', 0)) def testCalculateVersion1(self): print "Calculated CRC CCITT (XModem) for 0123456789 should be 0x9C58" self.assertEqual(self.crc_1.calculate("0123456789"), int('0x9C58', 0)) def testTableItem42(self): print "The precalculated table's item #42 should be 0x8528" self.assertEqual(self.crc_1.crc_ccitt_tab[42], '0x8528') def testTableItem10(self): print "The precalculated table's item #10 should be 0xa14a" self.assertEqual(self.crc_1.crc_ccitt_tab[10], '0xa14a') def testTableItems(self): print "After creating a CRC CCITT object we must have a precalculated table with 256 items" self.assertEqual(len(self.crc_1.crc_ccitt_tab), 256) def testTableNotEmpty(self): print "After creating a CRC CCITT object we must have a precalculated table not empty" self.assertIsNot(self.crc_1.crc_ccitt_tab, [])
def reg_mla(value, max_phy): frame = b'#' frame += b'\0\0\0' length = 4 + 4 * 1 frame += length.to_bytes(4, byteorder) '''add body''' frame += b'A' frame += b'\1' length = 4 frame += length.to_bytes(2, byteorder) frame += calc_adc_register_unipolar(0, value, max_phy) '''add crc''' mycrc = CRCCCITT().calculate(frame) frame += mycrc.to_bytes(2, byteorder) return frame
def reg_mda1(ls_phase, max_phy): frame = b'#' frame += b'\0\0\0' length = 4 * 2 + 4 * 4 frame += length.to_bytes(4, byteorder) '''add body''' frame += b'A' frame += b'\2' length = 8 frame += length.to_bytes(2, byteorder) frame += calc_adc_register(1, ls_phase[0], max_phy) frame += calc_adc_register(2, ls_phase[1], max_phy) frame += b'B' frame += b'\2' length = 8 frame += length.to_bytes(2, byteorder) frame += calc_adc_register(1, ls_phase[2], max_phy) frame += calc_adc_register(2, ls_phase[3], max_phy) '''add crc''' mycrc = CRCCCITT().calculate(frame) frame += mycrc.to_bytes(2, byteorder) return frame
def testWrongVersionInit(self): print "Providing wrong version at initialization should result in an Exception" self.assertRaises(Exception, CRCCCITT(version='WrongVersion'))
def testNoVersionInit(self): print "Providing no version at initialization should result in an Exception" self.assertRaises(Exception, CRCCCITT(version=None))
def setUp(self): self.crc_1 = CRCCCITT(version='XModem') self.crc_2 = CRCCCITT(version='FFFF') self.crc_3 = CRCCCITT(version='1D0F')
def setUp(self): self.crc_1 = CRCCCITT(version = 'XModem') self.crc_2 = CRCCCITT(version = 'FFFF') self.crc_3 = CRCCCITT(version = '1D0F')
def progmem_crc(self) -> int: crc_calc = CRCCCITT() check_sum = crc_calc.calculate(self.prog_mem) return check_sum