RFID超高频读写器CRC16计算方法(.NET、C语言、Java语言、python版本)

2022-03-09 15:00:36 lhqcool 8665

一体机读写器/分体式读写器AT指令CRC校验方法,暂时只有C#版本,C语言版本,Java语言版本、python版本

适用型号:

LT-DS302 DS312 DS309 DS310,LT-DS814 DS818 DS8112 DS8116,LT-DS509 DS512,LT-DS322

C#版本CRC:

  public class CRC

    {

        private static int POLYNOMIAL = 0x8408;

        private static int PRESET_VALUE = 0xFFFF;


        public static int crc16(string hex)

        {

            byte[] data = HexStringToByteArray(hex);

            int current_crc_value = PRESET_VALUE;

            for (int i = 0; i < data.Length; i++)

            {

                current_crc_value ^= data[i] & 0xFF;

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

                {

                    if ((current_crc_value & 1) != 0)

                    {

                        current_crc_value = (current_crc_value >> 1) ^ POLYNOMIAL;

                    }

                    else

                    {

                        current_crc_value = current_crc_value >> 1;

                    }

                }

            }

            return current_crc_value;

        }

        //16进制数组字符串转换         

        private static byte[] HexStringToByteArray(string s)

        {

            s = s.Replace(" ", "");

            byte[] buffer = new byte[s.Length / 2];

            for (int i = 0; i < s.Length; i += 2)

                buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16);

            return buffer;

        }

    }


(询查模式应答格式)调用示例:

string input = "1500010301010C300833B2DDD90140000000002A80BE"//读写器AT指令返回值,也可用于指令发送校验CRC

int bytes = CRC.crc16(input);

// bytes=0;bytes等于0表示通过,其他值不通过!

(询查模式应答格式)拆分示例:

Len:15

Adr:00

reComd:01

Status:03

ant:01

Num:01

EPC/TID长度:0C

EPC号或TID数据:300833B2DDD9014000000000

RSSI值:2A

CRC-LSB:80

CRC-MSB:BE



实时模式输入格式也相似,Statuss是0xee 就是实时模式回传的数据。




C语言版本CRC:

CRC16的C语言算法:

#include #include #include #include #define PRESET_VALUE 0xFFFF

#define POLYNOMIAL  0x8408


typedef  unsigned char byte;


typedef union name{

    int len;

    unsigned char ucx;

} name;



unsigned int uiCrc16Cal(unsigned char const* pucY, unsigned char ucX)

{

    unsigned char ucI, ucJ;

    unsigned short int  uiCrcValue = PRESET_VALUE;

    for (ucI = 0; ucI < ucX; ucI++)

    {

        uiCrcValue = uiCrcValue ^ *(pucY + ucI);

        for (ucJ = 0; ucJ < 8; ucJ++)

        {

            if (uiCrcValue & 0x0001)

            {

                uiCrcValue = (uiCrcValue >> 1) ^ POLYNOMIAL;

            }

            else

            {

                uiCrcValue = (uiCrcValue >> 1);

            }

        }

    }

    return uiCrcValue;

}


void hexToBytes(const std::string& hex, byte* bytes){

    int bytelen = hex.length()/2;

    std::string strByte;

    unsigned int n;

    for(int i = 0; i < bytelen; i++){

        strByte = hex.substr(i*2, 2);

        sscanf(strByte.c_str(), "%x", &n);

        bytes[i]=n;

    }

}



int main()

{

    std::string hex="1500010101010ce28011700000020cc282e26d84d652"

    int bytelen=hex.length()/2;

    byte *ptr=new byte[bytelen];

    hexToBytes(hex, ptr);


    unsigned int ret = uiCrc16Cal(ptr, bytelen);

      

    std::cout << ret << std::endl;

    delete [] ptr;

    return 0;

}

说明:

pucY是要计算CRC16的字符数组的入口(需转换字节数组),ucX是字符数组中字符个数。

上位机收到数据的时候,只要把收到的数据按以上算法进行计算CRC16,结果为0x0000表明数据正确。

言版本:


package cn.longhaul.test;


/**

 *  CRC16的校验算法工具类

 */

public class Crc16Util {


/**

* 一个字节包含位的数量 8

*/

private static final int BITS_OF_BYTE = 8;

/**

* 多项式

*/

private static final int POLYNOMIAL = 0x8408;

/**

* 初始值

*/

private static final int INITIAL_VALUE = 0xFFFF;


/**

* CRC16 编码

*

* @param bytes 编码内容

* @return 编码结果

*/

public static int crc16(int[] bytes) {

int res = INITIAL_VALUE;

for (int data : bytes) {

res = res ^ data;

for (int i = 0; i < BITS_OF_BYTE; i++) {

res = (res & 0x0001) == 1 ? (res >> 1) ^ POLYNOMIAL : res >> 1;

}

}

return revert(res);

}


/**

* 翻转16位的高八位和低八位字节

*

* @param src 翻转数字

* @return 翻转结果

*/

private static int revert(int src) {

int lowByte = (src & 0xFF00) >> 8;

int highByte = (src & 0x00FF) << 8;

return lowByte | highByte;

}



/** 十六进制转为IntBytes

* @param s 十六进制串

* @return  int[] bytes

*/

public static int[] hexString2IntBytes(String s) {

int[] bytes;

bytes = new int[s.length() / 2];

for (int i = 0; i < bytes.length; i++) {

bytes[i] = (int) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);

}

return bytes;

}


public static void main(String[] args) throws Exception {

int[] data = Crc16Util.hexString2IntBytes("18010206E28011700000020A7D001A0701000600000000");

final int res = Crc16Util.crc16(data);

final String hex = Integer.toHexString(res);

System.out.print(hex);

}


}




python版本:

# -*-coding:utf-8-*-


#  多项式 0x8408

POLYNOMIAL = 0x8408

# 初始值为:0xFFFF

INITIAL_VALUE = 0xFFFF



def crc16(dataarray):

    datalength = int(len(dataarray) / 2)

    datalist = [None] * datalength

    index = 0

    try:

        for index in range(datalength):

            item = dataarray[index * 2:index * 2 + 2]

            datalist[index] = int(item, 16)

        res = INITIAL_VALUE

        for data in datalist:

            res = res ^ data

            for index in range(8):

                if res & 0x0001 == 1:

                    res >>= 1

                    res ^= POLYNOMIAL

                else:

                    res >>= 1

        lowbyte = (res & 0xFF00) >> 8

        highbyte = (res & 0x00FF) << 8

        res = lowbyte | highbyte

        return res

    except ValueError as err:

        print(u'第{0}个数据{1}输入有误'.format(index, datalist[index]).encode('utf-8'))

        print(err)



if __name__ == '__main__':

    data_string = '18FF0206E28011700000020A7D001A0702000800000000'  # 16进制输入的数据流

    print('数据 :"{0:s}"对应的CRC16检验码为:{1:04X}'.format(data_string, crc16(data_string)))

    data_string = '18010206E28011700000020A7D001A0701000600000000'  # 16进制输入的数据流

    print('数据 :"{0:s}"对应的CRC16检验码为:{1:04X}'.format(data_string, crc16(data_string)))

    data_string = '06FF010400'  # 16进制输入的数据流

    print('数据 :"{0:s}"对应的CRC16检验码为:{1:04X}'.format(data_string, crc16(data_string)))


因为专注,所以专业

广东灵天智能科技有限公司是一家rfid电子标签生产厂家,自成立以来一直致力于超高频RFID解决方案生产服务商拥有自主生产、研发、销售体系,其rfid电子标签,rfid打印机,超高频读写器等产品远销国内外。在惠州设有工厂担任生产部分工作,普及应用领域:电力、银行、钢铁、有色、零售、制造业、服装、物流、电商、汽车配件等其他...

rfid电子标签展示

联系我们

广东省东莞市大岭山镇杨屋东埔新村路116号东盛科技园二区B栋六楼

400-807-22589

daysr@qyswf.com

7*24小时服务