Lua脚本
Lua标准手册
VG3扩展功能手册
VG3 Lua脚本Demo
VG3 Lua手册索引
Lua脚本公共库
PublicDataFunc
PublicMathFunc
LuaModbus读写应用示例
通过TCP触发流程与写入全局变量
【技术文档】VG3 Lua API 技术文档
【技术文档】LuaGlobalVar — 全局变量操作
【技术文档】ZMotion - 运动控制模块
本站点使用 MrDoc 构建
-
+
LuaModbus读写应用示例
# 基于VisonGrup3 Lua脚本环境的 LuaCommu 寄存器读写文档 ## 概述 本文档提供了一个完整的 Lua 寄存器读写工具库,包含单点读写和批量读写两种方式。基于您提供的代码,这是一个完整的、可直接使用的寄存器读写示例。 ## 目录 - [依赖环境](#依赖环境) - [常量定义](#常量定义) - [单点读写函数](#单点读写函数) - [批量读写函数](#批量读写函数) - [字节数组操作](#字节数组操作) - [完整示例](#完整示例) - [注意事项](#注意事项) - [故障排除](#故障排除) ## 依赖环境 该工具基于以下 VisonGrup3视觉的Lua 扩展库: - `LuaCommu`: 寄存器通信接口 - `LuaLog`: 日志记录 - `LuaString`: 字节数组转换 ## 常量定义 ### 字节序编码常量 ```lua local orderCode = { ABCD = "ABCD", -- 大端序(默认) BADC = "ADC", -- 字节交换 CDAB = "CDAB", -- 字交换 DCBA = "DCBA" -- 小端序 } ``` ### 数据类型常量 ```lua local dataType = { SHORT = "short", -- 16位有符号整数 INT = "int", -- 32位有符号整数 FLOAT = "float" -- 32位浮点数 } ``` ## 单点读写函数 ### 1. 浮点数(Real)读写 ```lua -- 写入浮点数 local val = 15.235 LuaCommu:WriteRegisterReal(0, 100, val) -- 通信索引0,寄存器地址100 -- 读取浮点数 local retVal = LuaCommu:ReadRegisterReal(0, 100, 1) -- 从地址100开始读取1个实数 LuaLog:Debug("读取的浮点数: " .. tostring(retVal)) LuaLog:Debug(" 保留2位小数: " .. string.format("%.2f", retVal) ``` ### 2. 长整数(Long)读写 ```lua -- 写入长整数 local val = 554 LuaCommu:WriteRegisterLong(0, 100, val) -- 读取长整数 local retVal = LuaCommu:ReadRegisterLong(0, 100, 1) LuaLog:Debug("读取的长整数: " .. tostring(retVal)) ``` ### 3. 短整数(Short)读写 ```lua -- 写入短整数 local val = 124 LuaCommu:WriteRegisterShort(0, 100, val) -- 读取短整数 local retVal = LuaCommu:ReadRegisterShort(0, 100, 1) LuaLog:Debug("读取的短整数: " .. tostring(retVal)) ``` ## 批量读写函数 ### 1. 批量读取寄存器 #### 函数原型 ```lua function readRegisterMultiple(commIndex, registerIndex, order, count, dataTypeStr) ``` #### 参数说明 | 参数名 | 类型 | 必填 | 默认值 | 说明 | |--------|------|------|--------|------| | commIndex | integer | 否 | 0 | 通信通道索引 | | registerIndex | integer | 是 | - | 起始寄存器地址 | | order | string | 否 | "ABCD" | 字节序格式 | | count | integer | 是 | - | 要读取的数据个数 | | dataTypeStr | string | 是 | - | 数据类型 ("short"/"int"/"float") | #### 返回值 - 类型:数组 (table) - 说明:包含所有解析后数值的数组 #### 使用示例 ```lua -- 读取2个16位有符号整数 local retData = readRegisterMultiple(0, 100, orderCode.ABCD, 2, dataType.SHORT) for i, value in ipairs(retData) do LuaLog:Debug("读取值" .. i .. ": " .. value) end ``` ### 2. 批量写入寄存器 #### 函数原型 ```lua function writeRegisterMultiple(commIndex, registerIndex, values, dataTypeStr, order) ``` #### 参数说明 | 参数名 | 类型 | 必填 | 默认值 | 说明 | |--------|------|------|--------|------| | commIndex | integer | 否 | 0 | 通信通道索引 | | registerIndex | integer | 是 | - | 起始寄存器地址 | | values | array | 是 | - | 要写入的数值数组 | | dataTypeStr | string | 是 | - | 数据类型 ("short"/"int"/"float") | | order | string | 否 | "ABCD" | 字节序格式 | #### 使用示例 ```lua -- 批量写入SHORT类型数据 local shortValues = {100, 200, 300} writeRegisterMultiple(0, 100, shortValues, dataType.SHORT, orderCode.ABCD) ``` ## 字节数组操作 ### 1. 批量读取字节数组 ```lua -- 读取字节数组 local val = LuaCommu:ReadRegisterByteArray(0, 100, 2) -- 从地址100读取2个字节 LuaLog:Debug("读取寄存器的值:" .. LuaString:FromByteArray(val, "-")) -- 打印示例: 0a-00-00-00-01-00-00-00- ``` ### 2. 截取字节数组 ```lua -- 从位置0开始截取4个字节 local midVal = LuaCommu:midBytearray(val, 0, 4) LuaLog:Debug("截取寄存器值:" .. LuaString:FromByteArray(midVal, "-")) ``` ### 3. 字节数组转换 ```lua -- 字节数组转换为整数 local retVal = LuaCommu:byteArray_int(midVal, "ABCD", -1) -- -1表示有符号 -- 字节数组转换为浮点数 local retVal = LuaCommu:byteArray_float(midVal, "ABCD", -1) -- 字节数组转换为短整数 local retVal = LuaCommu:byteArray_short(midVal, "ABCD", -1) ``` ### 4. 创建字节数组 ```lua -- 从整数创建字节数组 local val = LuaCommu:int_bytearray(12, "ABCD") -- 从浮点数创建字节数组 local val = LuaCommu:float_bytearray(12.12, "ABCD") -- 从短整数创建字节数组 local val = LuaCommu:short_bytearray(1, "ABCD") ``` ### 5. 追加字节数组 ```lua -- 创建多个字节数组并追加 local val = LuaCommu:int_bytearray(12, "ABCD") local val1 = LuaCommu:int_bytearray(13, "ABCD") val:appendByteArray(val1) ``` ### 6. 批量写入字节数组 ```lua -- 批量写入字节数组 LuaCommu:WriteRegisterByteArray(0, 50, val) ``` ### 7.批量读写寄存器函数实现 ```lua -- 批量读取寄存器函数 -- 参数说明: -- commIndex: 通信通道索引,默认为0 -- registerIndex: 起始寄存器地址 -- order: 字节序,从orderCode表中选择 -- count: 要读取的数据个数 -- dataTypeStr: 数据类型,从dataType表中选择 -- 返回值: 包含所有解析后数值的数组 function readRegisterMultiple(commIndex, registerIndex, order, count, dataTypeStr) -- 设置默认参数 commIndex = commIndex or 0 --默认通信索引为0 order = order or orderCode.ABCD --设置默认字节序 -- 计算需要读取的字节数 local byteCount if dataTypeStr == dataType.SHORT then byteCount = count * 2 elseif dataTypeStr == dataType.INT or dataTypeStr == dataType.FLOAT then byteCount = count * 4 else LuaLog:Debug("不支持的数据类型: " .. tostring(dataTypeStr)) end -- 计算需要读取的寄存器数量(每个寄存器2字节) --local registerCount = byteCount / 4 local registerCount = math.ceil(byteCount / 2) LuaLog:Debug(string.format("批量读取寄存器: 起始地址=%d, 数据类型=%s, 数据个数=%d, 字节数=%d", registerIndex, dataTypeStr, count, byteCount)) -- 批量读取寄存器 local byteArray = LuaCommu:ReadRegisterByteArray(commIndex, registerIndex, byteCount) if byteArray == nil or byteArray:size() ~= byteCount*2 then LuaLog:Debug("读取寄存器失败或字节数不正确") end LuaLog:Debug("读取到的原始字节长度: " ..byteArray:size()) LuaLog:Debug("期望读取到的原始字节长度: " ..byteCount*2) LuaLog:Debug("读取到的原始字节: " .. LuaString:FromByteArray(byteArray, ",")) -- 解析字节数组 local result = {} local elementSize -- 根据数据类型确定每个元素的大小 if dataTypeStr == dataType.SHORT then elementSize = 2 else elementSize = 4 end -- 遍历每个数据元素 for i = 1, count do local startPos = (i - 1) * elementSize local elementBytes = LuaCommu:midBytearray(byteArray, startPos, elementSize) -- 根据数据类型解析字节 local value if dataTypeStr == dataType.SHORT then value = LuaCommu:byteArray_short(elementBytes, order, -1) -- -1 表示有符号 elseif dataTypeStr == dataType.INT then value = LuaCommu:byteArray_int(elementBytes, order, -1) -- -1 表示有符号 elseif dataTypeStr == dataType.FLOAT then value = LuaCommu:byteArray_float(elementBytes, order, -1) end result[i] = value LuaLog:Debug(string.format("数据%d: %s (原始字节: %s)", i, tostring(value), LuaString:FromByteArray(elementBytes, ","))) end return result end -- 批量写入寄存器函数 -- 参数说明: -- commIndex: 通信通道索引,默认为0 -- registerIndex: 起始寄存器地址 -- values: 要写入的数值数组 -- dataTypeStr: 数据类型,从dataType表中选择 -- order: 字节序,从orderCode表中选择,默认为"ABCD" -- 返回值: 无 function writeRegisterMultiple(commIndex, registerIndex, values, dataTypeStr, order) -- 设置默认参数 commIndex = commIndex or 0 --默认通信索引为0 order = order or orderCode.ABCD --设置默认字节序 -- 检查输入参数 if type(values) ~= "table" or #values == 0 then LuaLog:Debug("values必须是非空数组") end LuaLog:Debug(string.format("批量写入寄存器: 起始地址=%d, 数据类型=%s, 数据个数=%d", registerIndex, dataTypeStr, #values)) -- 创建字节数组 local byteArray -- 根据数据类型处理第一个值 if dataTypeStr == dataType.SHORT then byteArray = LuaCommu:short_bytearray(values[1], order) elseif dataTypeStr == dataType.INT then byteArray = LuaCommu:int_bytearray(values[1], order) elseif dataTypeStr == dataType.FLOAT then byteArray = LuaCommu:float_bytearray(values[1], order) else LuaLog:Debug("不支持的数据类型: " .. tostring(dataTypeStr)) end -- 处理剩余的值 for i = 2, #values do local valueBytes if dataTypeStr == dataType.SHORT then valueBytes = LuaCommu:short_bytearray(values[i], order) elseif dataTypeStr == dataType.INT then valueBytes = LuaCommu:int_bytearray(values[i], order) elseif dataTypeStr == dataType.FLOAT then valueBytes = LuaCommu:float_bytearray(values[i], order) end -- 追加到字节数组 byteArray:appendByteArray(valueBytes) end LuaLog:Debug("写入的字节数据: " .. LuaString:FromByteArray(byteArray, ",")) -- 批量写入寄存器 LuaCommu:WriteRegisterByteArray(commIndex, registerIndex, byteArray) LuaLog:Debug("批量写入完成") end ``` ## 完整示例 ### 示例1:完整的工作流程 ```lua -- 1. 定义字节序和数据类 local orderCode = { ABCD = "ABCD", BADC = "BADC", CDAB = "CDAB", DCBA = "DCBA" } local dataType = { SHORT = "short", INT = "int", FLOAT = "float" } -- 四舍五入 function roundNumberSigned(value, decimals) decimals = decimals or 0 local multiplier = 10 ^ decimals if value >= 0 then return math.floor(value * multiplier + 0.5) / multiplier else return math.ceil(value * multiplier - 0.5) / multiplier end end -- 使用示例 local posValue = 12.345 local negValue = -12.345 LuaLog:Debug("正数四舍五入: " .. roundNumberSigned(posValue, 2)) -- 12.35 LuaLog:Debug("负数四舍五入: " .. roundNumberSigned(negValue, 2)) -- -12.35 -- 格式化显示(不改变实际值) function formatDisplay(value, decimals) decimals = decimals or 2 return string.format("%." .. tostring(decimals) .. "f", value) end -- 使用示例 local value = 12.345678 LuaLog:Debug("格式化显示: " .. formatDisplay(value, 2)) -- "12.35" LuaLog:Debug("实际值仍是: " .. value) -- 12.345678 -- 批量读取寄存器函数 -- 参数说明: -- commIndex: 通信通道索引,默认为0 -- registerIndex: 起始寄存器地址 -- order: 字节序,从orderCode表中选择 -- count: 要读取的数据个数 -- dataTypeStr: 数据类型,从dataType表中选择 -- 返回值: 包含所有解析后数值的数组 function readRegisterMultiple(commIndex, registerIndex, order, count, dataTypeStr) -- 设置默认参数 commIndex = commIndex or 0 --默认通信索引为0 order = order or orderCode.ABCD --设置默认字节序 -- 计算需要读取的字节数 local byteCount if dataTypeStr == dataType.SHORT then byteCount = count * 2 elseif dataTypeStr == dataType.INT or dataTypeStr == dataType.FLOAT then byteCount = count * 4 else LuaLog:Debug("不支持的数据类型: " .. tostring(dataTypeStr)) end -- 计算需要读取的寄存器数量(每个寄存器2字节) --local registerCount = byteCount / 4 local registerCount = math.ceil(byteCount / 2) LuaLog:Debug(string.format("批量读取寄存器: 起始地址=%d, 数据类型=%s, 数据个数=%d, 字节数=%d", registerIndex, dataTypeStr, count, byteCount)) -- 批量读取寄存器 local byteArray = LuaCommu:ReadRegisterByteArray(commIndex, registerIndex, byteCount) if byteArray == nil or byteArray:size() ~= byteCount*2 then LuaLog:Debug("读取寄存器失败或字节数不正确") end LuaLog:Debug("读取到的原始字节长度: " ..byteArray:size()) LuaLog:Debug("期望读取到的原始字节长度: " ..byteCount*2) LuaLog:Debug("读取到的原始字节: " .. LuaString:FromByteArray(byteArray, ",")) -- 解析字节数组 local result = {} local elementSize -- 根据数据类型确定每个元素的大小 if dataTypeStr == dataType.SHORT then elementSize = 2 else elementSize = 4 end -- 遍历每个数据元素 for i = 1, count do local startPos = (i - 1) * elementSize local elementBytes = LuaCommu:midBytearray(byteArray, startPos, elementSize) -- 根据数据类型解析字节 local value if dataTypeStr == dataType.SHORT then value = LuaCommu:byteArray_short(elementBytes, order, -1) -- -1 表示有符号 elseif dataTypeStr == dataType.INT then value = LuaCommu:byteArray_int(elementBytes, order, -1) -- -1 表示有符号 elseif dataTypeStr == dataType.FLOAT then value = LuaCommu:byteArray_float(elementBytes, order, -1) end result[i] = value LuaLog:Debug(string.format("数据%d: %s (原始字节: %s)", i, tostring(value), LuaString:FromByteArray(elementBytes, ","))) end return result end -- 批量写入寄存器函数 -- 参数说明: -- commIndex: 通信通道索引,默认为0 -- registerIndex: 起始寄存器地址 -- values: 要写入的数值数组 -- dataTypeStr: 数据类型,从dataType表中选择 -- order: 字节序,从orderCode表中选择,默认为"ABCD" -- 返回值: 无 function writeRegisterMultiple(commIndex, registerIndex, values, dataTypeStr, order) -- 设置默认参数 commIndex = commIndex or 0 --默认通信索引为0 order = order or orderCode.ABCD --设置默认字节序 -- 检查输入参数 if type(values) ~= "table" or #values == 0 then LuaLog:Debug("values必须是非空数组") end LuaLog:Debug(string.format("批量写入寄存器: 起始地址=%d, 数据类型=%s, 数据个数=%d", registerIndex, dataTypeStr, #values)) -- 创建字节数组 local byteArray -- 根据数据类型处理第一个值 if dataTypeStr == dataType.SHORT then byteArray = LuaCommu:short_bytearray(values[1], order) elseif dataTypeStr == dataType.INT then byteArray = LuaCommu:int_bytearray(values[1], order) elseif dataTypeStr == dataType.FLOAT then byteArray = LuaCommu:float_bytearray(values[1], order) else LuaLog:Debug("不支持的数据类型: " .. tostring(dataTypeStr)) end -- 处理剩余的值 for i = 2, #values do local valueBytes if dataTypeStr == dataType.SHORT then valueBytes = LuaCommu:short_bytearray(values[i], order) elseif dataTypeStr == dataType.INT then valueBytes = LuaCommu:int_bytearray(values[i], order) elseif dataTypeStr == dataType.FLOAT then valueBytes = LuaCommu:float_bytearray(values[i], order) end -- 追加到字节数组 byteArray:appendByteArray(valueBytes) end LuaLog:Debug("写入的字节数据: " .. LuaString:FromByteArray(byteArray, ",")) -- 批量写入寄存器 LuaCommu:WriteRegisterByteArray(commIndex, registerIndex, byteArray) LuaLog:Debug("批量写入完成") end -- 2. 单点读写测试 LuaLog:Debug("=== 单点读写测试 ===") -- 浮点数读写 local floatVal = 15.235 LuaCommu:WriteRegisterReal(0, 100, floatVal) local readFloat = LuaCommu:ReadRegisterReal(0, 100, 1) LuaLog:Debug("浮点数: 写入=" .. floatVal .. ", 读取=" .. readFloat) LuaLog:Debug(" 保留2位小数: " .. string.format("%.2f", floatVal) -- 长整数读写 local longVal = 554 LuaCommu:WriteRegisterLong(0, 110, longVal) local readLong = LuaCommu:ReadRegisterLong(0, 110, 1) LuaLog:Debug("长整数: 写入=" .. longVal .. ", 读取=" .. readLong) -- 短整数读写 local shortVal = 124 LuaCommu:WriteRegisterShort(0, 120, shortVal) local readShort = LuaCommu:ReadRegisterShort(0, 120, 1) LuaLog:Debug("短整数: 写入=" .. shortVal .. ", 读取=" .. readShort) -- 3. 批量读写测试 LuaLog:Debug("=== 批量读写测试 ===") -- 批量写入SHORT类型数据 local shortValues = {100, 200, 300} writeRegisterMultiple(0, 500, shortValues, dataType.SHORT, orderCode.ABCD) -- 批量写入INT类型数据 local intValues = {123, 2000, 3000, 987} writeRegisterMultiple(0, 600, intValues, dataType.INT, orderCode.ABCD) -- 批量写入FLOAT类型数据 local floatValues = {12.34, 56.78, 90.12} writeRegisterMultiple(0, 700, floatValues, dataType.FLOAT, orderCode.ABCD) -- 批量读取验证 LuaLog:Debug("=== 批量读取验证 ===") -- 读取SHORT数据 local retData = readRegisterMultiple(0, 500, orderCode.ABCD, 3, dataType.SHORT) LuaLog:Debug("SHORT读取成功: " .. table.concat(retData, ", ")) -- 读取INT数据 local retData1 = readRegisterMultiple(0, 600, orderCode.ABCD, 4, dataType.INT) LuaLog:Debug("INT读取成功: " .. table.concat(retData1, ", ")) -- 读取FLOAT数据 local retData2 = readRegisterMultiple(0, 700, orderCode.ABCD, 3, dataType.FLOAT) LuaLog:Debug("FLOAT读取成功: " .. table.concat(retData2, ", ")) LuaLog:Debug("保留两位小数格式化: " .. tostring(retData2[1])) -- 4. 字节数组操作示例 LuaLog:Debug("=== 字节数组操作示例 ===") -- 批量读寄存器 local byteArray = LuaCommu:ReadRegisterByteArray(0, 100, 2) LuaLog:Debug("读取寄存器的值:" .. LuaString:FromByteArray(byteArray, "-")) -- 截取并转换 local midVal = LuaCommu:midBytearray(byteArray, 0, 4) local intValue = LuaCommu:byteArray_int(midVal, "ABCD", -1) LuaLog:Debug("转换后的整数值: " .. intValue) -- 批量写入INT local writeArray = LuaCommu:int_bytearray(12, "ABCD") local writeArray1 = LuaCommu:int_bytearray(13, "ABCD") writeArray:appendByteArray(writeArray1) LuaCommu:WriteRegisterByteArray(0, 800, writeArray) LuaLog:Debug("=== 测试完成 ===") ``` ### 示例2:字节数组详细操作 ```lua -- 1. 批量写入不同类型数据 -- 批量写入INT local intArray = LuaCommu:int_bytearray(12, "ABCD") local intArray1 = LuaCommu:int_bytearray(13, "ABCD") intArray:appendByteArray(intArray1) LuaCommu:WriteRegisterByteArray(0, 50, intArray) -- 批量写入SHORT local shortArray = LuaCommu:short_bytearray(1, "ABCD") local shortArray1 = LuaCommu:short_bytearray(0, "ABCD") local shortArray2 = LuaCommu:short_bytearray(1, "ABCD") shortArray:appendByteArray(shortArray1) shortArray:appendByteArray(shortArray2) LuaCommu:WriteRegisterByteArray(0, 60, shortArray) -- 批量写入float local floatArray = LuaCommu:float_bytearray(12.12, "ABCD") local floatArray1 = LuaCommu:float_bytearray(13.54, "ABCD") floatArray:appendByteArray(floatArray1) LuaCommu:WriteRegisterByteArray(0, 70, floatArray) -- 2. 批量读取并解析 LuaLog:Debug("=== 读取并解析写入的数据 ===") -- 读取INT数据 local readIntBytes = LuaCommu:ReadRegisterByteArray(0, 50, 8) -- 2个INT需要8个字节 if readIntBytes then -- 解析第一个INT local intPart1 = LuaCommu:midBytearray(readIntBytes, 0, 4) local intValue1 = LuaCommu:byteArray_int(intPart1, "ABCD", -1) LuaLog:Debug("第一个INT值: " .. intValue1) -- 解析第二个INT local intPart2 = LuaCommu:midBytearray(readIntBytes, 4, 4) local intValue2 = LuaCommu:byteArray_int(intPart2, "ABCD", -1) LuaLog:Debug("第二个INT值: " .. intValue2) end -- 读取SHORT数据 local readShortBytes = LuaCommu:ReadRegisterByteArray(0, 60, 6) -- 3个SHORT需要6个字节 if readShortBytes then for i = 0, 2 do local shortPart = LuaCommu:midBytearray(readShortBytes, i * 2, 2) local shortValue = LuaCommu:byteArray_short(shortPart, "ABCD", -1) LuaLog:Debug("SHORT值" .. (i + 1) .. ": " .. shortValue) end end ``` ### 示例3:错误处理和调试 ```lua -- 错误处理和调试示例 LuaLog:Debug("=== 开始寄存器读写测试 ===") -- 测试1:读取不存在的寄存器 local success, result = pcall(function() return LuaCommu:ReadRegisterReal(0, 9999, 1) end) if not success then LuaLog:Debug("读取不存在的寄存器失败: " .. tostring(result)) else LuaLog:Debug("读取结果: " .. tostring(result)) end -- 测试2:使用自定义函数批量读取 local function safeReadMultiple(commIndex, registerIndex, order, count, dataTypeStr) local status, result = pcall(function() return readRegisterMultiple(commIndex, registerIndex, order, count, dataTypeStr) end) if status then return result else LuaLog:Debug("批量读取失败: " .. tostring(result)) return {} end end -- 安全读取 local data = safeReadMultiple(0, 100, orderCode.ABCD, 2, dataType.SHORT) if #data > 0 then LuaLog:Debug("成功读取到 " .. #data .. " 个数据") for i, val in ipairs(data) do LuaLog:Debug("数据" .. i .. ": " .. val) end end -- 测试3:验证写入和读取的一致性 local testValue = 1234.5678 LuaLog:Debug("测试值: " .. testValue) -- 写入 LuaCommu:WriteRegisterReal(0, 1000, testValue) -- 读取 local readValue = LuaCommu:ReadRegisterReal(0, 1000, 1) -- 比较(考虑浮点数精度) local tolerance = 0.001 if math.abs(readValue - testValue) < tolerance then LuaLog:Debug("✓ 数据一致性验证通过") else LuaLog:Debug("✗ 数据一致性验证失败: 写入=" .. testValue .. ", 读取=" .. readValue) end ``` ## 注意事项 ### 1. 寄存器地址计算 - 每个寄存器通常为2个字节 - 批量读取时,函数会自动计算需要的寄存器数量 - 对于INT和FLOAT类型(4字节),每个数据占用2个寄存器 ### 2. 字节序说明 - **ABCD**: 大端序,最高有效字节在前(默认) - **DCBA**: 小端序,最低有效字节在前 - **BADC**: 字节交换 - **CDAB**: 字交换 ### 3. 数据类型 - **SHORT**: 16位有符号整数 - **INT**: 32位有符号整数 - **FLOAT**: 32位单精度浮点数 ## 故障排除 ### 常见问题 1. **读取数据不正确** - 检查字节序设置是否与设备匹配 - 验证寄存器地址是否正确 - 确认数据类型是否匹配 2. **写入失败** - 检查寄存器是否可写 - 验证数值是否在允许范围内 - 确认通信通道是否正常 3. **批量操作问题** - 确认读取的字节数是否正确 - 检查字节数组的大小是否匹配 - 验证数据解析方式是否正确 ### 调试建议 1. **使用日志打印** ```lua LuaLog:Debug("当前操作: 读取寄存器 " .. registerIndex) ``` 2. **检查原始数据** ```lua local rawBytes = LuaCommu:ReadRegisterByteArray(0, 100, 2) LuaLog:Debug("原始字节: " .. LuaString:FromByteArray(rawBytes, "-")) ``` 3. **验证通信状态** ```lua -- 尝试读取已知寄存器 local testRead = LuaCommu:ReadRegisterShort(0, 0, 1) if testRead then LuaLog:Debug("通信正常") else LuaLog:Debug("通信失败") end ``` ## 版本历史 | 版本 | 日期 | 说明 | |------|------|------| | 1.0 | 2024-01-20 | 基于原始代码整理,包含单点和批量读写 | ## 作者 - 基于工中使用到的代码整理 - 适用于工业自动化场景 - 作者整理代码时难免有疏漏存在,如果你把他使用在生产环境中时,请自行检查代码。如有发现有问题存在,期望你提交存在的问题,作者会积极进行修改
李玉军
2026年4月24日 11:24
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码