搜索
bottom↓
回复: 10

求教:串口接收数据校检使用论坛中得当CRC都不对?

[复制链接]

出0入0汤圆

发表于 2013-1-6 21:07:06 | 显示全部楼层 |阅读模式
已经知道是用的51单片机,通过接收串口数据知道以下几组数据:
aa c1 2a 45 42 bb         ----------------第一组
aa d1 30 32 30 30 30 31 2a 46 38 bb----------第二组
aa c7 2a 45 44 bb        ------------------第三组
AA C6 30 30 30 31 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 32 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2A 45 45 BB   --------第四组

AA C6 30 30 30 31 30 30 30 30 30 30 30 31 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 32 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2A 45 45 BB   ---------第五组
在论坛中和其它网站找到几种CRC校检工具,不知道何故校出来的结果各不相同,自己本身就不知道什么校检,求高手能看出来这个是用什么校检的吗?为什么CRC校检的结果各不相同呢?
几个小CRC工具打包如下:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 2013-1-6 21:55:29 | 显示全部楼层
/* Copyright (c) 2002, 2003, 2004  Marek Michalkiewicz
   Copyright (c) 2005, Joerg Wunsch
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.

   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE. */

/* $Id: crc16.h,v 1.2.2.1 2006/04/19 20:35:54 joerg_wunsch Exp $ */

#ifndef _UTIL_CRC16_H_
#define _UTIL_CRC16_H_

#include <stdint.h>

/** \defgroup util_crc <util/crc16.h>: CRC Computations
    \code#include <util/crc16.h>\endcode

    This header file provides a optimized inline functions for calculating
    cyclic redundancy checks (CRC) using common polynomials.

    \par References:

    \par

    See the Dallas Semiconductor app note 27 for 8051 assembler example and
    general CRC optimization suggestions. The table on the last page of the
    app note is the key to understanding these implementations.

    \par

    Jack Crenshaw's "Implementing CRCs" article in the January 1992 isue of \e
    Embedded \e Systems \e Programming. This may be difficult to find, but it
    explains CRC's in very clear and concise terms. Well worth the effort to
    obtain a copy.

    A typical application would look like:

    \code
    // Dallas iButton test vector.
    uint8_t serno[] = { 0x02, 0x1c, 0xb8, 0x01, 0, 0, 0, 0xa2 };

    int
    checkcrc(void)
    {
        uint8_t crc = 0, i;

        for (i = 0; i < sizeof serno / sizeof serno[0]; i++)
            crc = _crc_ibutton_update(crc, serno);

        return crc; // must be 0
    }
    \endcode
*/

/** \ingroup util_crc
    Optimized CRC-16 calculation.

    Polynomial: x^16 + x^15 + x^2 + 1 (0xa001)<br>
    Initial value: 0xffff

    This CRC is normally used in disk-drive controllers.

    The following is the equivalent functionality written in C.

    \code
    uint16_t
    crc16_update(uint16_t crc, uint8_t a)
    {
        int i;

        crc ^= a;
        for (i = 0; i < 8; ++i)
        {
            if (crc & 1)
                crc = (crc >> 1) ^ 0xA001;
            else
                crc = (crc >> 1);
        }

        return crc;
    }

    \endcode */

static __inline__ uint16_t
_crc16_update(uint16_t __crc, uint8_t __data)
{
        uint8_t __tmp;
        uint16_t __ret;

        __asm__ __volatile__ (
                "eor %A0,%2" "\n\t"
                "mov %1,%A0" "\n\t"
                "swap %1" "\n\t"
                "eor %1,%A0" "\n\t"
                "mov __tmp_reg__,%1" "\n\t"
                "lsr %1" "\n\t"
                "lsr %1" "\n\t"
                "eor %1,__tmp_reg__" "\n\t"
                "mov __tmp_reg__,%1" "\n\t"
                "lsr %1" "\n\t"
                "eor %1,__tmp_reg__" "\n\t"
                "andi %1,0x07" "\n\t"
                "mov __tmp_reg__,%A0" "\n\t"
                "mov %A0,%B0" "\n\t"
                "lsr %1" "\n\t"
                "ror __tmp_reg__" "\n\t"
                "ror %1" "\n\t"
                "mov %B0,__tmp_reg__" "\n\t"
                "eor %A0,%1" "\n\t"
                "lsr __tmp_reg__" "\n\t"
                "ror %1" "\n\t"
                "eor %B0,__tmp_reg__" "\n\t"
                "eor %A0,%1"
                : "=r" (__ret), "=d" (__tmp)
                : "r" (__data), "0" (__crc)
                : "r0"
        );
        return __ret;
}

