From 00994abbf8860dd369a88528d42d54322e248d4e Mon Sep 17 00:00:00 2001 From: "CSSC-WORK\\murmur" Date: Mon, 18 Sep 2023 11:54:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 解析TT数据.lua | 207 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 解析TT数据.lua diff --git a/解析TT数据.lua b/解析TT数据.lua new file mode 100644 index 0000000..dc44f51 --- /dev/null +++ b/解析TT数据.lua @@ -0,0 +1,207 @@ +function getID(str) + local y, m, d, h, min, s, rand = string.unpack("0 and "手动开窗,"..isMaWin.."分钟后关窗" or "RTC")).."\r\n" + 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 cfgReq = 0x7007 + local info={ + selfTest = "自检", + battInfo = "电池信息", + locInfo = "位置信息", + depRes = "深度回复", + warnMsg = "告警信息", + rtcTime = "RTC时间", + window = "开窗查询", + signalInfo = "信号质量", + cmdReq = "任务查询", + dspReq = "DSP查询回复", + hearReq = "心跳查询", + hearSet = "心跳设置", + 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, 100, 10 do + local xh, jd, wd, gd = string.unpack("BBBBBBBhhhhhhhhh",data:sub(9,-1)) + rst.info = rst.info + .."实际电压 "..(a1/100).."V\r\n" + -- .."电芯数量 "..a2.."\r\n" + .."电量 "..a3.."%\r\n" + -- .."容量 "..a4.."\r\n" + -- .."输出电流 "..a5.."\r\n" + -- .."充电电流 ".. + .."温度1 "..a7.."℃\r\n" + .."温度2 "..a8.."℃\r\n" + .."温度3 "..a9.."℃\r\n" + -- .."电芯 1电压 ".. + -- .."电芯 2电压 ".. + -- .."电芯 3电压 ".. + -- .."电芯 4电压 ".. + -- .."电芯 5电压 ".. + -- .."电芯 6电压 ".. + -- .."电芯 7电压 ".. + -- .."电芯 8电压 ".. + 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 == 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) + +