38 lines
1.1 KiB
Lua
38 lines
1.1 KiB
Lua
|
|
function getBattInfo(str)
|
|
local a1,a2,a3,a4,a5,a6= string.unpack("<HHHHhh",str:sub(7,-1))
|
|
local rst = ""
|
|
.."12V实际电压V "..(a1/1000).."V\r\n"
|
|
.."28V实际电压V "..(a2/1000).."V\r\n"
|
|
.."12V输出电流A "..(a3/1000).."A\r\n"
|
|
.."28V输出电流A "..(a4/1000).."A\r\n"
|
|
.."温度1 "..(a5/10).."℃\r\n"
|
|
.."温度2 "..(a6/10).."℃\r\n"
|
|
|
|
apiAddPoint((a5/10),8)
|
|
apiAddPoint((a6/10),9)
|
|
return "\r\n"..rst
|
|
end
|
|
|
|
function getinfo(data)
|
|
local info = "\r\n长度:["..string.len(data).."]字节\r\n原始数据:\r\n"..data:toHex()
|
|
print(info)
|
|
|
|
--按换行符切开,防止粘包
|
|
local udata = data:split(string.char(0x89)..string.char(0x02))
|
|
|
|
--每包都处理一遍
|
|
for i=1,#udata do
|
|
if #udata[i]>10 then
|
|
-- local rst = parseInfo(string.char(0x89)..string.char(0x02)..udata[i])
|
|
local rst = getBattInfo(string.char(0x89)..string.char(0x02)..udata[i])
|
|
print(rst)
|
|
end
|
|
end
|
|
end
|
|
|
|
--注册串口接收函数
|
|
apiSetCb("uart",getinfo)
|
|
|
|
|