/** \ingroup util_crc
    Optimized CRC-XMODEM calculation.

    Polynomial: x^16 + x^12 + x^5 + 1 (0x1021)<br>
    Initial value: 0x0

    This is the CRC used by the Xmodem-CRC protocol.

    The following is the equivalent functionality written in C.

    \code
    uint16_t
    crc_xmodem_update (uint16_t crc, uint8_t data)
    {
        int i;

        crc = crc ^ ((uint16_t)data << 8);
        for (i=0; i<8; i++)
        {
            if (crc & 0x8000)
                crc = (crc << 1) ^ 0x1021;
            else
                crc <<= 1;
        }

        return crc;
    }
    \endcode */

static __inline__ uint16_t
_crc_xmodem_update(uint16_t __crc, uint8_t __data)
{
    uint16_t __ret;             /* %B0:%A0 (alias for __crc) */
    uint8_t __tmp1;             /* %1 */
    uint8_t __tmp2;             /* %2 */
                                /* %3  __data */

    __asm__ __volatile__ (
        "eor    %B0,%3"          "\n\t" /* crc.hi ^ data */
        "mov    __tmp_reg__,%B0" "\n\t"
        "swap   __tmp_reg__"     "\n\t" /* swap(crc.hi ^ data) */

        /* Calculate the ret.lo of the CRC. */
        "mov    %1,__tmp_reg__"  "\n\t"
        "andi   %1,0x0f"         "\n\t"
        "eor    %1,%B0"          "\n\t"
        "mov    %2,%B0"          "\n\t"
        "eor    %2,__tmp_reg__"  "\n\t"
        "lsl    %2"              "\n\t"
        "andi   %2,0xe0"         "\n\t"
        "eor    %1,%2"           "\n\t" /* __tmp1 is now ret.lo. */

        /* Calculate the ret.hi of the CRC. */
        "mov    %2,__tmp_reg__"  "\n\t"
        "eor    %2,%B0"          "\n\t"
        "andi   %2,0xf0"         "\n\t"
        "lsr    %2"              "\n\t"
        "mov    __tmp_reg__,%B0" "\n\t"
        "lsl    __tmp_reg__"     "\n\t"
        "rol    %2"              "\n\t"
        "lsr    %B0"             "\n\t"
        "lsr    %B0"             "\n\t"
        "lsr    %B0"             "\n\t"
        "andi   %B0,0x1f"        "\n\t"
        "eor    %B0,%2"          "\n\t"
        "eor    %B0,%A0"         "\n\t" /* ret.hi is now ready. */
        "mov    %A0,%1"          "\n\t" /* ret.lo is now ready. */
        : "=d" (__ret), "=d" (__tmp1), "=d" (__tmp2)
        : "r" (__data), "0" (__crc)
        : "r0"
    );
    return __ret;
}

/** \ingroup util_crc
    Optimized CRC-CCITT calculation.

    Polynomial: x^16 + x^12 + x^5 + 1 (0x8408)<br>
    Initial value: 0xffff

    This is the CRC used by PPP and IrDA.

    See RFC1171 (PPP protocol) and IrDA IrLAP 1.1

    \note Although the CCITT polynomial is the same as that used by the Xmodem
    protocol, they are quite different. The difference is in how the bits are
    shifted through the alorgithm. Xmodem shifts the MSB of the CRC and the
    input first, while CCITT shifts the LSB of the CRC and the input first.

    The following is the equivalent functionality written in C.

    \code
    uint16_t
    crc_ccitt_update (uint16_t crc, uint8_t data)
    {
        data ^= lo8 (crc);
        data ^= data << 4;

        return ((((uint16_t)data << 8) | hi8 (crc)) ^ (uint8_t)(data >> 4)
                ^ ((uint16_t)data << 3));
    }
    \endcode */

static __inline__ uint16_t
_crc_ccitt_update (uint16_t __crc, uint8_t __data)
{
    uint16_t __ret;

    __asm__ __volatile__ (
        "eor    %A0,%1"          "\n\t"

        "mov    __tmp_reg__,%A0" "\n\t"
        "swap   %A0"             "\n\t"
        "andi   %A0,0xf0"        "\n\t"
        "eor    %A0,__tmp_reg__" "\n\t"

        "mov    __tmp_reg__,%B0" "\n\t"

        "mov    %B0,%A0"         "\n\t"

        "swap   %A0"             "\n\t"
        "andi   %A0,0x0f"        "\n\t"
        "eor    __tmp_reg__,%A0" "\n\t"

        "lsr    %A0"             "\n\t"
        "eor    %B0,%A0"         "\n\t"

        "eor    %A0,%B0"         "\n\t"
        "lsl    %A0"             "\n\t"
        "lsl    %A0"             "\n\t"
        "lsl    %A0"             "\n\t"
        "eor    %A0,__tmp_reg__"

        : "=d" (__ret)
        : "r" (__data), "0" (__crc)
        : "r0"
    );
    return __ret;
}

