Created
July 23, 2018 15:10
-
-
Save mhutchins/4dfdb06683a5e6f7d0698c2fc7e81941 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# Note: This code should also work with python 2 | |
def crc16modbus(data, poly=0xA001, crc=0xFFFF): | |
data = bytearray(data) | |
for b in data: | |
crc ^= (0xFF & b) | |
for _ in range(0, 8): | |
if (crc & 0x0001): | |
crc = ((crc >> 1) & 0xFFFF) ^ poly | |
else: | |
crc = ((crc >> 1) & 0xFFFF) | |
return crc & 0xffff | |
instr=b'\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39' | |
print ("CRC: 0x%04x" % crc16modbus(instr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment