男人扒开女人腿做爽爽视频-日本一道人妻无码一区在线-天天爽夜夜爽夜夜爽-免费无码又爽又刺激毛片-久久国产乱子伦精品免费台湾

RFID超高頻讀寫器CRC16計算方法(.NET、C語言、Java語言、python版本)

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

一體機讀寫器/分體式讀寫器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電子標簽展示

聯系我們

廣東省東莞市中堂鎮潢涌工業橫路2號7棟8層

400-807-2289

daysr@qyswf.com

7*24小時服務

主站蜘蛛池模板: 在国产线视频a在线视频| 亚洲永久精品ww47永久入口 | 高清性欧美暴力猛交| 亚洲女女女同性video| 2019日韩中文字幕mv| 亚洲欧美日韩国产成人精品影院 | 天天躁日日躁狠狠躁性色av| 少妇真实被内射视频三四区| 色综合久久综合欧美综合网| 中国性少妇内射xxxx狠干| 欧美激情精品久久| 国产在线精品一区二区不卡麻豆| 免费看裸体???网站| 国色天香精品一卡2卡3卡4| 无码gogo大胆啪啪艺术| 亚洲人成色77777在线观看大战p| 国产96在线 | 欧美| 色欲av无码一区二区人妻| 久久国产精品视频| 亚洲中文字幕无码永久在线| 伦人伦xxxx国语对白| 久久精品国产中国久久| 欧美丰满熟妇乱xxxxx网站| 国产真实伦在线观看| 日韩~欧美一中文字幕| 7777精品伊人久久久大香线蕉 | 奇米影视第四色首页| 无码精品人妻一区二区三区老牛| 国产成人无码精品久久久性色| 人妻丰满熟妇aⅴ无码区| 别揉我奶头~嗯~啊~一区二区三区| 无码av高潮喷水无码专区线| 免费午夜无码18禁无码影院| 成人无码一区二区三区网站| 日本一区二区三区专线| 狠狠亚洲婷婷综合色香五月| 无套内谢少妇毛片aaaa片免费| 亚洲鲁丝片一区二区三区| 亚洲国内精品自在线影院牛牛| 久久精品私人影院免费看| av片在线观看|