473,383 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 17840
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: milesh | last post by:
I have a customer that we need to send data files to using their format. Below is a C routine they sent us for computing a CRC. I tranlated it to the best of my ability to VB6 but can't reproduce...
3
by: eugene | last post by:
I looked for a crc-16(crc 16) function to use but couln't find one that worked: This one is working .. def crc16(s): crcValue=0x0000
5
by: Tumzadoc | last post by:
Hi I'm receiving a message from a device which includes the CRC16, i also have to calculate the CRC16 and check if it matches the one received. My problem is that the calculated CRC is not the...
9
by: prabhat143 | last post by:
Hi, I was recently asked to write a function in C that would detect if memory is corrupted. I had no clue about the solution but what I believe is that the solution is not complicated. Does...
2
by: eeh | last post by:
Hi, Could anyone tell me how to calculate CRC of the polynomial x^16+x^15+x^2+x^0=0 in standard c?
6
by: maxthebaz | last post by:
Our machines have this requirement: if power failure occurs, many important variables are to be resumed from where they were interrupted after the machine is restarted (power on in this case). In...
4
Ali Rizwan
by: Ali Rizwan | last post by:
Well I have made a form with I have made mine titlebar and borders. Now I want to make an ocx for it and want that it applies on form automatically. And one thing more. I write a code of moving...
4
by: rosalin1 | last post by:
Can some body help me how to write a CRC 16 program for calculating CRC check on 16 bits data?
1
by: sdbranum | last post by:
For those who have found it impossible to get help in communicating with Wasp's WDT-2200 Barcode Scanner through a COM Port, except by using their supplied DTComm software, I am presenting what I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.