Lua脚本
Lua标准手册
VG3扩展功能手册
VG3 Lua脚本Demo
VG3 Lua手册索引
Lua脚本公共库
PublicDataFunc
PublicMathFunc
LuaModbus读写应用示例
通过TCP触发流程与写入全局变量
【技术文档】VG3 Lua API 技术文档
【技术文档】LuaGlobalVar — 全局变量操作
【技术文档】ZMotion - 运动控制模块
本站点使用 MrDoc 构建
-
+
VG3 Lua脚本Demo
> 此文件包含VG3 Lua脚本的实用示例,展示了VG3框架的核心功能 ## 示例1:基础类型和变量操作 ```lua -- VG3基础类型演示 LuaLog:Debug("=== VG3基础类型演示 ===") -- 创建不同类型的变量 local boolVar = CMvVariant(cyDType.cyDT_Bool):bool(true) local intVar = CMvVariant(cyDType.cyDT_Long):long(42) local floatVar = CMvVariant(cyDType.cyDT_Real):real(3.14159) local strVar = CMvVariant(cyDType.cyDT_String):string("Hello VG3 Lua") LuaLog:Debug("布尔值:", boolVar) LuaLog:Debug("整数:", intVar) LuaLog:Debug("浮点数:", floatVar) LuaLog:Debug("字符串:", strVar) ``` ## 示例2:几何类型操作 ```lua -- 几何类型演示 LuaLog:Debug("\n=== 几何类型演示 ===") -- 创建点对象 local point = CMvVariant(cyDType.cyDT_RealPoint):point({x = 10.5, y = 20.3}) LuaLog:Debug("点坐标: x=", point.x, " y=", point.y) -- 创建矩形 local rect = CMvVariant(cyDType.cyDT_Rect):rect({left = 5, top = 5, width = 100, height = 50}) LuaLog:Debug("矩形: 左=", rect.left, " 上=", rect.top, " 宽=", rect.width, " 高=", rect.height) -- 创建圆 local circle = CMvVariant(cyDType.cyDT_Circle):circle({x = 50, y = 50, radius = 25}) LuaLog:Debug("圆: 中心x=", circle.x, " 中心y=", circle.y, " 半径=", circle.radius) ``` ## 示例3:数组操作 ```lua -- 数组操作演示 LuaLog:Debug("\n=== 数组操作演示 ===") -- 创建姿态数组 local postureArr = CMvVariant() postureArr:postureArray() -- fill方法: 参数1---需要填充的数据;参数2---填充的个数 postureArr:fill(cyPosture(1, 2, 3), 5) LuaLog:Debug("postureArr fill:", postureArr) -- resize方法: 参数1--修改后的个数 postureArr:resize(3) LuaLog:Debug("postureArr resize:", postureArr) -- 转字符串: 结构体间使用英文逗号(,)分割,数组间使用英文分号进行(;)分割 local postureArrString = postureArr:toString() LuaLog:Debug("postureArrString", postureArrString) -- 字符串转: 参数1--数据类型;参数2--需要转换的字符串 local posturesString = "1,2,3;4,5,6" local postures = CMvVariant() postures:fromString(cyDType.cyAT_Posture, posturesString) LuaLog:Debug("postures:", postures) -- 点数组操作演示 LuaLog:Debug("\n=== 点数组操作演示 ===") -- 创建点数组 local point = CMvVariant() if point:isNull() then LuaLog:Debug("point为空数值") end point:realPointArray() -- 追加到尾部: 参数1--追加的数据 point:pushBack(cyRealPoint(1.2, 2.5)) point:pushBack(cyRealPoint(3.1, 2.3)) -- 追加到头部: 参数1--追加的数据 point:pushFront(cyRealPoint(4.2, 2.2)) -- 插入: 参数1--插入的数据的位置;参数2--插入的数据 point:insert(1, cyRealPoint(11, 22)) LuaLog:Debug("point:", point) if point:isArray() then LuaLog:Debug("是数组类型") end -- 获取长度 local len = point:length() LuaLog:Debug("len:", len) -- 直接访问数组元素 LuaLog:Debug("point[0].x:", point[0].x) point[0].x = 123.4 LuaLog:Debug("point:", point) -- 获取数组中对应的参数,修改参数会同时修改数组中的内容: 参数1--需要设置的数据的位置 local point_0 = point:get(0) LuaLog:Debug(point_0) point_0.x = 56.78 LuaLog:Debug("point", point) -- 拷贝数据后,不会改变原有的数据 local pointCopy = point_0:copy() pointCopy.x = 234.56 LuaLog:Debug("拷贝point", point) -- 设置: 参数1--需要设置的数据的位置;参数2--需要设置的数据 local newPoint = cyRealPoint(89, 99) point:set(1, newPoint) LuaLog:Debug("point", point) -- 移除最后一个 point:removeLast() LuaLog:Debug("point:", point) -- 移除最前面一个 point:removeFirst() LuaLog:Debug("point:", point) -- resize: 参数1--修改后的个数 point:resize(10) LuaLog:Debug("point:", point) -- remove: 参数1--删除指定位置的数值 point:remove(0) LuaLog:Debug("point:", point) -- mid: 返回截取后的数组,参数1--开始截取的位置(从0开始),参数2--截取的长度 local midPoint = point:mid(0, 4) LuaLog:Debug("midPoint:", midPoint) -- clear: 删除数组中所有的元素,长度转为0 point:clear() LuaLog:Debug("point:", point) -- 不同类型数组演示 LuaLog:Debug("\n=== 不同类型数组演示 ===") -- 布尔值数组 local boolArr = CMvVariant() boolArr:boolArray() boolArr:pushBack(true) boolArr:pushBack(false) boolArr:pushBack(true) LuaLog:Debug("boolArr:", boolArr) -- 浮点数数组 local realArr = CMvVariant() realArr:realArray() realArr:pushBack(1.1) realArr:pushBack(2.2) realArr:pushBack(3.3) LuaLog:Debug("realArr:", realArr) -- 字符串数组 local strArr = CMvVariant() strArr:stringArray() strArr:pushBack("hello") strArr:pushBack("world") strArr:pushBack("VG3") LuaLog:Debug("strArr:", strArr) ``` ## 示例4:VG3文件操作 ```lua -- VG3扩展功能演示 LuaLog:Debug("=== VG3扩展功能演示 ===") -- 文件操作示例 -- 读取文本文件 local readVal = LuaFile:ReadTxt("D:\\VisionGroup3\\1.txt") LuaLog:Debug("读取文本文件内容:", readVal) -- 读取CSV文件 local readVal = LuaFile:ReadCSV("D:\\VisionGroup3\\1.csv") LuaLog:Debug("读取CSV文件内容:", readVal) -- 创建新文本文件 local isSuc = LuaFile:NewTxt("D:\\VisionGroup3\\1.txt", "123") LuaLog:Debug("创建文本文件是否成功:", isSuc) -- 创建新目录 LuaFile:NewDirct("D:\\VisionGroup3\\NewDir\\") LuaLog:Debug("目录创建完成") -- 创建新CSV文件 local isSuc = LuaFile:NewCSV("D:\\VisionGroup3\\1.csv", "1,2,3") LuaLog:Debug("创建CSV文件是否成功:", isSuc) -- 检查文件是否存在 local isExit = LuaFile:ExistFile("D:\\VisionGroup3\\opencv_world490.dll") LuaLog:Debug("文件是否存在:", isExit) -- 检查目录是否存在 local isExit = LuaFile:ExistDirct("D:\\VisionGroup3\\") LuaLog:Debug("目录是否存在:", isExit) -- 删除文件 LuaFile:DelFile("D:\\VisionGroup3\\1.txt") LuaLog:Debug("文件删除完成") -- 删除目录(谨慎使用) local isExit = LuaFile:DelDirct("D:\\VisionGroup3\\") LuaLog:Debug("目录删除是否成功:", isExit) -- 追加文本到文件 local isSuc = LuaFile:AppendTxt("D:\\VisionGroup3\\1.txt", "123") LuaLog:Debug("追加文本是否成功:", isSuc) -- 追加行到CSV文件 local isSuc = LuaFile:AppendLineCSV("D:\\VisionGroup3\\1.csv", "1,2,3") LuaLog:Debug("追加CSV行是否成功:", isSuc) -- 日志功能演示 LuaLog:Debug("VG3 Lua Demo开始执行") LuaLog:WriteLog("这是一个信息日志") LuaLog:WriteLog("这是一个警告信息") LuaLog:WriteLog("这是一个错误信息") -- 系统信息(如果VG3框架提供系统函数) -- local sysFunc = GMvLuaSystemFunc() -- LuaLog:Debug("当前时间:", sysFunc:GetCurrentTime()) -- LuaLog:Debug("系统版本:", sysFunc:GetSystemVersion()) -- 实用示例:文件备份功能 function fileBackup(sourcePath, backupPath) LuaLog:Debug("开始备份文件:", sourcePath) -- 检查源文件是否存在 if not LuaFile:ExistFile(sourcePath) then LuaLog:WriteLog("源文件不存在:" .. sourcePath) LuaLog:Debug("源文件不存在,创建示例文件进行测试") -- 创建示例源文件 local createSuccess = LuaFile:NewTxt(sourcePath, "VG3配置示例文件\ 版本: 1.0\ 创建时间: 2024\ ") if not createSuccess then LuaLog:WriteLog("无法创建示例源文件:" .. sourcePath) return false end end -- 提取备份目录 local backupDir = backupPath:match("(.+)\\[^\\]+$") if backupDir and not LuaFile:ExistDirct(backupDir) then LuaLog:Debug("创建备份目录:", backupDir) LuaFile:NewDirct(backupDir) end -- 读取源文件内容 local content = LuaFile:ReadTxt(sourcePath) if not content then LuaLog:WriteLog("无法读取源文件:" .. sourcePath) return false end -- 写入备份文件 local success = LuaFile:NewTxt(backupPath, content) if success then LuaLog:WriteLog("文件备份成功:" .. sourcePath .. " -> " .. backupPath) local backupSize = LuaFile:ReadTxt(backupPath) if backupSize then LuaLog:Debug("备份文件大小:", #backupSize, "字节") end else LuaLog:WriteLog("文件备份失败:" .. sourcePath) -- 检查备份目录权限 LuaLog:Debug("检查备份目录权限:", backupDir) end return success end -- 测试文件备份功能 local sourceFile = "D:\\VisionGroup3\\config.txt" local backupFilePath = "D:\\VisionGroup3\\backup\\config_backup.txt" -- 先确保备份目录存在 local backupDir = "D:\\VisionGroup3\\backup" if not LuaFile:ExistDirct(backupDir) then LuaLog:Debug("创建备份目录:", backupDir) LuaFile:NewDirct(backupDir) -- 验证目录是否创建成功 if LuaFile:ExistDirct(backupDir) then LuaLog:Debug("备份目录创建成功") else LuaLog:Debug("备份目录创建失败,尝试使用当前目录") backupFilePath = "D:\\VisionGroup3\\config_backup.txt" end end -- 确保源文件存在 if not LuaFile:ExistFile(sourceFile) then LuaLog:Debug("创建示例源文件") LuaFile:NewTxt(sourceFile, "VG3配置示例文件\ 版本: 1.0\ 创建时间: 2024\ ") end local backupResult = fileBackup(sourceFile, backupFilePath) LuaLog:Debug("文件备份操作结果:", backupResult) -- 验证备份是否成功 if backupResult then if LuaFile:ExistFile(backupFilePath) then LuaLog:Debug("备份文件验证成功,文件存在:", backupFilePath) else LuaLog:Debug("备份文件验证失败,文件不存在:", backupFilePath) end else LuaLog:Debug("备份函数返回失败") end ``` ## 示例5:几何计算和算法 ```lua -- 几何计算演示 LuaLog:Debug("\n=== 几何计算演示 ===") -- 计算两点距离 function calculateDistance(p1, p2) local dx = p2.x - p1.x local dy = p2.y - p1.y return math.sqrt(dx*dx + dy*dy) end local point1 = {x = 0, y = 0} local point2 = {x = 3, y = 4} local distance = calculateDistance(point1, point2) LuaLog:Debug("两点距离:", distance) -- 检查点是否在矩形内 function isPointInRect(point, rect) return point.x >= rect.left and point.x <= rect.left + rect.width and point.y >= rect.top and point.y <= rect.top + rect.height end local testPoint = {x = 30, y = 20} local testRect = {left = 10, top = 10, width = 50, height = 40} if isPointInRect(testPoint, testRect) then LuaLog:Debug("点在矩形内") else LuaLog:Debug("点在矩形外") end ``` ## 示例6:实用函数集合 ```lua -- 实用函数集合 LuaLog:Debug("\n=== 实用函数集合 ===") ``` ## 示例7:完整的工作流程 ```lua -- 完整的工作流程示例 LuaLog:Debug("=== 完整工作流程示例 ===") function processVisionTask() -- 1. 初始化变量 local results = {} -- 2. 创建几何对象 local searchArea = CMvVariant(cyDType.cyDT_Rect):rect({ left = 0, top = 0, width = 640, height = 480 }) -- 创建点数组(使用正确的VG3方式) local targetPoints = CMvVariant() targetPoints:realPointArray() -- 3. 模拟检测过程 for i = 1, 5 do local x = math.random(100, 500) local y = math.random(100, 400) -- 使用pushBack方法添加点 targetPoints:pushBack(cyRealPoint(x, y)) LuaLog:Debug("检测到目标点[", i, "]: x=", x, " y=", y) end -- 4. 计算结果 local centerX, centerY = 0, 0 local pointCount = targetPoints:length() for i = 0, pointCount - 1 do local point = targetPoints:get(i) centerX = centerX + point.x centerY = centerY + point.y end if pointCount > 0 then centerX = centerX / pointCount centerY = centerY / pointCount end LuaLog:Debug("目标中心点: x=", centerX, " y=", centerY) -- 5. 记录日志 LuaLog:WriteLog("视觉任务完成,检测到" .. pointCount .. "个目标点") -- 创建结果点 local centerPoint = CMvVariant() centerPoint:realPointArray() centerPoint:pushBack(cyRealPoint(centerX, centerY)) return { center = centerPoint, pointCount = pointCount, searchArea = searchArea, targetPoints = targetPoints } end -- 执行工作流程 local taskResult = processVisionTask() LuaLog:Debug("任务完成,检测到", taskResult.pointCount, "个目标点") -- 显示检测到的目标点 if taskResult.pointCount > 0 then LuaLog:Debug("所有检测到的目标点:") for i = 0, taskResult.pointCount - 1 do local point = taskResult.targetPoints:get(i) LuaLog:Debug("点[", i, "]: x=", point.x, " y=", point.y) end end ``` ## 示例8:字符串和字节数组操作 ```lua -- 字符串和字节数组操作演示 LuaLog:Debug("=== 字符串和字节数组操作演示 ===") --[[************ 上方为输入输出口定义,请勿删除 ************************]] --字符串转bytearray 参数1:需要转换的字符串 参数2:分割符号 local byteVal = LuaString:ToByteArray("FF CA 00 12 AB", " ") LuaLog:Debug("字符串转字节数组:", LuaString:FromByteArray(byteVal, " ")) --byteArray转字符串 参数1:需要转换的bytearray 参数2:分隔符号 LuaLog:Debug("字节数组转字符串:", LuaString:FromByteArray(byteVal, " ")) --定义一个bytearray local newByteVal = QByteArray() LuaLog:Debug("新字节数组初始状态:", LuaString:FromByteArray(newByteVal, " ")) --后追加一个bytearray newByteVal:appendByteArray(byteVal) LuaLog:Debug("追加字节数组后:", LuaString:FromByteArray(newByteVal, " ")) --获取指定值 序号从0开始 local val_0 = newByteVal:at(1) LuaLog:Debug("获取索引1的值:", val_0) --前追加一个bytearray newByteVal:prependByteArray(byteVal) LuaLog:Debug("前追加字节数组后:", LuaString:FromByteArray(newByteVal, " ")) --插入指定位置一个值 参数1:插入位置 参数2:需要插入的值 newByteVal:insert(2, byteVal) LuaLog:Debug("插入字节数组后:", LuaString:FromByteArray(newByteVal, " ")) --获取bytearray的长度 local len = newByteVal:size() LuaLog:Debug("字节数组长度:", len) --修改bytearray长度 newByteVal:resize(10) LuaLog:Debug("调整长度后:", LuaString:FromByteArray(newByteVal, " ")) --填充 参数1: 填充值 参数2:长度 newByteVal:fill(0xff, 10) LuaLog:Debug("填充0xFF后:", LuaString:FromByteArray(newByteVal, " ")) --设置指定位置值 参数1:设置的序号 参数2:设置值 newByteVal:set(0, 0x0a) LuaLog:Debug("设置索引0为0x0A后:", LuaString:FromByteArray(newByteVal, " ")) --插入值 参数1:插入序号 参数2:插入值 newByteVal:insertChar(2, 0x0a) LuaLog:Debug("插入字符后:", LuaString:FromByteArray(newByteVal, " ")) --后追加一个值 newByteVal:appendChar(0x0e) LuaLog:Debug("后追加字符后:", LuaString:FromByteArray(newByteVal, " ")) --前追加一个值 newByteVal:prependChar(0x0b) LuaLog:Debug("前追加字符后:", LuaString:FromByteArray(newByteVal, " ")) --剪切值 参数1:起始位置 参数2:长度 local midByteVal = newByteVal:mid(2, 3) LuaLog:Debug("剪切中间部分:", LuaString:FromByteArray(midByteVal, " ")) -- 实用示例:处理通信协议数据 function processCommunicationData(hexString) LuaLog:Debug(" === 通信数据处理示例 ===") -- 将十六进制字符串转换为字节数组 local data = LuaString:ToByteArray(hexString, " ") LuaLog:Debug("原始数据:", LuaString:FromByteArray(data, " ")) -- 检查数据长度 local dataLen = data:size() LuaLog:Debug("数据长度:", dataLen) -- 提取数据包头部(前3个字节) local header = data:mid(0, 3) LuaLog:Debug("数据包头部:", LuaString:FromByteArray(header, " ")) -- 计算校验和(示例:简单的累加校验) local checksum = 0 for i = 0, dataLen - 1 do checksum = checksum + data:at(i) end checksum = checksum % 256 LuaLog:Debug("校验和:", string.format("0x%02X", checksum)) return { data = data, length = dataLen, checksum = checksum } end -- 测试通信数据处理 local testData = "55 AA 01 02 03 04 05 06" local result = processCommunicationData(testData) LuaLog:Debug("处理结果 - 长度:", result.length, "校验和:", result.checksum) ``` ## 使用说明 1. **运行环境**:这些示例需要在VG3框架环境中运行 2. **依赖文件**:确保VG3扩展库已正确加载 3. **调试技巧**: - 使用`LuaLog:Debug()`函数输出调试信息 - 利用VG3的日志功能记录重要事件 - 使用`type()`函数检查变量类型 ## 扩展建议 - 根据实际需求修改几何参数 - 添加错误处理机制 - 集成到实际的视觉处理流程中 这个Demo展示了VG3 Lua脚本的核心功能,可以作为学习和开发的基础模板。
唐红
2025年12月15日 12:00
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码