LabVIEW and Visual Basic - Example 1: Generating and Sorting Numbers

As you can see in Figure 1, both interfaces include a display of ten numbers as well as three control buttons. An interesting point is that LabVIEW is capable of natively displaying complex data types. In fact, the list of numbers on the LabVIEW front panel pictured above is directly tied to an array in which the data is stored. This means that any computations that affect the values in the array are automatically reflected on the front panel. While an array of text controls can be created in VB, code still needs to be written in order to keep them in-sync with a separate array of integers stored in memory. Often, arrays, such as those used to store the results of data acquisition, can be quite large. The array control in LabVIEW also allows a programmer or user to select the index of the elements being viewed, which allows viewing of the entire data set without filling too much space. These native displays can really be thought of as an automated mirror of the underlying data.
The program scheme of the Visual Basic solution is as follows:
□ Clicking the Generate button populates the ten text controls with random integers between 0 and 100.
□ Clicking the Sort button assigns the values of the text controls to elements of an array and then passes that array to an implementation of Bubble Sort. This scheme allows users to type in their own values instead of clicking the Generate button.
□ User entered values are automatically coerced to integers between 0 and 100.
□ Clicking the Quit button ends the program.
The LabVIEW scheme is similar:
□ Clicking the Generate button assigns random integers between 0 and 100 to the elements of an array. Since the numbers being displayed on the front panel are directly tied to the array data, they are updated automatically.
□ Clicking the Sort button passes the array to a sub VI (subroutine) that performs a Bubble Sort.
□ This VI (program) also coerces values between 0 and 100. Unlike Visual Basic, LabVIEW natively includes numeric controls, and this behavior can be automated without any additional code.
□ Clicking the Quit button ends the VI.
The image below illustrates the random generation of values to be stored in the front panel array. A very useful feature of LabVIEW’s loops is auto-indexing. When enabled, values within a loop are collected and automatically stored in an array, so you do not need to write code to declare such an array and manually store values within its elements. As shown in Figure 2 below, the code executes when the user clicks the Generate button. Just as in Visual Basic, a LabVIEW VI can be event-driven.

Figure 2. Generating an Array of Random Integers Using LabVIEW
Now that numbers have been generated, it’s time to sort them. A prime example of an O(n2) algorithm, Bubble Sort is a staple of introductory computer science classes. The algorithm’s concept is simple. In each loop iteration, the largest element of the current bubble is shifted to the bubble’s highest index. At first, the bubble encompasses the entire array, in effect moving the largest value to the correct location. In each subsequent iteration, the bubble size is reduced by one until all elements have been placed in their correct ascending order.
| Public Sub BubbleSort(ByRef sortArray() As Integer) Dim i As Integer Dim j As Integer Dim Temp As Integer For j = LBound(sortArray) To (UBound(sortArray) - i - 1) If sortArray(j + 1) < sortArray(j) Then Temp = sortArray(j) 'swap if the two items sortArray(j) = sortArray(j + 1) 'are out of order sortArray(j + 1) = Temp End If Next j Next i End Sub |
Figure 4 shows a LabVIEW implementation of Bubble Sort. As you can see, just as in the Visual Basic code above, there are two embedded For Loops that swap array elements in the current bubble that are out of order. In LabVIEW, For Loops automatically iterate from zero up to the specified “N” value. However, by performing calculations with “i”, the current iteration, any range of integers can be traversed.

[+] Enlarge Image
Though it is certainly easy to implement a sorting algorithm, LabVIEW already contains VIs to perform a multitude of common (and often uncommon) tasks, such as sorting, which saves programmers a lot of time in implementing redundant code. LabVIEW’s built-in library of VIs is also searchable, freeing programmers from memorizing details such as exact function names and locations within the programming environment.
While Figure 4 shows how a programmer might implement a literal translation of Bubble Sort in LabVIEW, there are other, simpler ways to achieve similar results. For example, Figure 5 is based on the same algorithm but results in a simplified diagram by making use of a built-in comparison VI. The Max & Min VI receives two numbers as inputs and identifies which is greater and which is lesser. Rather than writing extra code to make comparisons and only swap in certain cases, we can instead use the outputs of the Max & Min VI to always reassign elements in the correct order.

[+] Enlarge Image
As mentioned previously, LabVIEW also has many built-in VIs designed to handle common (and uncommon) programming tasks. One of these VIs is Sort 1D Array. Figure 6 shows how Sort 1D Array might be included within a VI instead of Bubble Sort.

Figure 6. The easiest method to sort a 1D array of data in LabVIEW
Just as in Visual Basic, subroutines can be abstracted into their own space for easy reuse and also to maintain a modular application design. In LabVIEW, subroutines are called subVIs. Bubble Sort is implemented as a sub VI in Example 1. A sub VI’s input and output parameters, which are like the list of formal parameters of a Visual Basic subroutine, are configurable from its front panel. For example, Bubble Sort has an input parameter called Source Array and an output called Sorted Array. Once the parameters have been defined through the connector pane, the Bubble Sort sub VI can be included within other VIs. A sub VI appears as an icon (shown immediately below) and allows input and output connections precisely where they were configured through its connector pane. Though it’s beyond the scope of this paper, because of the way that LabVIEW handles variables, in this case, both of the sort arrays can be thought of as references to a single location in memory, thus improving execution speed and eliminating the need to make copies of the array.
In this first example, you have seen the similarities in program design between the two programming languages, as well as some of LabVIEW’s features that make it easy to work with, such as automatically displaying complex data. Example 2 expands on these ideas by introducing ASCII file I/O, and clusters which are data structures that are very similar to types in Visual Basic. Example 2 also shows how LabVIEW’s interface can appear consistent with other applications in the operating system.
LabVIEW and Visual Basic
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
