编辑代码

local fanLevel = "F0";

--注册串口接收函数
apiSetCb("uart", function (data)
    sys.publish("RECV",data)--发布消息
end)

--将字符串转化成Hex数组
local function stringToHexArray(str)
   local hexArray = {}
   for i = 1, #str do
      local charCode = string.byte(str, i)
      table.insert(hexArray, charCode)
   end
   return hexArray
end

--将Hex数组转化成字符串
local function hexArrayToString(hexArray)
  local result = ""
  for i = 1, #hexArray do
    result = result .. string.char(hexArray[i])
  end
  return result
end

--计算二进制与或
function Xor(num1,num2)
    local tmp1 = num1
    local tmp2 = num2
    local str = ""
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 then
            str = "0"..str
        else
            str = "1"..str
        end
        tmp1 = math.modf(tmp1/2)
        tmp2 = math.modf(tmp2/2)
    until(tmp1 == 0 and tmp2 == 0)
    return tonumber(str,2)
end

--计算二进制与
local function And(num1,num2)
    local tmp1 = num1
    local tmp2 = num2
    local str = ""
    repeat
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 then
            if s1 == 1 then
                str = "1"..str
            else
                str = "0"..str
            end
        else
            str = "0"..str
        end
        tmp1 = math.modf(tmp1/2)
        tmp2 = math.modf(tmp2/2)
    until(tmp1 == 0 and tmp2 == 0)
    return tonumber(str,2)
end

--计算二进制或
local function Or(num1,num2)
    local tmp1 = num1
    local tmp2 = num2
    local str = ""
    repeat 
        local s1 = tmp1 % 2
        local s2 = tmp2 % 2
        if s1 == s2 then
            if s1 == 0 then
                str = "0"..str
            else
                str = "1"..str
            end
        else
            str = "1"..str
        end
        tmp1 = math.modf(tmp1/2)
        tmp2 = math.modf(tmp2/2)
    until(tmp1 == 0 and tmp2 == 0)
    return tonumber(str,2)
end

--计算CRC8
local function crc8(data, len)
	local crc = 0
	local index = 1
	while len > 0 do
		crc = Xor(crc,data[index])
    	index = index + 1
    	len = len - 1
    	for i = 1, 8 do
    		i = i + 1
    		if (And(crc,0x01) == 1) then
    			crc = Xor(crc // 2, 0x8C)
			else
				crc = crc // 2;
			end
			
    	end
	end

	return crc
end

--等待接收到消息再继续运行
sys.taskInit(function()
    while true do
        --等待消息,超时1000ms
        local r,udata = sys.waitUntil("RECV")
        if r then
        	local hexArray = stringToHexArray(udata)
        	if (And(hexArray[3], 0x01) == 0x01) then
        		if (fanLevel ~= "F1") then
        			fanLevel = "F1";
    				print("FAN = F1")
    			end
			elseif (And(hexArray[3], 0x02) == 0x02) then
				if (fanLevel ~= "F2") then
					fanLevel = "F2";
    				print("FAN = F2")
    			end
			elseif (And(hexArray[3], 0x04) == 0x04) then
				if (fanLevel ~= "F3") then
					fanLevel = "F3";
    				print("FAN = F3")
    			end
			elseif (And(hexArray[6], 0x01) == 0x01) then
				if (fanLevel ~= "FF") then
					fanLevel = "FF";
    				print("FAN = FF")
    			end
			end
			
			local sendArray = {0xA5, 0x86, 0x00, 0x00, 0x08, 0x00, 0x01, 0x01, 0xF3}
			sendArray[9] = crc8(sendArray, 8);
            local sendResult = apiSend("uart",hexArrayToString(sendArray))
        end
    end
end)