469,286 Members | 2,442 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

CRC16

Anyone know a module that does CRC16 for Python? I have an aplication
that I need to run it, and am not having alot of sucess. I have a
program in C that uses a CRC16 according to CCITT standards, but need
to get a program that tests it with python as well. Thanks!

Sep 23 '05 #1
4 17132
The first URL back from Google is:

http://www.onembedding.com/tools/python/code/crc16.htm

Site is Russian, but crc16.py that is listed there has
English comments. Maybe you can use some/all of it.

-Larry Bates

Tuvas wrote:
Anyone know a module that does CRC16 for Python? I have an aplication
that I need to run it, and am not having alot of sucess. I have a
program in C that uses a CRC16 according to CCITT standards, but need
to get a program that tests it with python as well. Thanks!

Sep 23 '05 #2
Tuvas wrote:
Anyone know a module that does CRC16 for Python? I have an aplication
that I need to run it, and am not having alot of sucess. I have a
program in C that uses a CRC16 according to CCITT standards, but need
to get a program that tests it with python as well. Thanks!


I'll include one below. There's a tricky bit in that most standards
assume a serial bit stream, not byte data, leading to the relection
issue discussed in the reference in the doc. Let me know if gets
the same answers as your C version.

--
--Bryan

================================================== =============
#!/usr/bin/env python

# crc16.py by Bryan G. Olson, 2005
# This module is free software and may be used and
# distributed under the same terms as Python itself.

"""
CRC-16 in Python, as standard as possible. This is
the 'reflected' version, which is usually what people
want. See Ross N. Williams' /A Painless Guide to
CRC error detection algorithms/.
"""

from array import array
def crc16(string, value=0):
""" Single-function interface, like gzip module's crc32
"""
for ch in string:
value = table[ord(ch) ^ (value & 0xff)] ^ (value >> 8)
return value
class CRC16(object):
""" Class interface, like the Python library's cryptographic
hash functions (which CRC's are definitely not.)
"""

def __init__(self, string=''):
self.val = 0
if string:
self.update(string)

def update(self, string):
self.val = crc16(string, self.val)

def checksum(self):
return chr(self.val >> 8) + chr(self.val & 0xff)

def hexchecksum(self):
return '%04x' % self.val

def copy(self):
clone = CRC16()
clone.val = self.val
return clone
# CRC-16 poly: p(x) = x**16 + x**15 + x**2 + 1
# top bit implicit, reflected
poly = 0xa001
table = array('H')
for byte in range(256):
crc = 0
for bit in range(8):
if (byte ^ crc) & 1:
crc = (crc >> 1) ^ poly
else:
crc >>= 1
byte >>= 1
table.append(crc)

crc = CRC16()
crc.update("123456789")
assert crc.checksum() == '\xbb\x3d'

Sep 23 '05 #3
Tuvas wrote:
Anyone know a module that does CRC16 for Python? I have an aplication
that I need to run it, and am not having alot of sucess. I have a
program in C that uses a CRC16 according to CCITT standards, but need
to get a program that tests it with python as well. Thanks!


Try this one

http://crcmod.sourceforge.net/

It will do what you want as long as you know what polynomial to use and
what type of algorithm. I think you want the CCITT polynomial with a
bit-reverse algorithm. If that is the case, the following will generate
the Python function you want (assumes you want to seed the CRC
calculation with the starting value of 0xffff).

import crcmod
crc16 = crcmod.mkCrcFun(0x11021, 0xffff, True)

Now compute the crc of a data string as

crc = crc16(data)
Sep 23 '05 #4
I should probably have mentioned that my code is just 6 bytes long, we
send an 8 byte data packet with 2 bytes CRC. There's a CRC poly of
0x1021. Still having a bit of problems, but it's coming, thanks for the
help!

Sep 26 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by eugene | last post: by
5 posts views Thread by Tumzadoc | last post: by
9 posts views Thread by prabhat143 | last post: by
2 posts views Thread by eeh | last post: by
6 posts views Thread by maxthebaz | last post: by
4 posts views Thread by rosalin1 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.