View Full Version : crc-16 for python
Don Tyszko
05-05-2009, 12:21 PM
We are looking for a CRC-16 calculation that can run on SNAPPy.
The 'C' equivalent is below. We only got portions to work correctly in SNAPy
The C equivalent:
The BYTES are 8 bit uns char
void generate_crc(BYTE message[], int length)
{
BYTE a;
int icounter;
crc_high = '\0'; crc_low = '\0'; a = '\0'
for (icounter = 0; icounter < length; icounter++)
{
a = message[icounter] ^ crc_high;
crc_high = crc_high_table[a] ^ crc_low
crc_low = crc_low_table[a]
}
}
kbanks
05-05-2009, 06:35 PM
We only got portions to work correctly in SNAPy
You are going to have to be a little more specific... since you didn't show your SNAPpy code, it is impossible for me to guess what you are doing wrong.
Please post your non-working SNAPpy script instead of your working C code.
In the meantime, here are some potential "gotchas" that I can think of:
1) SNAPpy does not have 8-bit "BYTES", only 16-bit integers. So, you may find it necessary to force rollover to occur at 255 (256 wraps back around to 0, etc.).
One way to do this would be to use the "and" function.
Example C snippet:
unsigned char a;
unsigned char b;
unsigned char c;
a = 255;
b = 1;
c = a + b; // because of "byte rollover", c = 0, not 256
Example SNAPpy snippet that will give a different total:
a = 255;
b = 1;
c = a + b; # c is a 16-bit int, and is 256 instead of the desired 0
[code]
You can force the needed rollover by doing:
[code]
c = c & 0xFF
SNAPpy supports XOR using the same ^ character as C, so that should not be a problem.
Another problem I often see is failure to use the ord() function.
If you want to treat the bytes of a string as numbers, you want to use expressions like:
ord(str[5])
not just:
str[5] # this is a very short string, not the ASCII value you might have expected
My final suggestion is one I made in a recent post... use liberal print statements to "trace" what your SNAPpy script is doing. You may be able to tell what is going wrong by looking at the intermediate results (compared to what your C version does).
Somebody else may have a SNAPpy CRC function handy that they can post...
jabelli
05-07-2009, 10:10 AM
This is the code we are using. It has been tested as working in standard python, but is returning "None" in SNAPpy.
def crc16(buff, crc = 0, poly = 0xa001):
l = len(buff)
i = 0
while i < l:
ch = ord(buff[i])
uc = 0
while uc < 8:
if (crc & 1) ^ (ch & 1):
crc = (crc >> 1) ^ poly
else:
crc >>= 1
ch >>= 1
uc += 1
i += 1
return crc
kbanks
05-11-2009, 03:27 PM
I posted a reply to this the other day, but it seems to have disappeared (it is possible that I had it typed up in a browser window, but never got around to hitting "Submit"). At any rate, I apologize for the delay.
When I looked at this script the other day, two things jumped out at me:
1) SNAPpy does not support default parameters. When you invoke your function, be sure to specify values for all parameters.
crc16(buff, 0, 0xa001) # this can work
crc16(buff) # this has no chance of working
2) Be sure and look at the topic "Remember SNAPpy Integers have a Sign Bit" in the SNAP Reference Manual (page 72 of the 2.1 version).
Your math may not be coming out like you want, until you add an "and" (&) with 0x7FFF after shifting (>>).
Again, sorry for the late reply. The forum highlights new posts, and I usually only check the highlighted threads.
vBulletin® v3.7.5, Copyright ©2000-2010, Jelsoft Enterprises Ltd.