function getID(str) local y, m, d, h, min, s, rand = string.unpack("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("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)