Просмотр исходного кода

add: crypto.crc16_modbus支持设置初始值,方便进行多段数据连续计算

Wendal Chen 2 лет назад
Родитель
Сommit
bbd7ad1181
2 измененных файлов с 11 добавлено и 2 удалено
  1. 6 2
      luat/modules/luat_lib_crypto.c
  2. 5 0
      luat/modules/luat_lib_i2s.c

+ 6 - 2
luat/modules/luat_lib_crypto.c

@@ -314,19 +314,23 @@ static int l_crypto_crc16(lua_State *L)
 
 
 /**
 /**
 直接计算modbus的crc16值
 直接计算modbus的crc16值
-@api crypto.crc16_modbus(data)
+@api crypto.crc16_modbus(data, start)
 @string 数据
 @string 数据
+@int 初始化值,默认0xFFFF
 @return int 对应的CRC16值
 @return int 对应的CRC16值
 @usage
 @usage
 -- 计算CRC16 modbus
 -- 计算CRC16 modbus
 local crc = crypto.crc16_modbus(data)
 local crc = crypto.crc16_modbus(data)
+-- 2023.11.06 新增初始值设置
+crc = crypto.crc16_modbus(data, 0xFFFF)
  */
  */
 static int l_crypto_crc16_modbus(lua_State *L)
 static int l_crypto_crc16_modbus(lua_State *L)
 {
 {
     size_t len = 0;
     size_t len = 0;
     const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
     const unsigned char *inputData = (const unsigned char*)luaL_checklstring(L, 1, &len);
+    uint16_t crc_init = luaL_optinteger(L, 2, 0xFFFF);
 
 
-    lua_pushinteger(L, calcCRC16_modbus(inputData, len));
+    lua_pushinteger(L, calcCRC16_modbus(inputData, len, crc_init));
     return 1;
     return 1;
 }
 }
 
 

+ 5 - 0
luat/modules/luat_lib_i2s.c

@@ -15,6 +15,9 @@
 #include "luat_malloc.h"
 #include "luat_malloc.h"
 #include "luat_i2s.h"
 #include "luat_i2s.h"
 #include "luat_zbuff.h"
 #include "luat_zbuff.h"
+
+#ifdef LUAT_USE_I2S
+
 #include "c_common.h"
 #include "c_common.h"
 #define LUAT_LOG_TAG "i2s"
 #define LUAT_LOG_TAG "i2s"
 #include "luat_log.h"
 #include "luat_log.h"
@@ -305,3 +308,5 @@ LUAMOD_API int luaopen_i2s(lua_State *L)
     return 1;
     return 1;
 }
 }
 
 
+#endif
+