CRC-CCITT 16位Python手动计算 [英] CRC-CCITT 16-bit Python Manual Calculation

查看:646
本文介绍了CRC-CCITT 16位Python手动计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我正在为嵌入式设备编写代码。

I am writing code for an embedded device. A lot of solutions out there for CRC-CCITT 16-bit calculations require libraries.

鉴于存在许多用于CRC-CCITT 16位计算的解决方案,因此使用库几乎是不可能的,并且会浪费其资源,因此需要一个函数。

Given that using libraries is almost impossible and a drain on its resources, a function is required.

可能的解决方案

以下在线计算出CRC。但是,其实现不正确。

The following CRC calculation was found online. However, its implementation is incorrect.

http://bytes.com/topic/python/insights/887357-python-check-crc-frame-crc-16-ccitt

def checkCRC(message):
    #CRC-16-CITT poly, the CRC sheme used by ymodem protocol
    poly = 0x11021
    #16bit operation register, initialized to zeros
    reg = 0xFFFF
    #pad the end of the message with the size of the poly
    message += '\x00\x00' 
    #for each bit in the message
    for byte in message:
        mask = 0x80
        while(mask > 0):
            #left shift by one
            reg<<=1
            #input the next bit from the message into the right hand side of the op reg
            if ord(byte) & mask:   
                reg += 1
            mask>>=1
            #if a one popped out the left of the reg, xor reg w/poly
            if reg > 0xffff:            
                #eliminate any one that popped out the left
                reg &= 0xffff           
                #xor with the poly, this is the remainder
                reg ^= poly
    return reg

现有的在线解决方案

以下链接正确计算了16位CRC。

The following link calculates a 16 bit CRC correctly.

http://www.lammertbies.nl/comm/info/crc-calculation.html#intr

CRC-CCITT(XModem)下的结果是正确的CRC。

The result under "CRC-CCITT (XModem)" is the correct CRC.

规格

我相信现有在线解决方案中的 CRC-CCITT(XModem)计算使用的多项式为 0x1021

I believe the "CRC-CCITT (XModem)" calculation in the existing online solution uses a polynomial of 0x1021.

问题

如果有人可以编写新功能或提供解决 checkCRC 函数达到要求的规格。请注意,使用库或任何导入都无济于事。

If someone could write a new function or provide direction to solve the checkCRC function to the required specification. Please note that the use of libraries or any import's would not help.

推荐答案

这里是 http://www.lammertbies.nl/ comm / info / crc-calculation.html 用于CRC-CCITT XMODEM

Here is a python port of the C library from http://www.lammertbies.nl/comm/info/crc-calculation.html for CRC-CCITT XMODEM

该库对于实际用例很有趣,因为它会预先计算crc表

This library is interesting for real use cases because it pre-computes a table of crc for enhanced speed.

用法(带有字符串或字节列表):

Usage (with a string or a list of bytes) :

crc('123456789')
crcb(0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39)

测试给出:'0x31c3'

POLYNOMIAL = 0x1021
PRESET = 0

def _initial(c):
    crc = 0
    c = c << 8
    for j in range(8):
        if (crc ^ c) & 0x8000:
            crc = (crc << 1) ^ POLYNOMIAL
        else:
            crc = crc << 1
        c = c << 1
    return crc

_tab = [ _initial(i) for i in range(256) ]

def _update_crc(crc, c):
    cc = 0xff & c

    tmp = (crc >> 8) ^ cc
    crc = (crc << 8) ^ _tab[tmp & 0xff]
    crc = crc & 0xffff
    print (crc)

    return crc

def crc(str):
    crc = PRESET
    for c in str:
        crc = _update_crc(crc, ord(c))
    return crc

def crcb(*i):
    crc = PRESET
    for c in i:
        crc = _update_crc(crc, c)
    return crc

您建议如果将 poly = 0x11021 替换为 poly,则 checkCRC 例程为CRC-CCITT变体 1D0F = 0x1021 开头。

Your proposed checkCRC routine is CRC-CCITT variant '1D0F' if you replace poly = 0x11021 with poly = 0x1021 at the beginning.

这篇关于CRC-CCITT 16位Python手动计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