#-------------------------------------------------------------------------------
# Name: CRC Calculator
# Purpose: For modbusRTU CRC calculation
#
# Author: Parthi
#
# Created: 04/01/2013
# Copyright: (c) 2013
# Licence: <your licence>
#-------------------------------------------------------------------------------
#To get the bytes from user
s = raw_input("Enter the bytes: \n Make sure that the bytes going to enter are in decimal and not in HexaDecimal:\n")
data = map(int, s.split()) #To split the bytes after every SPACE
print "The Entered Bytes are:",data
n=len(data) #To find the Bytes length
print "The Total Byte length is:",n
temp=0xFFFF;
i=0
while(i<n):
temp=temp ^ data[i]; #ExOR FFFF and the actual byte
j = 0
while(j<8): #For Eight Right shift
flag=temp & 0x0001; #To get the Carry Flag
temp=temp >> 1; #To shift 1 bit to the right
if (flag==1):
temp=temp ^ 0xA001; #ExOR with A001, if Flag is 1
j=j+1;
i=i+1;
temp2=temp >> 8; #Reverse Byte Order
temp=(temp << 8) | temp2;
temp &= 0xFFFF;
print "The Final CRC is:"
print hex(temp)
-----------------------------------------------------------------------------------------------------------
# Name: CRC Calculator
# Purpose: For modbusRTU CRC calculation
#
# Author: Parthi
#
# Created: 04/01/2013
# Copyright: (c) 2013
# Licence: <your licence>
#-------------------------------------------------------------------------------
#To get the bytes from user
s = raw_input("Enter the bytes: \n Make sure that the bytes going to enter are in decimal and not in HexaDecimal:\n")
data = map(int, s.split()) #To split the bytes after every SPACE
print "The Entered Bytes are:",data
n=len(data) #To find the Bytes length
print "The Total Byte length is:",n
temp=0xFFFF;
i=0
while(i<n):
temp=temp ^ data[i]; #ExOR FFFF and the actual byte
j = 0
while(j<8): #For Eight Right shift
flag=temp & 0x0001; #To get the Carry Flag
temp=temp >> 1; #To shift 1 bit to the right
if (flag==1):
temp=temp ^ 0xA001; #ExOR with A001, if Flag is 1
j=j+1;
i=i+1;
temp2=temp >> 8; #Reverse Byte Order
temp=(temp << 8) | temp2;
temp &= 0xFFFF;
print "The Final CRC is:"
print hex(temp)
-----------------------------------------------------------------------------------------------------------