123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- --- 模块功能:自建公用函数库
- module(..., package.seeall)
- require "misc"
- function getTime()
- local tm = misc.getClock()
- return string.format("%04d/%02d/%02d,%02d:%02d:%02d", tm.year, tm.month, tm.day, tm.hour, tm.min, tm.sec)
- end
- --十六进制字符串反转
- function hexStrReverse(hexstr)
- local str = ""
- for i = 1, string.len(hexstr) - 1, 2 do
- local tmpStr = string.sub(hexstr, -(i+1), -i)
- str = str .. tmpStr
- end
- return str
- end
- --十六进制字符串转二进制
- --openMulByteReverse:开启多字节反转
- function hex2bin( hexstr, openMulByteReverse )
- --检查字符串长度
- if(hexstr:len()%2~=0) then
- return false,"hex2str invalid input lenth"
- end
- if openMulByteReverse then
- hexstr = hexStrReverse(hexstr)
- end
- local str = ""
- for i = 1, string.len(hexstr) - 1, 2 do
- local doublebytestr = string.sub(hexstr, i, i+1);
- local n = tonumber(doublebytestr, 16);
- if 0 == n then
- str = str .. '\00'
- else
- str = str .. string.format("%c", n)
- end
- end
- return str
- end
- --十六进制字符串转ascii码字符串
- function hex2str(str)
- --判断输入类型
- if (type(str)~="string") then
- return nil,"hex2str invalid input type"
- end
- --滤掉分隔符
- str=str:gsub("[%s%p]",""):upper()
- --检查内容是否合法
- if(str:find("[^0-9A-Fa-f]")~=nil) then
- return nil,"hex2str invalid input content"
- end
- --检查字符串长度
- if(str:len()%2~=0) then
- return nil,"hex2str invalid input lenth"
- end
- --拼接字符串
- local index=1
- local ret=""
- for index=1,str:len(),2 do
- ret=ret..string.char(tonumber(str:sub(index,index+1),16))
- end
- --log.info("socketHeartbeatTask.hex2str", ret)
- return ret
- end
- --计算920标签crc16校验码
- function get920Crc16(packet)
- if not packet then
- log.error("funlib.get920Crc16", "packet empty!!")
- return false
- end
- local CRC = 0XFFFF
- local POLYNOMIAL = 0x8408
- for i=1,#packet do
- CRC = bit.bxor( CRC, string.byte(packet, i))
- for j=1,8 do
- if string.format("%02X",bit.band(CRC, 0x1)) == "01" then
- CRC = bit.bxor(bit.rshift(CRC, 1), POLYNOMIAL)
- else
- CRC = bit.rshift(CRC, 1)
- end
- end
- end
- CRC = tonumber(hexStrReverse(string.format("%04X",CRC)),16)
- return true, CRC
- end
- --[[
- 函数名:getCrc8
- 功能 :计算crc8
- 参数 :
- packet:二进制数据
- crc_init:crc初始值
- crc_poly: 多项式值
- 返回值:第一个返回值是处理结果,第二个返回值是CRC值
- ]]
- function getCrc8(packet,crc_init,crc_poly)
- if not packet then
- log.error("funlib.getCrc8", "packet empty!!")
- return false
- end
- local CRC = crc_init or 0XFF
- local POLYNOMIAL = crc_poly or 0xFF
- for i=1,#packet do
- CRC = bit.bxor( CRC, string.byte(packet, i))
- for j=1,8 do
- if string.format("%02X",bit.band(CRC, 0x80)) == "01" then
- CRC = bit.bxor(bit.lshift(CRC, 1), POLYNOMIAL)
- else
- CRC = bit.lshift(CRC, 1)
- end
- end
- end
-
- return true, CRC
- end
- function ipFormatVerify(host)
- if not host then
- log.error("binMqttTask.ipFormatVerify","host not existed!")
- return false
- end
- if type(host) ~= "string" then
- log.error("binMqttTask.ipFormatVerify","host type not string!")
- return false
- end
- if string.match(host, "[%w%-]+%.[%w%-]+%.[%w%-]+%.[%l]+")
- or string.match(host, "[%w%-]+%.[%w%-]+%.[%l]+")
- or string.match(host, "%d+%.%d+%.%d+%.%d+")
- or string.match(host, "[%w%-]+%.[%l]+") then
- return true
- else
- log.error("binMqttTask.ipFormatVerify","host format invalid!")
- return false
- end
- end
|