|
想做自定义串口接收,每隔1S获取回传指令,然后根据回传设置串口屏控件显示,我的代码获取了回传指令但显示屏无反应,麻烦大佬看看哪里有问题或者能否给份参考代码:
uart_free_protocol = 1
local cmd_head = 0 -- 帧头检测
local cmd_end = 0 -- 帧尾检测
local buff = {} -- 缓冲区
local cmd_length = 0 -- 帧长度
local cmd_head_tag = 0 -- 帧头标识
local cmd_end_tag = 0 --帧尾标识
local cmd_flag = 0 -- 命令类型标识
-- 系统函数: 串口接收函数
function on_uart_recv_data(packet)
local recv_packet_size = (#(packet))
for i = 0, recv_packet_size
do
cmd_head = (cmd_head<<8)|packet
cmd_head = cmd_head&0xFFFF
--反馈抑制
if cmd_head == 0xEF4A and cmd_head_tag == 0
then
cmd_flag = 1
cmd_head_tag = 1
--静音开关
elseif cmd_head == 0xEF44 and cmd_head_tag == 0
then
cmd_flag = 2
cmd_head_tag = 1
--音量大小
elseif cmd_head == 0xEF45 and cmd_head_tag == 0
then
cmd_flag = 3
cmd_head_tag = 1
--幻象供电
elseif cmd_head == 0xEF68 and cmd_head_tag == 0
then
cmd_flag = 4
cmd_head_tag = 1
--高通
elseif cmd_head == 0xEF41 and cmd_head_tag == 0
then
cmd_flag = 5
cmd_head_tag = 1
--低通
elseif cmd_head == 0xEF42 and cmd_head_tag == 0
then
cmd_flag = 6
cmd_head_tag = 1
end
if cmd_head_tag == 1
then
buff[cmd_length] = packet
cmd_length = cmd_length + 1
cmd_end = (cmd_end<<8)|packet
cmd_end = cmd_end&0xFFFFFFFF
if cmd_end== 0xFFFEFFFF
then
my_processmessage(buff)
buff = {}
cmd_length = 0
cmd_end_tag = 0
cmd_head_tag = 0
cmd_head=0
cmd_end=0
cmd_flag=0
end
end
end
end
function my_processmessage(buff)
--反馈抑制
if cmd_flag ==1 then
--IN1
if buff[1]==0x10 then
--啸叫抑制: 1表示打开, 0表示关闭
if buff[2]==0x01 then
set_value(6,7,0)
elseif buff[2]==0x00 then
set_value(6,7,1)
end
--尾长,64,96,128,192
if buff[3]==0x00 and buff[4]==0x40 then
set_value(4,4,1)
elseif buff[3]==0x00 and buff[4]==0x60 then
set_value(4,6,1)
elseif buff[3]==0x00 and buff[4]==0x80 then
set_value(4,8,1)
elseif buff[3]==0x00 and buff[4]==0xC0 then
set_value(4,15,0)
end
--噪声等级:6~18, 步长为1
set_text(6,14,tonumber(buff[5]))
--噪声功能:1表示打开,0表示关闭
if buff[6]==0x01 then
set_value(4,10,0)
elseif buff[6]==0x00 then
set_value(4,10,1)
end
--模式:0表示浑厚,1表示明亮
if buff[7]==0x01 then
set_value(4,13,1)
elseif buff[7]==0x00 then
set_value(4,13,0)
end
end
end
if cmd_flag ==2 then
if buff[1] == 0x10 and buff[2]==0x01 then
set_value(9,1,1)
set_value(9,3,1)
elseif buff[1] == 0x10 and buff[2]==0x00 then
set_value(9,1,0)
set_value(9,3,0)
end
if buff[1] == 0x11 and buff[2]==0x01 then
set_value(9,2,1)
set_value(9,4,1)
elseif buff[1] == 0x11 and buff[2]==0x00 then
set_value(9,2,0)
set_value(9,4,0)
end
end
end
|
|