Browse Source

fix:780EPM,Air8000:uart.on注册回调函数不能放在while循环中

wjq 5 months ago
parent
commit
b445333701

+ 89 - 89
module/Air780EPM/demo/780EPM视频教程示例代码/25_RS485实战演练/Radar_485.lua

@@ -69,6 +69,95 @@ end
 --=============================================================
 --采集雷达数据
 local function get_radar_date()
+    --=============================================================
+    --注册串口事件回调
+    uart.on(uartid, "receive", function(id, len)
+        local s = ""
+        repeat --repeadt类似C语言的do…while循环,repeat重复执行循环,直到until指定条件为真
+            s = uart.read(id, len)
+            --log.info("uart", "receive", id, #s, s)
+            log.info("串口2", "receive", id, #s, s:toHex())
+            --log.info("第三个数string.byte(s,3)=", string.byte(s,3))
+            if #s > 0  then -- #s 是取字符串的长度,string.byte(s,3)==8判断师回复的那条命令
+                -- 如果传输二进制/十六进制数据, 部分字符不可见, 不代表没收到
+                -- 关于收发hex值,请查阅 https://doc.openluat.com/article/583
+                log.info("第三个数string.byte(s,3)=", string.byte(s,3))
+                local crc16=crc16(s,#s-2)
+                local crc16_high=crc16 >> 8
+                local crc16_low=crc16 & 0xFF
+                log.info("CRC校验高位",crc16_high,"CRC校验低位",crc16_low)
+                --打印其hex字符串形式
+                --数据转换为数值string.byte,用于crc校验
+                local receive_crc_high = string.byte(s,#s-1)
+                local receive_crc_low = string.byte(s,#s)
+                local receive_Switch_state = string.byte(s,#s-2)
+                log.info("接收到的CRC校验高位", "接收到的CRC校验低位", receive_crc_high, receive_crc_low)
+                log.info("uart", "receive", id, #s, s:toHex())
+                --=============================================================
+                --识别读取数据并将数据赋值给空高料高变量
+                if crc16_high==receive_crc_high and crc16_low==receive_crc_low and string.byte(s,3)==20 then--物位计的判断
+                --if crc16_high==receive_crc_high and crc16_low==receive_crc_low and string.byte(s,3)==16 then--判断10数据
+                    --注意,string.byte(s,3)==16,收到字符串的第3个数据,判断时要转换为十进制数,第三个数为0x10,十进制数为16--低功耗雷达
+                    --注意,string.byte(s,3)==16,收到字符串的第3个数据,判断时要转换为十进制数,第三个数为0x14,十进制数为20--四线制雷达
+                    log.info("crc检验通过数据合法")
+                    --先将字符串转为hex格式
+                    local hexStr, len = string.toHex(s) -- 返回值"3132",2,后面的2是长度
+                    --log.info("收到的16进制字符串=",hexStr,len)
+                    --print(hexStr,len) -- 将输出 3132
+                    --截取空高数据,并加上0x个前缀
+                    --local kong_high="0x"..string.sub(hexStr,7,14)--将0x拼接到字符串上,string.sub(hexStr,7,14)是一个数字算一个(低功耗雷达)
+                    local kong_high="0x"..string.sub(hexStr,11,18)--物位计
+                    --截取料高数据,并加上0x个前缀
+                    --local liao_high="0x"..string.sub(hexStr,15,22)--将0x拼接到字符串上(低功耗雷达)
+                    local liao_high="0x"..string.sub(hexStr,19,26)--物位计
+                    --信噪比
+                    --local SNR_Date="0x"..string.sub(hexStr,23,30)--将0x拼接到字符串上(低功耗雷达)
+                    local SNR_Date="0x"..string.sub(hexStr,27,34)--物位计
+                    --雷达温度
+                    --local temp_Date="0x"..string.sub(hexStr,31,38)--将0x拼接到字符串上(低功耗雷达)
+                    local temp_Date="0x"..string.sub(hexStr,35,42)--物位计
+                    log.info("空高16进制字符串=",kong_high)
+                    log.info("料高16进制字符串=",liao_high)
+                    log.info("信噪比16进制字符串=",SNR_Date)
+                    log.info("雷达温度比16进制字符串=",temp_Date)
+                    --将hex格式转化为浮点型数据
+                    local kong_temp = string.pack("<L",kong_high)
+                    kong_Value_485 = string.unpack("f",kong_temp)
+                    log.info("空高kong_Value_485=",kong_Value_485)
+                    local liao_temp = string.pack("<L",liao_high)
+                    liao_Value_485= string.unpack("f",liao_temp)
+                    log.info("料高liao_Value_485=",liao_Value_485)
+                    local SNR_temp = string.pack("<L",SNR_Date)
+                    SNR_485 = string.unpack("f",SNR_temp)
+                    log.info("信噪比SNR_485=",SNR_485)
+                    local xinhao_temp = string.pack("<L",temp_Date)
+                    Radar_temp_485 = string.unpack("f",xinhao_temp)
+                    log.info("雷达温度Radar_temp_485=",Radar_temp_485)                               
+                    --uart.write(1, s)--正常采集不透传给串口1,也就是传给蓝牙
+                    log.info("发布雷达完成消息", "receive", id, #s, s)
+                    sys.publish("雷达完成")
+                --=============================================================
+                --如果不是采集命令,则透传给串口1
+                elseif crc16_high==receive_crc_high and crc16_low==receive_crc_low then
+                    log.info("串口2透传给串口1数据", "receive", id, #s, s)
+                    -- log.info("uart", "receive", id, #s, s:toHex())
+                    uart.write(1, s)--暂时不透传给串口1
+                elseif crc16_high~=receive_crc_high or crc16_low~=receive_crc_low then
+                    log.info("无数据或crc检验不通过")
+                    kong_Value_485=-100--空高
+                    liao_Value_485=-100--料高
+                    uart.write(1, s)--暂时不透传给串口1
+                    --两种情况,一种是采集失败,一种是采集回波曲线
+                    end
+            end
+            if #s == len then
+                --log.info("程序经过这里1")
+                break
+            end
+        until s == ""
+        --log.info("程序经过这里2")
+    end)
+    --=============================================================
     while true do
         --sys.waitUntil("读雷达")
         log.info("读485空高数值")
@@ -77,95 +166,6 @@ local function get_radar_date()
         -- log.info("串口发送完成",Read_Radar_Date)
         uart.write(uartid,Read_4_Date)
         log.info("485雷达采集发送完成",Read_4_Date)
-        --=============================================================
-        --注册串口事件回调
-        uart.on(uartid, "receive", function(id, len)
-            local s = ""
-            repeat --repeadt类似C语言的do…while循环,repeat重复执行循环,直到until指定条件为真
-                s = uart.read(id, len)
-                --log.info("uart", "receive", id, #s, s)
-                log.info("串口2", "receive", id, #s, s:toHex())
-                --log.info("第三个数string.byte(s,3)=", string.byte(s,3))
-                if #s > 0  then -- #s 是取字符串的长度,string.byte(s,3)==8判断师回复的那条命令
-                    -- 如果传输二进制/十六进制数据, 部分字符不可见, 不代表没收到
-                    -- 关于收发hex值,请查阅 https://doc.openluat.com/article/583
-                    log.info("第三个数string.byte(s,3)=", string.byte(s,3))
-                    local crc16=crc16(s,#s-2)
-                    local crc16_high=crc16 >> 8
-                    local crc16_low=crc16 & 0xFF
-                    log.info("CRC校验高位",crc16_high,"CRC校验低位",crc16_low)
-                    --打印其hex字符串形式
-                    --数据转换为数值string.byte,用于crc校验
-                    local receive_crc_high = string.byte(s,#s-1)
-                    local receive_crc_low = string.byte(s,#s)
-                    local receive_Switch_state = string.byte(s,#s-2)
-                    log.info("接收到的CRC校验高位", "接收到的CRC校验低位", receive_crc_high, receive_crc_low)
-                    log.info("uart", "receive", id, #s, s:toHex())
-                    --=============================================================
-                    --识别读取数据并将数据赋值给空高料高变量
-                    if crc16_high==receive_crc_high and crc16_low==receive_crc_low and string.byte(s,3)==20 then--物位计的判断
-                    --if crc16_high==receive_crc_high and crc16_low==receive_crc_low and string.byte(s,3)==16 then--判断10数据
-                        --注意,string.byte(s,3)==16,收到字符串的第3个数据,判断时要转换为十进制数,第三个数为0x10,十进制数为16--低功耗雷达
-                        --注意,string.byte(s,3)==16,收到字符串的第3个数据,判断时要转换为十进制数,第三个数为0x14,十进制数为20--四线制雷达
-                        log.info("crc检验通过数据合法")
-                        --先将字符串转为hex格式
-                        local hexStr, len = string.toHex(s) -- 返回值"3132",2,后面的2是长度
-                        --log.info("收到的16进制字符串=",hexStr,len)
-                        --print(hexStr,len) -- 将输出 3132
-                        --截取空高数据,并加上0x个前缀
-                        --local kong_high="0x"..string.sub(hexStr,7,14)--将0x拼接到字符串上,string.sub(hexStr,7,14)是一个数字算一个(低功耗雷达)
-                        local kong_high="0x"..string.sub(hexStr,11,18)--物位计
-                        --截取料高数据,并加上0x个前缀
-                        --local liao_high="0x"..string.sub(hexStr,15,22)--将0x拼接到字符串上(低功耗雷达)
-                        local liao_high="0x"..string.sub(hexStr,19,26)--物位计
-                        --信噪比
-                        --local SNR_Date="0x"..string.sub(hexStr,23,30)--将0x拼接到字符串上(低功耗雷达)
-                        local SNR_Date="0x"..string.sub(hexStr,27,34)--物位计
-                        --雷达温度
-                        --local temp_Date="0x"..string.sub(hexStr,31,38)--将0x拼接到字符串上(低功耗雷达)
-                        local temp_Date="0x"..string.sub(hexStr,35,42)--物位计
-                        log.info("空高16进制字符串=",kong_high)
-                        log.info("料高16进制字符串=",liao_high)
-                        log.info("信噪比16进制字符串=",SNR_Date)
-                        log.info("雷达温度比16进制字符串=",temp_Date)
-                        --将hex格式转化为浮点型数据
-                        local kong_temp = string.pack("<L",kong_high)
-                        kong_Value_485 = string.unpack("f",kong_temp)
-                        log.info("空高kong_Value_485=",kong_Value_485)
-                        local liao_temp = string.pack("<L",liao_high)
-                        liao_Value_485= string.unpack("f",liao_temp)
-                        log.info("料高liao_Value_485=",liao_Value_485)
-                        local SNR_temp = string.pack("<L",SNR_Date)
-                        SNR_485 = string.unpack("f",SNR_temp)
-                        log.info("信噪比SNR_485=",SNR_485)
-                        local xinhao_temp = string.pack("<L",temp_Date)
-                        Radar_temp_485 = string.unpack("f",xinhao_temp)
-                        log.info("雷达温度Radar_temp_485=",Radar_temp_485)                               
-                        --uart.write(1, s)--正常采集不透传给串口1,也就是传给蓝牙
-                        log.info("发布雷达完成消息", "receive", id, #s, s)
-                        sys.publish("雷达完成")
-                    --=============================================================
-                    --如果不是采集命令,则透传给串口1
-                    elseif crc16_high==receive_crc_high and crc16_low==receive_crc_low then
-                        log.info("串口2透传给串口1数据", "receive", id, #s, s)
-                        -- log.info("uart", "receive", id, #s, s:toHex())
-                        uart.write(1, s)--暂时不透传给串口1
-                    elseif crc16_high~=receive_crc_high or crc16_low~=receive_crc_low then
-                        log.info("无数据或crc检验不通过")
-                        kong_Value_485=-100--空高
-                        liao_Value_485=-100--料高
-                        uart.write(1, s)--暂时不透传给串口1
-                        --两种情况,一种是采集失败,一种是采集回波曲线
-                        end
-                end
-                if #s == len then
-                    --log.info("程序经过这里1")
-                    break
-                end
-            until s == ""
-            --log.info("程序经过这里2")
-        end)
-        --=============================================================
     end
 end
 --=============================================================

+ 14 - 14
module/Air8000/demo/netdrv/ch390/lan_TCP/main.lua

@@ -92,25 +92,25 @@ function TCP_TASK()
     socket.debug(netCB, true)                -- 打开调试日志
     socket.config(netCB, nil, protocol, ssl)      -- 此配置为TCP连接,无SSL加密
 
+    -- 收取数据会触发回调, 这里的"receive" 是固定值不要修改。
+    uart.on(uartid, "receive", function(id, len)
+        while true do
+            local len = uart.rx(id, uart_rx_buff)   -- 接收串口收到的数据,并赋值到uart_rx_buff
+            if len <= 0 then    -- 接收到的字节长度为0 则退出
+                break
+            end
+            -- 如果已经在线了,则发送socket.EVENT消息来打断任务里的阻塞等待状态,让任务循环继续
+            if connect_state then
+                sys_send(taskName, socket.EVENT, 0)
+            end
+        end
+    end)
+
     -- 串口和TCP服务器的交互逻辑
     while true do
         -- 连接服务器,返回是否连接成功
         result = libnet.connect(taskName, 15000, netCB, ip, port)
 
-        -- 收取数据会触发回调, 这里的"receive" 是固定值不要修改。
-        uart.on(uartid, "receive", function(id, len)
-            while true do
-                local len = uart.rx(id, uart_rx_buff)   -- 接收串口收到的数据,并赋值到uart_rx_buff
-                if len <= 0 then    -- 接收到的字节长度为0 则退出
-                    break
-                end
-                -- 如果已经在线了,则发送socket.EVENT消息来打断任务里的阻塞等待状态,让任务循环继续
-                if connect_state then
-                    sys_send(taskName, socket.EVENT, 0)
-                end
-            end
-        end)
-
         -- 如果连接成功,则改变连接状态参数,并且随便发一条数据到服务器,看服务器能不能收到
         if result then
             connect_state = true

+ 14 - 14
module/Air8000/demo/netdrv/ch390/wan_TCP/main.lua

@@ -73,25 +73,25 @@ function TCP_TASK()
     socket.debug(netCB, true)                -- 打开调试日志
     socket.config(netCB, nil, protocol, ssl)      -- 此配置为TCP连接,无SSL加密
 
+    -- 收取数据会触发回调, 这里的"receive" 是固定值不要修改。
+    uart.on(uartid, "receive", function(id, len)
+        while true do
+            local len = uart.rx(id, uart_rx_buff)   -- 接收串口收到的数据,并赋值到uart_rx_buff
+            if len <= 0 then    -- 接收到的字节长度为0 则退出
+                break
+            end
+            -- 如果已经在线了,则发送socket.EVENT消息来打断任务里的阻塞等待状态,让任务循环继续
+            if connect_state then
+                sys_send(taskName, socket.EVENT, 0)
+            end
+        end
+    end)
+
     -- 串口和TCP服务器的交互逻辑
     while true do
         -- 连接服务器,返回是否连接成功
         result = libnet.connect(taskName, 15000, netCB, ip, port)
 
-        -- 收取数据会触发回调, 这里的"receive" 是固定值不要修改。
-        uart.on(uartid, "receive", function(id, len)
-            while true do
-                local len = uart.rx(id, uart_rx_buff)   -- 接收串口收到的数据,并赋值到uart_rx_buff
-                if len <= 0 then    -- 接收到的字节长度为0 则退出
-                    break
-                end
-                -- 如果已经在线了,则发送socket.EVENT消息来打断任务里的阻塞等待状态,让任务循环继续
-                if connect_state then
-                    sys_send(taskName, socket.EVENT, 0)
-                end
-            end
-        end)
-
         -- 如果连接成功,则改变连接状态参数,并且随便发一条数据到服务器,看服务器能不能收到
         if result then
             connect_state = true