local n1 = {"漏水", "温度", "CDB", "S-TT", "BD", "SJL", "电源", "预留"} local n2 = {"S-TT", "BD", "CDB", "SJL", "FB", "预留2", "预留1", "预留0"} local bx = {"0.6kbps", "1.2kbps", "2.4kbps", "4.8kbps"} local sttSpeed = {"2.4bps", "4.8kbps", "9.6kbps"} local cdbSpeed = {"未知","未知","2.4kbps", "4.8kbps", "38.4kbps"} -- 创建一个自定义的遍历函数 function orderedPairs(t) local keys = {} -- 将table中的键存储到一个数组中 for k, _ in pairs(t) do table.insert(keys, k) end -- 对数组进行排序 table.sort(keys) -- 遍历排序后的数组,并打印每个元素的值 local tmp="" for _, k in ipairs(keys) do tmp = tmp..t[k].."\t" end return tmp end function getBattInfo(str) local a1,a3,a5,a6,a7,a8,a9,a10,a18,a19,a23,a24,a25,a26,a27,a28,a29,a30= string.unpack("> i) & 1 s1 = s1..(bitValue == 1 and "X" or "√") .. "\t" end s1=s1.."\r\n-----------------------------------------------------------------------" local s2="-----------------------------------------------------------------------\r\n" s2=s2..orderedPairs(n2).."\r\n" -- Print the corresponding statuses for i = 7,0,-1 do local bitValue = (a7 >> i) & 1 s2 = s2..(bitValue == 1 and "√" or "--") .. "\t" end s2=s2.."\r\n-----------------------------------------------------------------------" local rst = "\r\n".. "a1 "..a1.."\r\n".. "a3 "..a3.."\r\n".. "a5 "..a5.."\r\n".. "a6 "..string.format("0x%02X",a6).."\r\n".. s1.."\r\n".. "a7 "..string.format("0x%02X",a7).."\r\n".. s2.."\r\n".. "a8 ".."S工作状态\t\t"..(a8== 0 and "未入网" or (a8== 1 and "已入网" or "错误:"..string.format("0x%02X",a8))).."\r\n".. "a9 ".."S信号强度\t\t"..(a9== 0 and "无信号" or a9.."(4最强)").."\r\n".. "a10 "..a10.."\r\n".. "a18 "..a18.."\r\n".. "a19 ".."CDB频率\t\t"..a19.." kHz\r\n".. "a23 ".."CDB速率\t\t"..(a23 < 0x05 and bx[a23] or "错误:"..string.format("0x%02X",a23)).."\r\n".. "a24 ".."S天线模式\t\t"..(a24 == 0x01 and "一线通天线<----->榄杆天线" or (a24 == 0x02 and "二线通天线<----->FB天线" or "错误:"..string.format("0x%02X",a24))).."\r\n".. "a25 ".."TT模式\t\t"..(a25 == 0x00 and "点波束" or (a28 == 0x01 and "海模式" or "错误:"..string.format("0x%02X",a25))).."\r\n".. "a26 ".."TT速率\t\t"..(a26 < 0x04 and sttSpeed[a26] or "错误:"..string.format("0x%02X",a26)).."\r\n".. "a27 ".."BD天线模式\t"..(a27 == 0x01 and "一线通天线<----->榄杆天线" or (a27 == 0x02 and "二线通天线<----->FB天线" or "错误:"..string.format("0x%02X",a27))).."\r\n".. "a28 ".."卡模式\t\t"..(a28 == 0x01 and "民卡模式" or (a28 == 0x02 and "芯片模式" or "错误:"..string.format("0x%02X",a28))).."\r\n".. "a29 "..a29.."\r\n".. "a30 "..a30.."\r\n".. "a31 ".."时间\t\t"..string.format("%d年%d月%d日 %d:%d:%d.%d",a31,a33,a34,a35,a36,a37,a38).."\r\n".. "a40 ".."纬度\t\t"..a40.."\r\n".. "a48 ".."经度\t\t"..a48.."\r\n".. "a56 ".."定位\t\t"..(a56== 6 and "正在估算" or (a56 == 1 and "非差分定位" or "未定位")).."\r\n".. "a57 ".."卫星数\t\t"..a57.."\r\n".. "a58 ".."HDOP\t\t"..a58.."\r\n".. "a62 ".."海拔\t\t"..a62.."\r\n".. "a66 ".."航速\t\t"..a66.."\r\n".. "a70 ".."调制方式\t\t"..(a70 == 1 and "FM" or "AM").."\r\n".. "a71 ".."信道类型\t\t"..(a71 == 0xc1 and "X02" or (a71 == 0xc2 and "X03" or "错误:"..string.format("0x%02X",a71))).."\r\n".. "a73 ".."站方式\t\t"..(a73 == 0x01 and "主站" or (a73 == 0x04 and "从站" or "错误:"..string.format("0x%02X",a73))).."\r\n".. "a75 ".."波形速率\t\t"..(a75 < 0x04 and bx[a75] or "错误").."\r\n".. --"a76 "..a76.."\r\n".. --"a78 "..a78.."\r\n".. --"a80 "..a80.."\r\n".. --"a82 "..a82.."\r\n".. --"a84 "..a84.."\r\n".. --"a86 "..a86.."\r\n".. --"a88 "..a88.."\r\n".. --"a90 "..a90.."\r\n".. --"a92 "..a92.."\r\n".. --"a94 "..a94.."\r\n".. --"a96 "..a96.."\r\n".. "a97 "..a97.."\r\n" return 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(0x54)..string.char(0x58)) --每包都处理一遍 for i=1,#udata do if #udata[i]>10 then -- local rst = parseInfo(string.char(0x54)..string.char(0x58)..udata[i]) local rst = getBattInfo(string.char(0x54)..string.char(0x58)..udata[i]) print(rst) end end end function getinfo1(data) local data = data.data getinfo(data) end --注册串口接收函数 apiSetCb("uart",getinfo) apiSetCb("tcp-server",getinfo1)