| Home | Trees | Indices | Help |
|---|
|
|
This module can model common CRC algorithms given the set of defining parameters. This is intended to be easy to use for experimentation rather than optimized for speed. It is slow even for a native Python CRC implementation.
Several common CRC algorithms are predefined in this module.
>>> '%X' % CRC32.calcString('123456789') 'CBF43926'
This test function runs all of the defined algorithms on the test input string '123456789':
>>> _printResults() CRC-5-USB: 19 CRC-8-SMBUS: F4 CRC-15: 059E CRC-16: BB3D CRC-16-USB: B4C8 CRC-CCITT: 29B1 CRC-HDLC: 906E CRC-24: 21CF02 CRC-32: CBF43926 CRC-32C: E3069283 CRC-64: 46A5A9388A5BEFFE CRC-256: 79B96BDC0C519B239BE759EC0688C86FD25A3F4DF1E7F054AD1F923D0739DAC8
Calculating in parts:
>>> value = CRC32.calcString('1234') >>> '%X' % CRC32.calcString('56789', value) 'CBF43926'
Or, done a different way:
>>> crc = CrcRegister(CRC32) >>> crc.takeString('1234') >>> crc.takeString('56789') >>> '%X' % crc.getFinalValue() 'CBF43926'
Inversion of a CRC function:
>>> CRC_CCITT.reverse().reflect().calcWord(54321, 16, 0) 1648 >>> CRC_CCITT.calcWord(_, 16, 0) 54321
A 15-bit CRC is used in CAN protocols. The following sample CAN frame (in binary here) is converted to hexadecimal for the calcWord call. The bits after the 15-bit CRC are not included in the CRC:
0 11101000001 0 0 0 0001 00010010 011000010111011 1 1 1 1111111
This sample CAN frame was found in this paper: <http://www.anthony-marino.com/documents/HDL_implementation_CAN.pdf>
>>> '%X' % CRC15.calcWord(0x3A08112, 27) '30BB'
If the CRC is included, the remainder should always be zero:
>>> print CRC15.calcWord(0x1D0408930BB, 42) 0
A 5-bit CRC is used some kinds of USB packets. Here is a sample start-of-frame packet:
10100101 01100111000 01111
(found at <http://www.nital.com/corporate/usb2snooper.html>)
The first field is the PID (not included in the CRC), the next 11-bit field is the frame number (0xE6, LSb-first order), and the final five bits are the CRC (0x1E, LSb-first order).
>>> '%X' % CRC5_USB.calcWord(0xE6, 11) '1E'
| Classes | |
|
CrcAlgorithm Represents the parameters of a CRC algorithm. |
|
|
CrcRegister Holds the intermediate state of the CRC algorithm. |
|
| Functions | |||
|
|||
|
|||
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0beta1 on Mon Jun 11 17:53:44 2007 | http://epydoc.sourceforge.net |