Blog

Edit Time Code Generation in LabVIEW - The POWER OF VI SCRIPTING!

The Power of VI Scripting

The LabVIEW Development Environment contains a set of functions, properties, and methods that can be used to inspect, edit, and create LabVIEW code. Yes, you read that right! This collection of functionality is VI Scripting. This functionality is often overlooked; however, if used correctly, it can be amazing. This blog post shows some of the ways in which we used VI Scripting at Erdos Miller and will hopefully be a guide for you to get started.

To begin our journey, we must first understand what an XNode is.

What are XNodes?

XNodes are block diagram nodes that have the ability to encompass VIs that run in the development environment. They are the block diagram equivalent of XControls. The ability VIs can change an XNode's appearance, change its run-time behavior by editing the underlying code, or do anything you can do in the development environment via VI Scripting. XNodes are not an official LabVIEW feature, and there is not a way to create them in the development environment, but the community has made tools to allow XNode creation and editing.

What are some things XNodes can be used for?

Parametric Polymorphism

Many of LabVIEW's primitive functions can accept many different data types at their inputs. For example, the Equal? function can accept values of any type at its inputs, as long as they are the same type (or as long as one type can be coerced to the other). This ability to handle values of different types is called parametric polymorphism. SubVIs cannot have this functionality because each of their inputs must be of a single type.

Polymorphic VIs resolve to one of a fixed number SubVIs based on input types or a menu selection. This is ad hoc polymorphism, and to make a polymorphic VI that can work with any type requires a lot of code duplication.

Parametric polymorphism can be achieved using XNodes because an XNode can change the type of its inputs and outputs based on an input type. The OpenG Array XNodes are an example.

VIMs (VI Macros), which use an XNode to do their work, also have this functionality without needing to write an XNode. To create a VIM you have to add "ExternalNodesEnabled=True" to your LabVIEW INI file. You can then change a SubVI's extension to .vim, and its inputs and outputs will have adaptive types. VIMs are also not an officially supported feature and should be used with caution.

Generate Different Code Based on Edit Time Information

Because runtime behavior of the XNode can be changed by the XNode at edit time, we can use edit-time information to decide what the node should do. An example of this is this LV-Control-Refs XNode.

It populates a cluster with control references to every control in the owning VI. When in this VI:

1.png

 

it generates this code:

2.png

but when dropped in this VI:

3.png

it generates this code:

4.png

 

Interactive Nodes

We can also make nodes with interactive behaviors. This package includes XNodes that allow a VI to be dropped onto them that it will asynchronously call.

5.png

LV-Read-Write.xnode from this package is growable and has elements that are selectable like a property node.

6.png

 

Create Types

With VI Scripting, XNodes can create and edit .ctl files. This allows them to create and edit types.

LV-Create-Signals.xnode, LV-Control-Refs.xnode, and LV-Define-Tagged-Union.xnode all have an output type that can change. This output type can be bound to an existing typedef that the XNode will update when it needs to.

7.png

 

Fake Extensions to the LabVIEW Type System

Some programming languages support sum types, types that are a disjoint union of other types. One common way to get similar functionality in LabVIEW is to use a cluster of an enumeration and a variant, where based on the enumeration value, the developer knows the concrete type of the value inside the variant. The downside of this method is that it requires the developer to use the Variant to Data function, and mistakes result in runtime errors.

This set of XNodes provides what look like sum types, but are really just a correctly-typed interface to an enum & variant cluster.

 

8.png

Conclusion

At Erdos Miller we use VI Scripting and Edit-Time Code Generation techniques to increase our productivity, make code easy to write and easier to maintain. We are always looking to meet new clients and help them solve their technical challenges quickly and efficiently. We are also always looking to expand our team and bring new ideas, minds and talents into our team. Get in touch with us today.

 

Recent Posts:

MWD Basics: Accelerometers Part 2
Publish Date 20 Mar 2017 Nathan SzantoDavid Taylor

In Part 1 of this post, we used the idea of shining a light through a cut-out in a [..]

Employee Profile: Meet Kelsey
Publish Date 20 Mar 2017 Shannon HayesDavid Taylor

Today we sat down and talked with one of our Erdos Miller employees to learn about [..]

MWD Basics: Accelerometers Part 1
Publish Date 20 Mar 2017 Nathan SzantoDavid Taylor

In a previous blog post on the building blocks of an MWD system, I mentioned that [..]

Basic Static Structural FEA TIPS
Publish Date 20 Mar 2017 Garret HallmarkDavid Taylor

Finite Element Analysis (FEA) can seem like magic when you first use it. The program [..]

Employee Profile: Meet Rohama

Today we sat down and talked with one of our Erdos Miller employees to learn about [..]

Popular Posts

Posts by Topic

See All Topic