crc8的js实现(crc8代码)

  • 1、求并行 CRC8 的 verilog 代码,多项式为 X8+X5+X4+X3+1(0x39)
  • 2、用C语言编写,crc8校验9个字节的数据,生成多项式为x8+x2+x+1,
  • 3、CRC校验的算法有几种?
  • 4、如何用java实现CRC8验证算法
  • 5、CAN总线通信里的这个字符的CRC运算是CRC8,还是CRC16还是CRC32运算?
  • 6、nodejs 怎么执行 crc32

在线生成工具

这个生成的是function,不可综合的。自己改成module就行了

////////////////////////////////////////////////////////////////////////////////

// Copyright (C) 1999-2008 Easics NV.

// This source file may be used and distributed without restriction

// provided that this copyright statement is not removed from the file

// and that any derivative work contains the original copyright notice

// and the associated disclaimer.

//

// THIS SOURCE FILE IS PROVIDED “AS IS” AND WITHOUT ANY EXPRESS

// OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED

// WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

//

// Purpose : synthesizable CRC function

// * polynomial: (0 3 4 5 8)

// * data width: 8

//

// Info : tools@easics.be

//

////////////////////////////////////////////////////////////////////////////////

module CRC8_D8;

// polynomial: (0 3 4 5 8)

// data width: 8

// convention: the first serial bit is D[7]

function [7:0] nextCRC8_D8;

input [7:0] Data;

input [7:0] crc;

reg [7:0] d;

reg [7:0] c;

reg [7:0] newcrc;

begin

d = Data;

c = crc;

newcrc[0] = d[6] ^ d[5] ^ d[4] ^ d[3] ^ d[0] ^ c[0] ^ c[3] ^ c[4] ^ c[5] ^ c[6];

newcrc[1] = d[7] ^ d[6] ^ d[5] ^ d[4] ^ d[1] ^ c[1] ^ c[4] ^ c[5] ^ c[6] ^ c[7];

newcrc[2] = d[7] ^ d[6] ^ d[5] ^ d[2] ^ c[2] ^ c[5] ^ c[6] ^ c[7];

newcrc[3] = d[7] ^ d[5] ^ d[4] ^ d[0] ^ c[0] ^ c[4] ^ c[5] ^ c[7];

newcrc[4] = d[4] ^ d[3] ^ d[1] ^ d[0] ^ c[0] ^ c[1] ^ c[3] ^ c[4];

newcrc[5] = d[6] ^ d[3] ^ d[2] ^ d[1] ^ d[0] ^ c[0] ^ c[1] ^ c[2] ^ c[3] ^ c[6];

newcrc[6] = d[7] ^ d[4] ^ d[3] ^ d[2] ^ d[1] ^ c[1] ^ c[2] ^ c[3] ^ c[4] ^ c[7];

newcrc[7] = d[5] ^ d[4] ^ d[3] ^ d[2] ^ c[2] ^ c[3] ^ c[4] ^ c[5];

nextCRC8_D8 = newcrc;

end

endfunction

endmodule

// 8bit CRC (X(8) + X(2) + X(1) + 1)

typedef unsigned long  DWORD;

typedef unsigned short WORD;

typedef unsigned char  BYTE;

#define AL2_FCS_COEF ((1  7) + (1  6) + (1  5))

//data为指向校验数据的指针,length为长度,返回一个字节的校验码

BYTE GetCrc8(unsigned char * data, int length)

{

   BYTE cFcs = 0;

   int i, j;

   for( i = 0; i  length; i ++ )

   {

      cFcs ^= data[i];

      for(j = 0; j  8; j ++)

      {

         if(cFcs  1)

         {

            cFcs = (BYTE)((cFcs  1) ^ AL2_FCS_COEF);

         }

         else

         {

            cFcs = 1;

         }

      }

   }

   return cFcs;

}

具体的找些参考资料吧,一两句话也说不清楚

;hl=zh-CNct=clnkcd=1gl=cnst_usg=ALhdy2_AJ0YjhnCWFMb5xtYPvXTzPgd_2Q

下面的还有程序实现:

/*

*—————————————————————————

* Copyright (C) 1999,2000 Dallas Semiconductor Corporation, All Rights Reserved.

*

* Permission is hereby granted, free of charge, to any person obtaining a

* copy of this software and associated documentation files (the “Software”),

* to deal in the Software without restriction, including without limitation

* the rights to use, copy, modify, merge, publish, distribute, sublicense,

* and/or sell copies of the Software, and to permit persons to whom the

* Software is furnished to do so, subject to the following conditions:

*

* The above copyright notice and this permission notice shall be included

* in all copies or substantial portions of the Software.

*

* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS

* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

* IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES

* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

* OTHER DEALINGS IN THE SOFTWARE.

*

* Except as contained in this notice, the name of Dallas Semiconductor

* shall not be used except as stated in the Dallas Semiconductor

* Branding Policy.

*—————————————————————————

*/

package com.dalsemi.onewire.utils;

