广州大彩串口屏论坛_大彩开发者交流论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 949|回复: 2

哪位大神能把自由串口协议的每一行指令什么意思详细解.....

[复制链接]

2

主题

8

帖子

35

积分

新手上路

Rank: 1

积分
35
发表于 2023-12-27 12:48:00 | 显示全部楼层 |阅读模式
uart_free_protocol = 1
--[[--LUA脚本开启自由串口协议时(uart_free_protocol = 1),
屏幕接收数据会自动触发on_uart_recv_data(packet)函数
--]]
local sc_Home = 0

local Func_lampState = 0x01
local Func_lampLight = 0x02

local cmd_head    = 0x5A
local cmd_end     = 0xA55AA5A5
local buff = {}
local cmd_length = 0
local cmd_end_tag      = 0
       
local judge_lamp1_light = 0



--calculate CRC16
--@data : t, data to be verified
--@n    : number of verified
--@return : check result
function add_crc16(start, n, data)
       
        local carry_flag, a = 0
    local result = 0xffff
    local i = start

        while(true)
        do
                result = result ~ data[i]
                for j = 0, 7
        do
            a = result
            carry_flag = a & 0x0001
            result = result >> 1
            if carry_flag == 1
            then
                 result = result ~ 0xa001
            end
        end
               
                i = i + 1
                if i == start + n
                then
                        break
                end
        end

    return result
end


-- Calculate checksum
-- @buffTb: check table
function my_cal_checksum(buffTb)
       
        local check_sum = 0

        for i = 0, #(buffTb)-1
        do
                check_sum = check_sum + buffTb[i]
        end
       
        return (check_sum & 0xFF)
end

--set the lamp state
--@xth_lamp:which lamp
--@xth_lamp_style: state of lamp, 0-colse,1-open
function my_set_lamp_state(xth_lamp, xth_lamp_state)
       
        if xth_lamp == 1
        then
                set_value(sc_Home, 1, xth_lamp_state)       
        end
end

--set the style of the lamp
--@xth_lamp : which lamp
--@xth_lamp_style: style of lamp
function my_set_lamp_light(xth_lamp, xth_lamp_light)
       
        if xth_lamp == 1
        then
                set_value(sc_Home, 2, xth_lamp_light)       
        end
end

--send notify of lamp status
--@xth_lamp: which lamp
--@state: state of lamp, 0-colse,1-open
function my_uartsend_lampstate_notify(xth_lamp,state)

        local lamp_state_notify = {}
        local send_crc16        = 0
       
        lamp_state_notify[0]  = 0xA5
        lamp_state_notify[1]  = Func_lampState
        lamp_state_notify[2]  = xth_lamp
        lamp_state_notify[3]  = state
       
        send_crc16 = add_crc16(1, 3, lamp_state_notify)
        lamp_state_notify[4] = (send_crc16 >> 8) & 0xFF
        lamp_state_notify[5] = (send_crc16 >> 0) & 0xFF

        lamp_state_notify[6] = 0x5A
        lamp_state_notify[7] = 0xA5
        lamp_state_notify[8] = 0x5A
        lamp_state_notify[9] = 0x5A
       
        uart_send_data(lamp_state_notify)
end       

--send notify of lamp brightness
function my_uartsend_lamplight_notify(xth_lamp)

        local xth_lamp_light    = 0
        local lamp_light_notify = {}
        local send_crc16        = 0
       
        if xth_lamp == 1
        then
                xth_lamp_light = get_value(sc_Home, 2)
        end
       
        lamp_light_notify[0]  = 0xA5
        lamp_light_notify[1]  = Func_lampLight
        lamp_light_notify[2]  = xth_lamp
        lamp_light_notify[3]  = xth_lamp_light
       
        send_crc16 = add_crc16(1, 3, lamp_light_notify)
       
        lamp_light_notify[4] = (send_crc16 >> 8) & 0xFF
        lamp_light_notify[5] = (send_crc16 >> 0) & 0xFF

        lamp_light_notify[6] = 0x5A
        lamp_light_notify[7] = 0xA5
        lamp_light_notify[8] = 0x5A
        lamp_light_notify[9] = 0x5A
       
        uart_send_data(lamp_light_notify)
