工作中使用Lua 遇到的小案例,(双重if 中,内层判断体)(代码部分的优化)
内容纲要

module(..., package.seeall)
require "utils"
require "pm"

require "testJson" -- 引入JSON 使用JSON 解包

local UartID = 2

function UartReceive()

    local cacheDATA, ReceiveCount = "", 0 -- 定义缓存,以及数据接收次数。

    -- local tail = string.find(data,FrameTail)
    while true do
        local data = uart.read(UartID, "*l") -- 放在循环里,每次都要读一下
        cacheJSON = data

        ReceiveCount = ReceiveCount + 1
        cacheDATA = cacheDATA .. data

        sys.wait(20) -- 系统在这里停20 ms
        if data == "" then
            if not sys.waitUntil("UartReceive", 200) then -- 如果收到消息, sys.waitUntil() 这个函数就会返回true    --所以下面这个判断体内在200ms 没有收到数据就打印现有数据

                if cacheDATA:len() > 0 then
                    log.info("receiveCount: ", ReceiveCount)

                    -- UartSend("100ms dont receive any Message,Print cache:   "..cacheDATA)

                    local DecodeData, getResult, erroInfo = json.decode(
                                                                cacheDATA) -- 此处cache 存储的带有一个“” ,
                    cacheJSON = ""

                    -- @paragram DecodeData
                    if getResult then -- 如果成功解析,就把结果传进去
                        judgeField(DecodeData)

                        print(" DecodeData = ", DecodeData) -- 这里返回的是一个二进制table

                    else
                        UartSend(json.encode({resp = "error"}))
                        log.info("得到的结果 getResult = ", getResult)
                        print("errorinfo =  ", erroInfo)
                    end

                    cacheDATA = "" -- 每次200ms 内没数据就将缓存置空
                end

            else

                log.info("收到的数据次数:", ReceiveCount)
            end
        end

        -- log.info("cacheDATA = ",data)
    end
end
showInfo = {resp = "yes", code = 0}
-- 判断传入的值是否合法
function judgeField(sourceDATA)
    -- if (sourceDATA.age:tostring) == "age" and ((type(sourceDATA.age)) ~= "number")  then
    if (sourceDATA.age and ("number" == type(sourceDATA.age))) then

        if ((sourceDATA.age) < 20) then -- 因为  如果是name 判断通过了,sourceDATA.age 没有这个值,nil 类型不能和它判断。
            showInfo.resp = "no"
        end

    else
        showInfo.resp = "error"
    end
    UartSend(json.encode(showInfo))

end

-- 定义串口发送函数
function UartSend(sendMessage)
    log.info("UartSend被调用,", sendMessage)
    uart.write(UartID, sendMessage .. "\r\n")
end

-- 下方UART 初始化
require "pm"
pm.wake("懒猪起床了!")
uart.on(UartID, "Uart_SendMessage", UartSend)
uart.on(UartID, "Uart_ReceiveMessage", UartReceive)
uart.setup(UartID, 115200, 8, uart.PAR_NONE, uart.STOP_1) -- Uart 端口 、波特率、
-- 创建一个task
sys.taskInit(UartReceive)
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