I am having problems getting some python code to properly calculate the checksums for the Elk serial protocol.
I have looked at the serial protocol document and the example C source code in the Appendix. I have also downloaded the ELK SDK so that I can verify what the SDK is calculating for a checksum vs my code.
The way I understand it the checksum is the sum of all of the decimal representation of the ASCII characters in the message taken mod256. That number is then taken as a twos complement.
Code below. Using a test string of 00300005000 the SDK outputs a checksum of 74 while the below outputs 19. Since the SDK doesn't include source I can see what their tool is doing. Does anyone here have some experience with this? Any pointers would be very appreciated.
Thanks
def calc_checksum (string):
'''
Calculcates checksum for sending commands to the ELKM1. Sums the ASCII character values mod256 and takes
the Twos complement
'''
sum= 0
for i in range(len(string)) :
sum = sum + ord(string)
temp = sum % 256
rem = temp ^ 256
rem = rem + 1
cc1 = hex(rem)
cc = cc1.upper()
p=len(cc)
return cc[p-2
]
I have looked at the serial protocol document and the example C source code in the Appendix. I have also downloaded the ELK SDK so that I can verify what the SDK is calculating for a checksum vs my code.
The way I understand it the checksum is the sum of all of the decimal representation of the ASCII characters in the message taken mod256. That number is then taken as a twos complement.
Code below. Using a test string of 00300005000 the SDK outputs a checksum of 74 while the below outputs 19. Since the SDK doesn't include source I can see what their tool is doing. Does anyone here have some experience with this? Any pointers would be very appreciated.
Thanks
def calc_checksum (string):
'''
Calculcates checksum for sending commands to the ELKM1. Sums the ASCII character values mod256 and takes
the Twos complement
'''
sum= 0
for i in range(len(string)) :
sum = sum + ord(string)
temp = sum % 256
rem = temp ^ 256
rem = rem + 1
cc1 = hex(rem)
cc = cc1.upper()
p=len(cc)
return cc[p-2
