user_script/解析TT数据.lua
CSSC-WORK\murmur bf5b2f9691 上报位置附带电池信息,据此更新解析
添加jd wd 电压 电量绘图
2023-09-20 09:34:14 +08:00

224 lines
7.5 KiB
Lua

function getID(str)
local y, m, d, h, min, s, rand = string.unpack("<BBBBBBB", str:sub(1, -1)) -- only 1 pair
return "\r\n时戳 20" .. y
.. "-" .. string.format("%02d",m)
.. "-" .. string.format("%02d",d)
.. " " .. string.format("%02d",h)
.. ":" .. string.format("%02d",min)
.. ":" .. string.format("%02d",s)
.. "_" .. string.format("%02d",rand) .. "\r\n"end
function getCFG( str )
-- body
local sendInterval,maxTTWaitTime,maxTTRetryCnt,minTTPeriCnt,minTTsinal,timeout = string.unpack("<BBBBBB",str)
local rst = "预置开窗\r\n"
for i = 1, 60, 4 do
local v1, v2, v3, v4 = string.unpack("<BBBB", str:sub(6 + i, -1))
if v1 == v3 and v2 == v4 then
break
end
rst = rst .. "[" .. string.format("%02d", v1) .. ":" .. string.format("%02d", v2) .. " - " ..
string.format("%02d", v3) .. ":" .. string.format("%02d", v4) .. "]\r\n"
end
local maxSizePerFile,commMode,selfDesSW,locAlert,locRepInterval,isMaWin = string.unpack("<HBBBBb",str:sub(6+60+1,-1))
rst = rst
-- .."sendInterval\t\t"..sendInterval.."\r\n"
.."maxTTWaitTime\t\t"..maxTTWaitTime.."分钟\r\n"
.."maxTTRetryCnt\t\t"..maxTTRetryCnt.."\r\n"
.."minTTPeriCnt\t\t"..minTTPeriCnt.."\r\n"
.."minTTsinal\t\t"..minTTsinal.."\r\n"
.."timeout\t\t\t"..timeout.."分钟\r\n"
.."maxSizePerFile\t\t"..maxSizePerFile.." Bytes\r\n"
.."commMode\t\t"..(commMode==0 and "TT" or "BD").."\r\n"
.."selfDesSW\t\t"..(selfDesSW == 0 and "" or "").."\r\n"
.."locAlert\t\t\t"..(locAlert== 0 and "" or "").."\r\n"
.."locRepInterval\t\t"..locRepInterval.."分钟\r\n"
.."isMaWin\t\t"..(isMaWin == 0 and "手动开窗,需手动关窗" or (isMaWin >0 and "手动开窗,"..isMaWin.."分钟后关窗" or "RTC")).."\r\n"
return rst
end
function getBattInfo(str)
local a1,a2,a3,a4,a5,a6,a7,a8,a9,b1,b2,b3,b4,b5,b6,b7,b8= string.unpack(">hhhhhhhhhhhhhhhhh",str)
local rst = ""
.."实际电压 "..(a1/100).."V\r\n"
.."电芯数量 "..a2.."\r\n"
.."电量 "..a3.."%\r\n"
.."容量 "..(a4/100).."AH\r\n"
.."输出电流 "..(a5/100).."A\r\n"
.."充电电流 "..(a6/100).."A\r\n"
.."温度1 "..a7.."\r\n"
.."温度2 "..a8.."\r\n"
.."温度3 "..a9.."\r\n"
.."电芯1电压 "..(b1/1000).."V\r\n"
.."电芯2电压 "..(b2/1000).."V\r\n"
.."电芯3电压 "..(b3/1000).."V\r\n"
.."电芯4电压 "..(b4/1000).."V\r\n"
.."电芯5电压 "..(b5/1000).."V\r\n"
.."电芯6电压 "..(b6/1000).."V\r\n"
.."电芯7电压 "..(b7/1000).."V\r\n"
.."电芯8电压 "..(b8/1000).."V\r\n"
apiAddPoint((a1/100),8)
apiAddPoint(a3,9)
return rst
end
--解析
function parseInfo(data)
local selfTest = 0x7001
local battInfo = 0x8888
local locInfo = 0x7011
local depRes = 0x0608
local warnMsg = 0x0d01
local rtcTime = 0x4116
local window = 0x1931
local signalInfo = 0x6666
local cmdReq = 0x0E32
local dspReq = 0x2237
local hearReq = 0x1937
local hearSet= 0x1917
local heartInfo= 0x1903
local cfgReq = 0x7007
local info={
selfTest = "自检",
battInfo = "电池信息",
locInfo = "位置信息",
depRes = "深度回复",
warnMsg = "告警信息",
rtcTime = "RTC时间",
window = "开窗查询",
signalInfo = "信号质量",
cmdReq = "任务查询",
dspReq = "DSP查询回复",
hearReq = "心跳查询",
hearSet = "心跳设置",
hearInfo = "心跳信息",
cfgReq = "配置查询",
}
local head, taddr, saddr, cmd = string.unpack(">HBBH", data) -- 澶х缂栫爜
-- print(string.format("%04X",head))
local rst = {cmd="",info=""}
if head == 0x5aa5 then
-- 逐项解析指令
if cmd == selfTest then
rst.cmd = info.selfTest
local sysSta,xh,jh,commSpeed,powerLevel,flash,sd = string.unpack(">BBBBBHH",data:sub(9,-1))
rst.info = "\r\n"
.."系统状态 "..(sysSta==1 and "正常" or "异常").."\r\n"
.."信号质量 "..xh.."\r\n"
.."电池剩余电量 "..powerLevel.."%\r\n"
.."flash剩余容量 "..flash.."MB\r\n"
.."sd卡剩余容量 "..sd.."MB\r\n"
..getCFG(data:sub(18,-1))
end
if cmd == locInfo then
rst.cmd = info.locInfo
rst.info = getID(data:sub(8+1+1,-1))
local islocok = string.unpack("B",data:sub(8+1))
rst.info = rst.info.."位置状态 "..(islocok == 0 and "正常" or "异常").."\r\n"
rst.info = rst.info.. "信号, 经度, 纬度, 高度\r\n"
for i = 1, 80, 10 do
local xh, jd, wd, gd = string.unpack("<Bffb", data:sub(8+7 + 1 + i, -1)) -- only 1 pair
rst.info = rst.info.."["..string.format("%02d",(xh-0xE0))..", " .. string.format("%.6f",jd) .. ", " .. string.format("%.6f",wd) .. ", " .. string.format("%03d",gd) .. "] \r\n"
apiAddPoint(jd,0)
apiAddPoint(wd,1)
end
rst.info = rst.info..getBattInfo(data:sub(8+8+80+1,-1))
end
if cmd == battInfo then
rst.cmd = info.battInfo
--print(data:toHex())
rst.info = getID(data:sub(8+1,-1))
rst.info = rst.info..getBattInfo(data:sub(8+1+7,-1))
end
if cmd == signalInfo then
rst.cmd = info.signalInfo
rst.info = getID(data:sub(8+1,-1))
local y,m,d,h,min,s,rand= string.unpack(">BBBBBBB",data:sub(9,-1))
.."信号质量 "
for i = 1, 100, 1 do
local v= string.unpack(">B",data:sub(8+7+i,-1))
local sta= v&0x20 ==0x20 and "" or "-"
v = (v&0x1F) == 0x1f and 99 or (v&0x1F)
rst.info = rst.info..sta..v.." "
end
end
if cmd == depRes then
rst.cmd = info.depRes
end
if cmd == warnMsg then
rst.cmd = info.warnMsg
end
if cmd == rtcTime then
rst.cmd = info.rtcTime
end
if cmd == window then
rst.cmd = info.window
end
if cmd == cmdReq then
rst.cmd = info.cmdReq
end
if cmd == hearReq then
rst.cmd = info.hearReq
end
if cmd == hearSet then
rst.cmd = info.hearSet
end
if cmd == dspReq then
rst.cmd = info.dspReq
end
if cmd == heartInfo then
rst.cmd = info.hearInfo
end
if cmd == cfgReq then
rst.cmd = info.cfgReq
rst.info = getCFG(data:sub(9,-1))
end
else--帧头不匹配
cmd="NONE"
end
--print rst
for key, value in pairs(rst) do
if key ~= "" then
-- print(value)
return "\r\n指令:"..string.format("0x%04X",cmd).."\r\n----"..rst.cmd.."----"..rst.info.."\r\n"
end
end
end
function log2file(str)
local f=io.open("D:/User/murmur/Desktop/log.txt","r+")
if f then
f:write(str)
f:close()
end
end
function getinfo(data)
local info = "\r\n长度:["..string.len(data).."]字节\r\n原始数据:\r\n"..data:toHex()
print(info)
log2file(info)
--按换行符切开,防止粘包
local udata = data:split(string.char(0x5a)..string.char(0xa5))
--每包都处理一遍
for i=1,#udata do
if #udata[i]>10 then
local rst = parseInfo(string.char(0x5a)..string.char(0xa5)..udata[i])
print(rst)
log2file(rst)
end
end
end
--注册串口接收函数
apiSetCb("uart",getinfo)