本章节以“通过脚本控制通信数据收发”为例,介绍脚本模块如何在方案中应用,以及方案最终实现效果。
本节内容包含:
假设某视觉业务场景需要满足如下需求:
需求 |
描述 |
---|---|
需求1 |
VM接收到TCP客户端发送的任意字符串(string)型数据时,触发VM执行流程。 |
需求2 |
流程执行完成时,VM输出int、float、string、IMAGE、byte和ROIBOX这6种类型的数据。 |
需求3 |
需求2中提及的各类型数据,需输出回至TCP客户端。 |
基于上述方案思路,可按以下四大步骤搭建该方案。
为实现接收TCP客户端发送的数据,需在VM中启用TCP客户端并完成其IP地址和端口号等配置。
已启用TCP客户端。
本示例中假设目标TCP客户端的IP地址为127.0.0.1,端口号为7920。
至此,流程搭建完成。
已在流程中调用脚本模块,且已在该模块定义其输入/输出变量。
以上步骤的示例代码如下:
using System; using System.Text; using System.Windows.Forms; using Script.Methods; public partial class UserScript : ScriptMethods, IProcessMethods { //the count of process //执行次数计数 int processCount; /// <summary> /// Initialize the field's value when compiling /// 预编译时变量初始化 /// </summary> public void Init() { //You can add other global fields here //变量初始化,其余变量可在该函数中添加 processCount = 0; } /// <summary> /// Enter the process function when running code once /// 流程执行一次进入Process函数 /// </summary> /// <returns></returns> public bool Process() { try { //1.int数据测试 int na = 0, ncount = 0; GetIntValue("in0", ref na); SetIntValue("out0", na); int[] narry = new int[5]; //获取int数组 GetIntArrayValue("in5", ref narry, out ncount); //设置int数组方式1 for (int i = 0; i < Math.Min(ncount, 5); i++) { SetIntValueByIndex("out5", narry[i], i, Math.Min(ncount, 5)); } //设置int数组方式2 //SetIntArrayValue("out5",narry,0,narry.Length); //2.float数据测试 float fa = 0; int fcount = 0; GetFloatValue("in1", ref fa); SetFloatValue("out1", fa); float[] farry = new float[5]; //获取float数组 GetFloatArrayValue("in6", ref farry, out fcount); //设置float数组方式1 for (int i = 0; i < Math.Min(fcount, 5); i++) { SetFloatValueByIndex("out6", farry[i], i, Math.Min(fcount, 5)); } //设置float数组方式2 //SetFloatArrayValue("out6",farry,0,farry.Length); //3.string数据测试 string stra = ""; int strcount = 0; GetStringValue("in2", ref stra); SetStringValue("out2", stra); string[] strarry = new string[5]; //获取string数组 GetStringArrayValue("in7", ref strarry, out strcount); //设置string数组方式1 for (int i = 0; i < Math.Min(strcount, 5); i++) { SetStringValueByIndex("out7", strarry[i], i, Math.Min(strcount, 5)); } //设置string数组方式2 //SetStringArrayValue("out7",strarry,0,strarry.Length); //6.设置/获取16进制数据 byte[] tempBytes = new byte[] { }; GetBytesValue("in4", ref tempBytes); SetBytesValue("out4", tempBytes); //7.设置/获取图像数据 ImageData imagedata = new ImageData(); int n1 = GetImageValue("in3", ref imagedata); int n = SetImageValue("out3", imagedata); //8.获取roibox数据 RoiboxData roibox = new RoiboxData(); GetRoiboxValue("in8", ref roibox); SetRoiboxValue("out8",roibox); //获取roi数组 RoiboxData[] roiboxArray = new RoiboxData[100]; int boxcount = 0; GetRoiBoxArrayValue("in8", ref roiboxArray, out boxcount); SetRoiBoxArrayValue("out9", roiboxArray, 0, boxcount); } catch (Exception ex) { ShowMessageBox(ex.ToString()); } return true; } }
为了实现在VM接收到TCP客户端发送的字符串时触发流程执行,需配置全局触发。全局触发是VM的配置工具,用于配置针对方案的全局触发规则。
已完成流程搭建。
完成方案搭建后,可使用市面上的网络调试工具对方案效果进行测试。
从以下两张截图可见,VM的流程最终输出的int、float、string、IMAGE、byte和ROIBOX这6种类型的数据,发送到了TCP客户端。该方案最终实现了业务需求:“VM接收到TCP客户端发送的任意字符串时,触发流程执行,并将流程输出的6中类型的数据发送至TCP客户端”。