funlib.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. --- 模块功能:自建公用函数库
  2. module(..., package.seeall)
  3. require "misc"
  4. function getTime()
  5. local tm = misc.getClock()
  6. return string.format("%04d/%02d/%02d,%02d:%02d:%02d", tm.year, tm.month, tm.day, tm.hour, tm.min, tm.sec)
  7. end
  8. --十六进制字符串反转
  9. function hexStrReverse(hexstr)
  10. local str = ""
  11. for i = 1, string.len(hexstr) - 1, 2 do
  12. local tmpStr = string.sub(hexstr, -(i+1), -i)
  13. str = str .. tmpStr
  14. end
  15. return str
  16. end
  17. --十六进制字符串转二进制
  18. --openMulByteReverse:开启多字节反转
  19. function hex2bin( hexstr, openMulByteReverse )
  20. --检查字符串长度
  21. if(hexstr:len()%2~=0) then
  22. return false,"hex2str invalid input lenth"
  23. end
  24. if openMulByteReverse then
  25. hexstr = hexStrReverse(hexstr)
  26. end
  27. local str = ""
  28. for i = 1, string.len(hexstr) - 1, 2 do
  29. local doublebytestr = string.sub(hexstr, i, i+1);
  30. local n = tonumber(doublebytestr, 16);
  31. if 0 == n then
  32. str = str .. '\00'
  33. else
  34. str = str .. string.format("%c", n)
  35. end
  36. end
  37. return str
  38. end
  39. --十六进制字符串转ascii码字符串
  40. function hex2str(str)
  41. --判断输入类型
  42. if (type(str)~="string") then
  43. return nil,"hex2str invalid input type"
  44. end
  45. --滤掉分隔符
  46. str=str:gsub("[%s%p]",""):upper()
  47. --检查内容是否合法
  48. if(str:find("[^0-9A-Fa-f]")~=nil) then
  49. return nil,"hex2str invalid input content"
  50. end
  51. --检查字符串长度
  52. if(str:len()%2~=0) then
  53. return nil,"hex2str invalid input lenth"
  54. end
  55. --拼接字符串
  56. local index=1
  57. local ret=""
  58. for index=1,str:len(),2 do
  59. ret=ret..string.char(tonumber(str:sub(index,index+1),16))
  60. end
  61. --log.info("socketHeartbeatTask.hex2str", ret)
  62. return ret
  63. end
  64. --计算920标签crc16校验码
  65. function get920Crc16(packet)
  66. if not packet then
  67. log.error("funlib.get920Crc16", "packet empty!!")
  68. return false
  69. end
  70. local CRC = 0XFFFF
  71. local POLYNOMIAL = 0x8408
  72. for i=1,#packet do
  73. CRC = bit.bxor( CRC, string.byte(packet, i))
  74. for j=1,8 do
  75. if string.format("%02X",bit.band(CRC, 0x1)) == "01" then
  76. CRC = bit.bxor(bit.rshift(CRC, 1), POLYNOMIAL)
  77. else
  78. CRC = bit.rshift(CRC, 1)
  79. end
  80. end
  81. end
  82. CRC = tonumber(hexStrReverse(string.format("%04X",CRC)),16)
  83. return true, CRC
  84. end
  85. --[[
  86. 函数名:getCrc8
  87. 功能 :计算crc8
  88. 参数 :
  89. packet:二进制数据
  90. crc_init:crc初始值
  91. crc_poly: 多项式值
  92. 返回值:第一个返回值是处理结果,第二个返回值是CRC值
  93. ]]
  94. function getCrc8(packet,crc_init,crc_poly)
  95. if not packet then
  96. log.error("funlib.getCrc8", "packet empty!!")
  97. return false
  98. end
  99. local CRC = crc_init or 0XFF
  100. local POLYNOMIAL = crc_poly or 0xFF
  101. for i=1,#packet do
  102. CRC = bit.bxor( CRC, string.byte(packet, i))
  103. for j=1,8 do
  104. if string.format("%02X",bit.band(CRC, 0x80)) == "01" then
  105. CRC = bit.bxor(bit.lshift(CRC, 1), POLYNOMIAL)
  106. else
  107. CRC = bit.lshift(CRC, 1)
  108. end
  109. end
  110. end
  111. return true, CRC
  112. end
  113. function ipFormatVerify(host)
  114. if not host then
  115. log.error("binMqttTask.ipFormatVerify","host not existed!")
  116. return false
  117. end
  118. if type(host) ~= "string" then
  119. log.error("binMqttTask.ipFormatVerify","host type not string!")
  120. return false
  121. end
  122. if string.match(host, "[%w%-]+%.[%w%-]+%.[%w%-]+%.[%l]+")
  123. or string.match(host, "[%w%-]+%.[%w%-]+%.[%l]+")
  124. or string.match(host, "%d+%.%d+%.%d+%.%d+")
  125. or string.match(host, "[%w%-]+%.[%l]+") then
  126. return true
  127. else
  128. log.error("binMqttTask.ipFormatVerify","host format invalid!")
  129. return false
  130. end
  131. end