CRC16

T

Tuvas

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!
 
B

Bryan Olson

Tuvas said:
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'
 
R

Raymond L. Buvel

Tuvas said:
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)
 
T

Tuvas

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

CRC16 CCITT 2
need help with converting c function to python function 2
Python ideas 0
CRC16 not the same 5
CRC Doubts 1
CRC calculation 5
Crc16 on power failure 6
My boss tells me to work at home 5

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top