实际应用时,发送装置计算出CRC值并随数据一同发送给接收装置,接收装置对收到的数据重新计算CRC并与收到的CRC相比较,若两个CRC值不同,则说明数据通讯出现错误。
unsigned short crc_dsp(unsigned short reg, unsigned char data_crc)//reg为crc寄存器, data_crc为将要处理的8bit数据流
{
unsigned short msb//crc寄存器将移出的最高1bit
unsigned short data
unsigned short gx = 0x8005, i = 0//i为左移次数, gx为生成多项式
data = (unsigned short)data_crc
data = data <<8
reg = reg ^ data
do
{
msb = reg &0x8000
reg = reg <<1
if(msb == 0x8000)
{
reg = reg ^ gx
}
i++
}
while(i <8)
return (reg)
}
以下是我的分析,不知是否正确,你参考下1、首先来看你打java代码:crc=(byte)((crc>>1)^0x8c)和 crc=(byte)(crc>>1)导致这个问题是因为byte的最高位符号位,转换的时候就出错了2、示例代码:package com.testpublic class test {public static void main(String[] args) {byte[] ptr = { 1, 1, 1, 1, 1, 1 }byte res = getCrc(ptr)System.out.println()System.out.println((byte)( (1 >>1) ^ 0x8c ) + ":" +( (1 >>1) ^ 0x8c ) )}public static byte getCrc(byte[] ptr) {int crc = 0for (int i = 0i >1) ^ 0x8c} else {crc = crc >>1}}}return (byte) crc}}