/** \ingroup util_crc
    Optimized Dallas (now Maxim) iButton 8-bit CRC calculation.

    Polynomial: x^8 + x^5 + x^4 + 1 (0x8C)<br>
    Initial value: 0x0

    See http://www.maxim-ic.com/appnotes.cfm/appnote_number/27

    The following is the equivalent functionality written in C.

    \code
    uint8_t
    _crc_ibutton_update(uint8_t crc, uint8_t data)
    {
        uint8_t i;

        crc = crc ^ data;
        for (i = 0; i < 8; i++)
        {
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0x8C;
            else
                crc >>= 1;
        }

        return crc;
    }
    \endcode
*/

static __inline__ uint8_t
_crc_ibutton_update(uint8_t __crc, uint8_t __data)
{
        uint8_t __i, __pattern;
        __asm__ __volatile__ (
                "        eor        %0, %4" "\n\t"
                "        ldi        %1, 8" "\n\t"
                "        ldi        %2, 0x8C" "\n\t"
                "1:        bst        %0, 0" "\n\t"
                "        lsr        %0" "\n\t"
                "        brtc        2f" "\n\t"
                "        eor        %0, %2" "\n\t"
                "2:        dec        %1" "\n\t"
                "        brne        1b" "\n\t"
                : "=r" (__crc), "=d" (__i), "=d" (__pattern)
                : "0" (__crc), "r" (__data));
        return __crc;
}

#endif /* _UTIL_CRC16_H_ */

看看AVRGCC的CRC头文件

出0入0汤圆

 楼主| 发表于 2013-1-7 06:39:19 | 显示全部楼层
因为自己的愚笨还不会CRC这样的方法,所以用了上面的工具想尝试谁知道几个工具用同一种方法算出来的结果都不一样,现在看CRC方法还是没看明白。
非常感谢楼上。

出0入0汤圆

发表于 2013-1-7 09:02:42 | 显示全部楼层
本帖最后由 dr2001 于 2013-1-7 09:11 编辑
hisun 发表于 2013-1-7 06:39
因为自己的愚笨还不会CRC这样的方法,所以用了上面的工具想尝试谁知道几个工具用同一种方法算出来的结果都 ...


Google这个英文文档“A Painless Guide To CRC Error Detection Algorithms”。

一个具体的CRC算法,由以下所有的参数共同决定:
1、CRC宽度,就是常说的CRC-N的N。
2、CRC多项式是什么。
3、CRC寄存器的初始值。
4、输入的字节流是否要反序。
5、CRC结果是否要反序。
6、CRC结果异或的值。
这就是你贴的CRC-Calc软件中提示的一系列参数的含义。每个具体的CRC名称,对应一系列确定的上述参数。

以上内容只要有一个东西不一样,那么计算结果就完全不同。因为CRC实质上是一种Hash。

反向CRC比较麻烦。
首先,你必须保证人家用的算法一定是标准CRC算法,只要有一点不一样都不行。
然后,可以用软件进行推断,但是数据量要有,而且数据字段和CRC结果要人工分析清楚。

反向CRC可以Google这个软件,CRC RevEng, an arbitrary-precision CRC calculator and algorithm finder。

出0入0汤圆

发表于 2013-1-7 09:04:29 | 显示全部楼层
CRC我也一直没搞懂,也找了很多资料,最后根据网上的一个CRC-16的校验方法,用Fx3G的PLC写出的程序,校验是正确的。现在把这个计算方法贴出来,你研究一下呢


CRC-16校验码计算方法:
常用查表法和计算法。计算方法一般都是:
(1)、预置1个16位的寄存器为十六进制FFFF(即全为1),称此寄存器为CRC寄存器;
(2)、把第一个8位二进制数据(既通讯信息帧的第一个字节)与16位的CRC寄存器的低
       8位相异或,把结果放于CRC寄存器,高八位数据不变;
(3)、把CRC寄存器的内容右移一位(朝低位)用0填补最高位,并检查右移后的移出位;
(4)、如果移出位为0:重复第3步(再次右移一位);如果移出位为1,CRC寄存器与多
    项式A001(1010 0000 0000 0001)进行异或;
