def test_algorithm_checks(algorithm_name): """Check the result of every algorithm against its recorded check-word""" check_word = crcengine.get_algorithm_params(algorithm_name, True)["check"] crc_alg = crcengine.new(algorithm_name) assert crc_alg( b"123456789") == check_word, "Check CRC mismatch for {}".format( algorithm_name)
def crc16_xmodem(): return crcengine.new('crc16-xmodem')
def crc16_kermit(): return crcengine.new('crc16-kermit')
def crc32(): return crcengine.new('crc32')
def test_crc32_bzip2(): crc32_bzip2 = crcengine.new('crc32-bzip2') assert crc32_bzip2(b'A') == 0x81B02D8B assert crc32_bzip2(b'123456789') == 0xFC891918
import socket import crcengine HOST = '127.0.0.1' # The server's hostname or IP address PORT = 65432 # The port used by the server crcAlg = crcengine.new('crc16-modbus') def calcCrc (data): return crcAlg(data) def makebuff(s:str): bts = bytearray(s,'ascii') l = len(bts) buff = bytearray(3) buff[0]=255 buff[1]=255 buff[2]=l buff.extend(bts) crc = calcCrc(bts) crcb = crc.to_bytes(2, byteorder='little') print ("crc = {} [{}, {}]".format(crc, crcb[0], crcb[1])) buff.extend(crcb) return buff with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) txt = "Saying hello" while str != "q": buff = makebuff (txt) s.sendall(buff)
def test_crc8_autosar(): crc8 = crcengine.new("crc8-autosar") assert crc8(b"123456789") == 0xDF
def test_crc8_autosar(): crc8 = crcengine.new('crc8-autosar') assert crc8(b'123456789') == 0xdf
def crc16_autosar(): return crcengine.new("crc16-autosar")
def test_crc8(): crc8 = crcengine.new("crc8") assert crc8(b"123456789") == 0xBC
def crc16_xmodem(): return crcengine.new("crc16-xmodem")
def crc16_kermit(): return crcengine.new("crc16-kermit")
def crc32(): return crcengine.new("crc32")
def crc16_autosar(): return crcengine.new('crc16-autosar')
def test_crc8_bluetooth(): crc8 = crcengine.new("crc8-bluetooth") assert crc8(b"123456789") == 0x26
def test_crc8(): crc8 = crcengine.new('crc8') assert crc8(b'123456789') == 0xbc
import crcengine if __name__ == '__main__': params = crcengine.get_algorithm_params('crc32') crcengine.generate_code(params, 'out/') crcengine.generate_code('crc16-xmodem', 'out/') filename = "out/crc16_xmodem.c" print("Created {} with the following content:") with open(filename, "r") as f: file = f.read() print(file) available = crcengine.algorithms_available() print("\nThe following algorithms are available") print(", ".join(available)) data = b"123456789" crc16x = crcengine.new("crc16-xmodem") result = crc16x.calculate(data) print("\nThe crc16-xmodem of {} is 0x{:x}".format(data.decode("ascii"), result))
def test_crc8_bluetooth(): crc8 = crcengine.new('crc8-bluetooth') assert crc8(b'123456789') == 0x26
def test_crc32_bzip2(): crc32_bzip2 = crcengine.new("crc32-bzip2") assert crc32_bzip2(b"A") == 0x81B02D8B assert crc32_bzip2(b"123456789") == 0xFC891918