2023年11月30日发(作者:)
基于UI Messages在TestStand和LabVIEW OI之间实现数据
的双向传递
问题:
UI Messages可以将TestStand Engine状态、当前Execution的信息传递至LabVIEW用户界面(Operator
Interface),UI Messages传递的数据类型有三种:数值型(Numeric)、字符串(String)、对象引用(Object
Reference)。如果期望将数组(Numeric Array,String Array,Boolean Array)也从TestStand传递至
OI;另外,如果期望从LabVIEW OI中将数据传递至TestStand,该如何实现。
解答:
在引入以上抛出的问题的解决方法之前,我们先简单介绍一下TestStand的对象模型。TestStand的核
心是TestStand Engine,它是基于ActiveX/COM的自动化服务器(Automation Server),严格的遵循面
向对象编程(OOP)的方式,通过使用方法和属性将访问的接口提供给客户端,这些接口即TestStand API。
通过TestStand API,任何的编程语言如LabVIEW、LabWindows、C#、、C++都可以和TestStand
Engine进行交互,如在LabVIEW Operator Interface(后续简称LabVIEW OI)中,通过TestStand API
连接TestStand Engine,并唤起特定的Sequence执行,最后在LabVIEW OI中显示结果。OI本身并不执
行复杂的序列编辑等工作。
既然是遵循面向对象编程,那么TestStand Engine中所有的对象都将具备封装性、继承性和多态性。
而TestStand中几个比较重要的对象是:Engine Object、SequenceContext Object、RunState Subproperty。
TestStand Engine是TestStand架构的核心,Engine Object有许多方法,如唤起TestStand Engine、
加载文件、发起Execution、用户登录等,这可以通过Application Manager对象来完成(在OI中,每一
个程序都将有一个对应的Application Manager)。如果我们要执行测试,必须在最开始先创建一个
TestStand Engine的实例。
SequenceContext Object是TestStand API中被访问的最频繁的一个对象,因为它代表了一个
Sequence的所有执行状态信息。SequenceContext Object包含了一个特定Sequence在特定时间点的所
有状态和背景信息。每一个处于执行中的Sequence都有自己独立的SequenceContext,所以如果是有四
个Sequences在并行执行,那将会有四个SequenceContext Objects。理解SequenceContext Object是
非常重要的,因为通过它可以访问一个Sequence的所有信息,包括Sequence的所有对象、变量和属性。
从TestStand API Object的关系图中可以看到,SequenceContext Object是位于API Object的顶端,通
过它可以访问当前已加载到内存中的许多对象。并且,在TestStand中你也可以将SequenceContext做为
参数传递至某个Step的Code Module。
图and API Object关系图
表1中描述了SequenceContext所包含的First-Level属性。包含所有的Variables,Parameters,
Globals,RunState等。这里留意一下ThisContext,它的Type是SequenceContext,所以它将包含
Sequence的所有信息。如果我们将ThisContext传递至LabVIEW OI,且如果ThisContext包含的Variables
中有数组,通过传递ThisContext,在LabVIEW OI中我们将有可能获取到数组的值。
Sequence Context Description
Subproperty
Locals
Run-time copy of the sequence local variables for the current sequence
invocation.
Parameters
Run-time copy of the sequence parameters for the current sequence
invocation.
FileGlobals
StationGlobals
Run-time copy of the sequence files global variables for the current execution.
Contains the station global variables for the engine invocation. TestStand
maintains a single copy of the station globals in memory.
ThisContext
Holds a reference to the current sequence context. Usually, you use this
property to pass the entire sequence context as an argument to a subsequence
or a step module
RunState
Contains properties that describe the state of execution in the sequence
invocation.
Step
Run-time copy of the properties in the currently executing step of the current
sequence invocation. The Step property only exists while a step executes. The
property does not exist when the execution is between steps, such as at a
breakpoint.
表1. SequenceContext First-level属性
图ntext的类型和包含的属性
RunState Object是SequenceContext的subproperty,它包含Sequence运行时的状态。如StepIndex,
StepGroup等。这个Object就不做详细介绍。
好了,前面做了这么多的铺垫,就是为了要解决我们在文章开头提出的两个问题:
1)如何将Locals数组从TestStand传递至LabVIEW OI;
2)如何从LabVIEW中将参数传递到TestStand中。
这两个问题的实现方法就是通过传递ThisContext。在TestStand Sequence File中添加
PostUIMessageEx Step,分别将当前Step名称和ThisContext做为参数传递到LabVIEW OI。
图3. PostUIMessageEx Step设置
在LabVIEW OI中,需要添加UserMessage回调函数。在这里,我们将字符串和用户注册事件引用作
为UserMessage回调函数的输入参数:
图4. Configure Event Callbacks
UserMessage回调函数的程序框图如下,StringData返回的是NameOf(Step),传递给属性节点并修改
StepName字符串控件的值。ActiveXData传递的是ThisContext引用,作为事件数据并产生用户事件。
图5. UserMessage回调函数程序框图
在LabVIEW Top-level VI,由于注册了用户事件,因此UserMessage回调函数中产生用户事件激发
Top-Level VI事件结构分支的执行。Variant to Data函数将变体数据转换为ThisContext,使用TestStand
– Get Property 和TestStand – Set Property 函数,结合Lookup String即可以读取或修
改数组。StepName字符串控件的值在UserMessage中被修改,因此用来做为条件结构的输入端,决定具
体对ThisContext执行什么操作。ThisContext引用配合StepName字符串控件的方式提供了一定的灵活
性,大家可以在这个基础之上做修改和拓展。例如,在Sequence File的任意位置添加了PostUIMessageEx
Step,在LabVIEW Top-level VI中只需要在条件结构分支中添加一个分支,而不用修改整个框架。
图6. LabVIEW Top-level VI事件结构处理分支
接下来我们来看一下程序的测试:
在Main Sequence中,Setup Group中添加一个PostUIMessageEx Step,用来接收从OI传递的参数,
并赋给_Delay数组;MainGroup中也添加一个PostUIMessageEx Step,用来将
_Delay数组的值从TestStand传回给OI,这样一个loop back操作即成功验证TestStand和
OI之间基于UIMessage双向通讯的可行性。
图7. Sequence File
在Top-level VI的Configuration页面中,先设置Step_Delay参数,然后读回参数值。
注意:由于ThisContext是当前执行Sequence的引用,当从TestStand传递ThisContext
到LabVIEW之后,LabVIEW通过ThisContext获取局部变量的值,或者修改局部变量
的值。如果ThisContext不存在,例如Sequence已执行完毕了,在LabVIEW中任何
试图使用ThisContext访问Sequence将返回错误“对象不存在”。所以推荐的方式是在
UIMessage后面加一定的延时(如0.5s)。而且,不建议从LabVIEW传递大量数据到
TestStand,大量数据传递到TestStand请使用文件加载。
图8. LabVIEW Top-level VI运行结果


发布评论