(5)、重复步骤3和4,直到右移8次,这样整个8位数据全部进行了处理;
(6)、重复步骤2到步骤5,进行通讯信息帧下一个字节的处理;
(7)、将该通讯信息帧所有字节按上述步骤计算完成后,得到的16位CRC寄存器的高、低
       字节进行交换;
(8)、最后得到的CRC寄存器内容即为:CRC码。
以上计算步骤中的多项式A001是8005按位颠倒后的结果。
查表法是将移位异或的计算结果做成了一个表,就是将0~256放入一个长度为16位的寄存器中的低八位,高八位填充0,然后将该寄存器与多项式0XA001按照上述3、4步骤,直到八位全部移出,最后寄存器中的值就是表格中的数据,高八位、低八位分别单独一个表。

出0入0汤圆

发表于 2013-1-7 09:14:03 | 显示全部楼层
dlmaowf 发表于 2013-1-7 09:04
CRC我也一直没搞懂,也找了很多资料,最后根据网上的一个CRC-16的校验方法,用Fx3G的PLC写出的程序,校验是 ...

参考我上边的回复。

你提到的算法是Modbus用的16Bit CRC。
width=16  poly=0x8005  init=0xffff  refin=true  refout=true  xorout=0x0000  check=0x4b37  name="MODBUS"

实际应用中有很多16Bit的CRC,各不相同。

出0入0汤圆

发表于 2013-1-7 09:27:10 | 显示全部楼层
楼上高人,确实是MODBUS的CRC校验。谢谢,我好好研究一下

出0入0汤圆

 楼主| 发表于 2013-1-7 17:27:33 | 显示全部楼层
现在感觉它不象是用了CRC这样的计算方法,我调动参数发送到单片机后检测到的尾数中2a 45 XX bb会变化,现在基本上知道2a之前发除了开头第一第二字节外全都是数据,按说用了CRC这样的计算那么校检数XX重合的可能比较小,但用一个一个数据调动重合的机率比较大,单独调前一个参数或后一个参数就有很多都是重合了,从数据量最少的来说:
aa c1 2a 45 42 bb         ----------------第一组
aa c7 2a 45 44 bb        ------------------第二组
仅两组中的2a 45 XX bb 就和尝试多次发一百多个数据包的校检结尾很多都相同,加来加去还没搞清楚这家伙用的是什么计算方法。从自己目前所得到的数据来看,唯一怀疑的就只有那个XX了,难道说他们真是定义的加多少减多少这种不怕麻烦的方式啊?下面三组是我调动参数后检测到的数据:
2.1
AA C6 30 30 32 31 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 32 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2A 45 43 BB

10.0
AA C6 30 31 30 30 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 32 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2A 45 45 BB

1.3
AA C6 30 30 30 31 30 30 30 30 30 30 30 31 30 30 30 30 30 30 31 33 30 30 30 30 30 30 30 31 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 32 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 2A 45 44 BB
感觉跟字节有很大关系,不管是哪个只要是字节内容相同,那么2A 45 XX BB就就会相同,求问都有哪些跟字节相关的校检方式?非常感谢了!

出0入264汤圆

发表于 2013-1-7 21:18:30 来自手机 | 显示全部楼层
呵呵,这是要逆向别人的产品么?透漏一下是什么产品,这样去研究说不定更有效,不然你就扔一堆数据出来,大家没法分析啊。

出0入0汤圆

发表于 2013-1-7 22:15:40 | 显示全部楼层
本帖最后由 eduhf_123 于 2013-1-7 22:17 编辑

协议格式:
帧头,命令,[数据,]结束符1,结束符2,校验和,帧尾

其中:
“帧头”定义为0xAA;
“命令”可能为0xCX的形式;
“数据”为可选部分;
“结束符1”为'*',即0x2A;
“结束符2”为'E',即0x45;
“校验和”为整个帧的字节校验数据,目前有点眉目了;
“帧尾”定义为0xBB。

mcu_lover 发表于 2013-1-7 21:18
呵呵,这是要逆向别人的产品么?透漏一下是什么产品,这样去研究说不定更有效,不然你就扔一堆数据出来,大家没法分析啊。
如9楼所说,希望LZ透漏更多项目信息,不然我不会公布校验数据的算法的。

出0入0汤圆

 楼主| 发表于 2013-1-8 10:02:30 | 显示全部楼层
不是逆把别人的东西,是原来的人找不到了,现在的数据范围要放大上位已经到了极限所有要从写个,就是简单地把要改的一些参数写到单片机里,因为是别人的成品不好具体讲出来,基本上就是个向电机输出电压电流范围的东西,下位机是用滴胶封闭,我也不知道对方也只有很早前的的一份烧写文件。协议格式就是如楼上所说的一样,
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-21 18:17

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表