LabVIEW功能全局变量

简介: LabVIEW功能全局变量

LabVIEW功能全局变量


功能全局变量(FGV)是一种常用的设计模式。FGV是一个非可重入VI,具有迭代一次的while循环,并具有未初始化的移位寄存器。此构造的目的是在对FGV的连续调用之间保留数据。


可以使用FGV代替全局变量,主要有两个原因:


更改存储值时,可以执行其他任务。因此,FGV有时也被称为动作引擎(AE)。


如果以不安全、意外的方式使用,正常的全局变量可能会导致代码中出现争用条件。FGV可以帮助预防这些。


下面是FGV常见用例的示例。该FGV可用作定时器SubVI。移位寄存器在While循环之外未初始化,这会导致每次循环迭代传递最后一个存储值。在这种情况下,值仅在“初始化”案例中初始化。


21638330cd904915944fd8b8cb86d305.png


当两段并行代码修改同一变量时,可能会出现争用条件。例如,两段代码同时写入变量的值。最后保存变量新值的代码部分将覆盖其他代码的写入值。这可能会导致稍后读取变量时出现意外值。


功能全局变量可用于防止此类修改数据的操作。请注意,代码的所有关键部分必须由不可重入的FGV作为一个整体进行保护;这可确保一次只执行一个操作,因此冲突操作永远不会并行执行。


举个不好的例子,以下两个并行操作会引起争用条件,因为关键的读取-修改-写入操作是在FGV之外实现的:

5879a5fb828247979ed9811513ade0e2.png


在FGV中,代码的关键读取-修改-写入部分已作为FGV的操作实现。由于FGV的非重入设置确保一次只执行一个操作,因此两个调用将始终相互执行,从而防止出现争用条件。


Functionalglobal variable (FGV)


Usea functional global variable (FGV) to communicate between two parallel processloops contained within different VIs under the same target, and use a FGV tostop parallel loops with one "stop" button. The"functional" nature of the FGV means that you can create additionalfunctionality beyond that of a basic global variable, e.g., counting andcalculations that operate on the stored value.


LabVIEWRT block diagram snippet: Functional global variable subVI with unitializedwhile-loop shift register and case structure


Usecases


Communicatedata, control, and status between two or more parallel process loops containedwithin the same target (“target-scoped”), either in the same VI or in differentVIs


Makethe latest value of a variable available to other process loops


Stopmultiple parallel loops from a single control


Performadditional operations (the “functional” aspect of the FGV acronym) beyondmerely storing the data, e.g., counting and calculations


Features


Afunctional global variable is subVI with these defining characteristics:


Uninitializedshift register – an uninitialized shift register on a while-loop causes LabVIEWto allocate storage for a single value that persists as long as the calling VIremains in memory


Single-iterationwhile-loop – the while-loop conditional terminal is wired to a “true” constant,therefore it only iterates once; the while-loop is merely a mechanism to holdthe shift register


Casestructure with enumerated control – the case structure selects an operation toperform on the stored value; “read” and “write” are the most basic operations,and additional cases can implement additional functions such as “increment”,“decrement”, etc.


Non-reentrantsubVI – this execution mode ensures that only one instance of the subVI existsin the target; multiple instances of the subVI all refer to the same storedvalue


Functionalglobal variables offer two advantages over local variables and globalvariables:


Memoryefficiency – the stored value exists in only one place in memory, whereas eachinstance of a local/global variable reader creates its own copy of the data


Avoidsrace conditions – the stored value cannot be written while it is being readbecause the subVI can only be executed by one process at a time


Keepin mind


ThesubVI execution mode must be set to “non-reentrant” (open the subVI, pressCtrl+I, select “Execution” category, and choose “non-reentrant execution”); theother two reentrant execution modes create independent copies (“clones”) of thesubVI, thereby eliminating the global variable aspect of the FGV


LabVIEWblock diagram elements


Thefunctional global variable is a design pattern as opposed to a set of built-inVIs. Refer to the example code below for details.


Examplecode


Connectyour Academic RIO Device to your PC using USBLAN, Ethernet, or Wi-Fi. NOTE: Notall Academic RIO Devices have Ethernet and Wi-Fi connectivity options.


Downloadand unpack the rt_functional-global-variable.zip (for use with NI myRIO 1900)or the NIELVISIII-rt_functional-global-variable.zip (for use with NI ELVIS III)archive, and then double-click the ".lvproj" file to open theproject. NOTE: This project was written for a NI myRIO 1900 or NI ELVIS IIIconnected by USBLAN at IP address 172.22.11.2. If you are using a different IPaddress or another Academic RIO Device (Example: NI myRIO 1950 or NI RIOControl Module) do the following:


Ifusing the NI myRIO 1950 or NI RIO Control Module start with the NI myRIO 1900Archive.


DifferentIP address: Right-click on the "NI myRIO 1900" Device, choose"Properties", and then enter the new IP address


Differentdevice:


Right-clickon the top of the project hierarchy, select "New Targets andDevices", keep the "Existing target or device" option, and thenfind and select your particular device


Selectall of the components under the "NI myRIO 1900" device: click thefirst one and then shift+click the last one


Dragthe selected components to the new device


Right-clickthe "NI myRIO 1900" device and select "Remove from project"


Openthree VIs: “RT Main”, “RT subVI-1”, and “RT subVI-2”


Run“RT Main”


The“fast counter” in Process Loop #1 of “VI one” increments once each loop cycle(100 ms)


TheBoolean “reset” generated by Process Loop #2 of “VI two” resets the fastcounter


The“slow counter” in Process Loop #2 increments each time the fast counter reaches10 counts; this condition also enables the “reset” signal


Openthe three “fgv - ” subVIs to observe the state of the three functional global variables“fast counter”, “reset”, and “all stop”


Stopall VIs by clicking the “Stop” button to set the “all stop” FGV


RTMain


Initializethe “all stop” functional global variable (FGV)


Runtwo subVIs, each containing FGVs


Pollthe “Stop” button and indicate loop activity


Setthe “all stop” FGV when the “Stop” button is clicked


Thetwo VIs read “all stop” FGV for the loop conditional terminal


RTsubVI-1: “fast counter”


Displaysup-counter value as front-panel indicator


Writescounter value to “fast counter” FGV


Resetscounter to zero when “reset” FGV is true


RTsubVI-2: “slow counter”


Displaysup-counter displayed as front-panel indicator


Incrementscounter when the “fast counter” FGV equals 10


Activates“reset” indicator and corresponding FGV when the “fast counter” value equals 10


Functionalglobal variable VIs


Outercase structure implements standard error behavior


While-loopstructure:


Uninitializedshift register allocates persistent storage


Loopconditional wired to “T” means loop runs only once per call


Innercase structure:


Enumerateddata type selects the operation (or “function”) to be performed on the variable


“Read” and “Write” are the most basicoperations


Addadditional operations with more case structure subdiagrams


Mustuse non-reentrant execution mode


需要说明的是,上述的例程和文档,都是可以下载的,双击即可打开,其中压缩文件是可以采用粘贴复制的方式,拷贝到硬盘上。这不是图片,各位小伙伴看到后尝试一下,这个问题就不用加微信咨询了。有关LabVIEW编程、LabVIEW开发等相关项目,可联系们。附件中的资料这里无法上传,可去公司网站搜索下载。


LabVIEW程序,如下附件所示。

相关文章
|
8天前
|
XML 数据格式 Windows
LabVIEW中调用共享库
LabVIEW中调用共享库
11 0
|
7天前
LabVIEW实现编程改变EnumeratedType说明与例程
LabVIEW实现编程改变EnumeratedType说明与例程
15 2
|
8天前
|
Linux iOS开发 MacOS
LabVIEW使用源代码控制
LabVIEW使用源代码控制
11 1
|
8天前
|
存储
LabVIEW谨慎使用局部变量和全局变量
LabVIEW谨慎使用局部变量和全局变量
15 1
|
7天前
|
人机交互
LabVIEW编程LabVIEW开发控制阿尔泰DAM-3948D例程与相关资料
LabVIEW编程LabVIEW开发控制阿尔泰DAM-3948D例程与相关资料
13 0
|
8天前
LabVIEW编程LabVIEW控制ESM-100 HE 场表例程与相关资料
LabVIEW编程LabVIEW控制ESM-100 HE 场表例程与相关资料
15 2
|
7天前
|
C语言 C++ 开发者
LabVIEW调用C/C++ DLLs
LabVIEW调用C/C++ DLLs
12 0
|
7天前
LabVIEW编程LabVIEW控制WNSC600运动控制器例程与相关资料
LabVIEW编程LabVIEW控制WNSC600运动控制器例程与相关资料
16 1
|
8天前
|
存储 编解码
LabVIEW编程LabVIEW控制picoharp 300例程与相关资料
LabVIEW编程LabVIEW控制picoharp 300例程与相关资料
13 0
|
7天前
|
API
LabVIEW编程LabVIEW控制PXI-5122例程与相关资料
LabVIEW编程LabVIEW控制PXI-5122例程与相关资料
17 1

热门文章

最新文章