end

--Instruction analysis
--@msg:data table
function my_processmessage(msg)
       
        local funccode = msg[1]
        if funccode == Func_lampState
        then
               
                local xth_lamp = msg[2]
                local xth_lamp_state = msg[3]
                my_set_lamp_state(xth_lamp, xth_lamp_state)
               
        elseif funccode == Func_lampLight
        then
                local xth_lamp = msg[2]
                local xth_lamp_light = msg[3]
                my_set_lamp_light(xth_lamp, xth_lamp_light)
        end

end

-- 系统函数: 初始化
function on_init()
        uart_set_timeout(0, 0)
        uart_setup(115200, 0, 1.5, 8)
end

local cmd_head     = 0x5A           --帧头
local cmd_end      = 0xA55AA5A5     --帧尾
local buff         = {}             --缓冲区
local cmd_length   = 0              --帧长度
local cmd_head_tag = 0
local cmd_end_tag  = 0              --帧尾标识

-- 系统函数: 串口接收函数
function on_uart_recv_data(packet)

        local recv_packet_size = (#(packet))
        local check16          = 0
       
        for i = 0, recv_packet_size
        do
                if packet[i] == cmd_head and cmd_head_tag == 0
                then
                        cmd_head_tag = 1
                end
               
                if cmd_head_tag == 1
                then
                        buff[cmd_length] = packet[i]
                        cmd_length = cmd_length + 1
                        cmd_end_tag = (cmd_end_tag << 8) | (packet[i])
                       
                        if (cmd_end_tag & cmd_end)== cmd_end
                        then
                                check16 = ((buff[cmd_length - 6] << 8) | buff[cmd_length - 5]) & 0xFFFF
                                print('check16 = '..string.format('%04X', check16))
                                print('result = '..string.format('%04X', add_crc16(1, cmd_length - 7, buff)))
                                if check16 == add_crc16(1, cmd_length - 7, buff)
                                then
                                       
                                        my_processmessage(buff)
                                        buff         = {}
                                        cmd_length   = 0
                                        cmd_end_tag  = 0
                                        cmd_head_tag = 0
                                else
                                        buff         = {}
                                        cmd_length   = 0
                                        cmd_end_tag  = 0
                                        cmd_head_tag = 0
                                end
                        end
                end
        end
end


-- 系统函数: 触摸回调函数
function on_press(state,x,y)
        local cur_screen = get_current_screen()
       
        if state == 0 and cur_screen == sc_Home
        then
                if judge_lamp1_light == 1
                then
                        my_uartsend_lamplight_notify(1)
               
                        judge_lamp1_light = 0
                end
        end
end

--用户通过触摸修改控件后,执行此回调函数。
--点击按钮控件,修改文本控件、修改滑动条都会触发此事件。
function on_control_notify(screen,control,value)
       
        --开关灯设置
        if screen == sc_Home and control == 1
        then
                my_uartsend_lampstate_notify(1, value)
               
        --亮度设置
        elseif screen == sc_Home and control == 2
        then
                judge_lamp1_light = 1
        end
       
end

回复

使用道具 举报

2

主题

8

帖子

35

积分

新手上路

Rank: 1

积分
35
 楼主| 发表于 2023-12-27 12:49:14 | 显示全部楼层
注释太少了,看起来特别吃力,研究一个礼拜了,没看明白
回复

使用道具 举报

2

主题

1253

帖子

1万

积分

论坛元老

Rank: 8Rank: 8

积分
12541
发表于 2023-12-28 14:41:20 | 显示全部楼层
看一下这个例程

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Easy doesn't enter into Grown-up life
成年人的生活里面没有“容易”二字
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|广州大彩串口屏论坛_大彩开发者交流论坛

GMT+8, 2024-4-27 14:13 , Processed in 0.053445 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表