/**

* CRC8 is a class to contain an implementation of the

* Cyclic-Redundency-Check CRC8 for the iButton. The CRC8 is used

* in the 1-Wire Network address of all iButtons and 1-Wire

* devices.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @version 0.00, 28 Aug 2000

* @author DS

*

*/

public class CRC8

{

//——–

//——– Variables

//——–

/**

* CRC 8 lookup table

*/

private static byte dscrc_table [];

/*

* Create the lookup table

*/

static

{

//Translated from the assembly code in iButton Standards, page 129.

dscrc_table = new byte [256];

int acc;

int crc;

for (int i = 0; i 256; i++)

{

acc = i;

crc = 0;

for (int j = 0; j 8; j++)

{

if (((acc ^ crc) 0x01) == 0x01)

{

crc = ((crc ^ 0x18) 1) | 0x80;

}

else

crc = crc 1;

acc = acc 1;

}

dscrc_table [i] = ( byte ) crc;

}

}

//——–

//——– Constructor

//——–

/**

* Private constructor to prevent instantiation.

*/

private CRC8 ()

{

}

//——–

//——– Methods

//——–

/**

* Perform the CRC8 on the data element based on the provided seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc data element on which to perform the CRC8

* @param seed seed the CRC8 with this value

* @return CRC8 value

*/

public static int compute (int dataToCRC, int seed)

{

return (dscrc_table [(seed ^ dataToCRC) 0x0FF] 0x0FF);

}

/**

* Perform the CRC8 on the data element based on a zero seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc data element on which to perform the CRC8

* @return CRC8 value

*/

public static int compute (int dataToCRC)

{

return (dscrc_table [dataToCRC 0x0FF] 0x0FF);

}

/**

* Perform the CRC8 on an array of data elements based on a

* zero seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @return CRC8 value

*/

public static int compute (byte dataToCrc [])

{

return compute(dataToCrc, 0, dataToCrc.length);

}

/**

* Perform the CRC8 on an array of data elements based on a

* zero seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @param off offset into array

* @param len length of data to crc

* @return CRC8 value

*/

public static int compute (byte dataToCrc [], int off, int len)

{

return compute(dataToCrc, off, len, 0);

}

/**

* Perform the CRC8 on an array of data elements based on the

* provided seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @param off offset into array

* @param len length of data to crc

* @param seed seed to use for CRC8

* @return CRC8 value

*/

public static int compute (byte dataToCrc [], int off, int len, int seed)

{

// loop to do the crc on each data element

int CRC8 = seed;

for (int i = 0; i len; i++)

CRC8 = dscrc_table [(CRC8 ^ dataToCrc [i + off]) 0x0FF];

return (CRC8 0x0FF);

}

/**

* Perform the CRC8 on an array of data elements based on the

* provided seed.

* p

* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.

*

* @param dataToCrc array of data elements on which to perform the CRC8

* @param seed seed to use for CRC8

* @return CRC8 value

*/

public static int compute (byte dataToCrc [], int seed)

{

return compute(dataToCrc, 0, dataToCrc.length, seed);

}

}

CAN总线的CRC场共16bit,其中最后1bit为CRC序列后随的CRC界定符仅由单个“隐性”位构成。所以用于CRC校验的是15bit即CRC15。用于帧校验的CRC序列由特别适用于位数小于127位帧的循环冗余码校验(BCH码)驱动。为实现CRC计算,被除的多项式被定义为这样一个多项式,其系数由帧起始、仲裁场、控制场、数据场(如果存在)和15位最低系数为0组成的解除填充的位流给定。此多项式被下列生成多项式

X15+X14+X10+X8+X7+X4+X3+1

除(系数按模-2计算),该多项式相除的余数即为发至总线的CRC序列。

为实现这一功能,可以使用15位移位寄存器CRC-RG(14:0)。若NXTBIT标示由帧起始直至数据场结束的解除填充位序列给定位流的下一位,则CRC序列计算如下:

CRC—RG(14:0)=(0); //初始化移位寄存器

REPEAT

CRCNXT=NXTBIT EXOR CRC—RG(14);

CRC—RG(14:1)= CRC—RG(13:0); //左移一位

CRC—RG(0)=0;

IF CRCNXT THEN

CRC—RG(14:0)=CRC—RG(14:0)EXOR(4599H);

END IF

UNTIL(NXTBIT=数据结束或存在一个错误条件)。

数据场最后一位发送/接收后,CRC—RG中含有CRC序列。CRC序列后随的CRC界定符仅由单个“隐性”位构成。

const crc=require(‘node-crc’);

var result = crc.crc32(Buffer.from(‘hello’, ‘utf8’)).toString(‘hex’);

var result2 = crc.crc64(Buffer.from(‘world’, ‘utf8’)).toString(‘hex’);

链接在这里:网页链接

你品,你细品

原创文章,作者:XZB0O,如若转载,请注明出处:https://www.506064.com/n/126798.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
XZB0O的头像XZB0O
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相关推荐

发表回复

登录后才能评论