• lua 实现modebus16crc校验


    function And(num1,num2)
    	local tmp1 = num1
    	local tmp2 = num2
    	local ret = 0
    	local count = 0
    	repeat
    		local s1 = tmp1 % 2
    		local s2 = tmp2 % 2
    		if s1 == s2 and s1 == 1 then
    			ret = ret + 2^count
    		end
    		tmp1 = math.modf(tmp1/2)
    		tmp2 = math.modf(tmp2/2)
    		count = count + 1
    	until(tmp1 == 0 and tmp2 == 0)
    	return ret
    end
     
    function Xor(num1,num2)
    	local tmp1 = num1
    	local tmp2 = num2
    	local ret = 0
    	local count = 0
    	repeat
    		local s1 = tmp1 % 2
    		local s2 = tmp2 % 2
    		if s1 ~= s2 then
    			ret = ret + 2^count
    		end
    		tmp1 = math.modf(tmp1/2)
    		tmp2 = math.modf(tmp2/2)
    		count = count + 1
    	until(tmp1 == 0 and tmp2 == 0)
    	return ret
    end
     
    function bit_rshift(value,n)
    	value = math.modf(value / (2^n))
    	return value
    end
     
    function CRC16(arr)
    	local tmp = 0xffff
    	for i=1,#arr do
    		tmp = Xor(arr[i],tmp)
    		for j=1,8 do
    			local tmp1 = And(tmp,0x01)
    			if tmp1 == 1 then
    				tmp = bit_rshift(tmp,1)
    				tmp = Xor(tmp,0xa001)
    			else
    				tmp = bit_rshift(tmp,1)
    			end
    		end
    	end
    	local ret1 = (tmp % 256)
    	local ret2 = math.modf( tmp / 256)
    	return ret1,ret2
    end
     
     
    --调用
     
    arr[1] = 0x01
    arr[2] = 0x03
    arr[3] = 0x61
    arr[4] = 0x00
    arr[5] = 0x00
    arr[6] = 0x02
     
    print(CRC16(arr))
     
    --输出: 219	247  (DB F7)
    
  • 相关阅读:
    冲刺阶段九
    冲刺阶段八
    学习进度十一
    人月神话阅读笔记01
    单词统计续
    冲刺阶段七
    冲刺阶段六
    冲刺阶段五
    bzoj1570: [JSOI2008]Blue Mary的旅行
    bzoj 1690: [Usaco2007 Dec]奶牛的旅行
  • 原文地址:https://www.cnblogs.com/still-smile/p/14684897.html
Copyright © 2020-2023  润新知