Can LabVIEW C? - Example 1: Basic Structures and Methods

Publish Date: Jul 07, 2008 | 39 Ratings | 3.46 out of 5 | Print | Submit your review

Table of Contents

  1. Conditional Execution
  2. Storing Data in Arrays
  3. Generating Random Numbers
  4. Sorting Arrays

This set of examples (GenerateAndSort.c and GenerateAndSort.vi), linked below, is designed to illustrate the concepts of loops, arrays, and data manipulation in LabVIEW and in C. The task for these examples is to generate and sort an array of integers. Both programs let the user decide whether to generate an array of random numbers or to input one manually. They then allow the user to trigger the sorting of the array. The LabVIEW front panel uses dialog controls, which are controls that adhere to the default controls of the current operating system. Figures 1 and 2 below are the user interfaces for the C and LabVIEW examples.



Figure 1. C Standard Output Window



Figure 2. LabVIEW Front Panel


1. Conditional Execution

In the C program, the user is prompted with a menu after each selection. This is accomplished by using a Switch statement inside of a While Loop. The Switch statement executes a different section of code depending on the user input. The Do-While Loop is terminated and the program ends when the user enters case "4". Figure 3 shows the C code for the While Loop and the Switch Statement.


Figure 3. C Code for the Do Loop and the Switch-Case structure


The LabVIEW program, or VI, stores the states of all of the button controls in a cluster.  A cluster is a set of ordered, unindexed data elements of any data type, in some respects, analogous to the struct in C. The While Loop polls the value of that cluster every 100 milliseconds to determine which button, if any, has been pressed and uses that information to control the Case structure. The LabVIEW While Loop is equivalent to the Do-While Loop in C. Both execute the code within the loop at least once. Like the Switch-Case structure in C, the Case structure in LabVIEW executes a different set of instructions depending on which operation the user has selected. The While Loop continues to execute until a value of True is sent to the stop condition. This occurs when the user clicks the Quit button. Figure 4 shows the block diagram (or code) for the GenSortRand VI, illustrating the Case structure within a While Loop.

Figure 4. Block Diagram for GenSortRand.vi


The LabVIEW examples in this paper use the Case structure instead of the Event structure to maintain consistency with standard ANSI C. The LabVIEW Event structure is a more efficient way to respond to user interface interactions in LabVIEW. The Event structure has similar functionality to the Case structure but does not waste processor time by polling like the Case structure does. The Event structure in LabVIEW is a powerful feature that makes event-based programming easy.

 

Back to Top

2. Storing Data in Arrays

The C and LabVIEW programs store data in similar ways. The C program stores the integers in an array called iArray. The array is local to the main function and the type and size are defined. Integers can be stored in each element of the array, 0-9. The init() function is called to initialize each of those indices to 0. The C program stores the size of the array using the ARRAYSIZE global variable. The LabVIEW developer also declares the array by placing an array control on the front panel and placing a numeric control inside the array control. Arrays can also be created dynamically on a LabVIEW block diagram. Moreover, it is unnecessary to declare the size of the array since LabVIEW handles memory management automatically. Note that when loop determinism is important, especially in embedded or realtime applications, arrays can be pre-allocated to the required size to avoid dynamic memory allocation. The array values are all set to 0 at the beginning of the program using the built-in Initialize Array function. In this example, the LabVIEW array control is called Array and is also has 10 element, indexed 0-9. As in the C code, the LabVIEW program uses a constant to determine the size of the array and then passes that value out to the rest of the program. This is a situation where a local variable could have been used in LabVIEW for the array size, but it was not used because it is better LabVIEW programming practice to pass data using a wire from one VI to another. Successful and efficient LabVIEW programs adhere to this concept of data flow whenever possible.

Back to Top

3. Generating Random Numbers

The way the data is generated is very similar between LabVIEW and C. Random number generation is achieved through the rand() function in C and the Random Number (0-1) VI in LabVIEW. Both outputs have to be scaled in order to produce integers within the desired range (0-100). Figure 5 and Figure 6 show the C and LabVIEW code examples that generate, scale, and round an array of random numbers. Both generate a random number between 0 and 100 and coerce it to an integer.

 


Figure 5. Random Number Generation in C



Figure 6. Random Number Generation in LabVIEW


If the manual input option is chosen in the C example, the user is prompted for each number one at a time. Some error checking is done to see if the number the user entered is out of range. If so, the value is coerced. To enter the data manually in LabVIEW, the user simply enters numbers into the numeric array control on the VI’s front panel. Although it is possible to coerce the values in code from the VI’s block diagram, it is unnecessary. A LabVIEW developer may choose how the values are coerced using the numeric control’s property page, which can be reached by right-clicking the numeric control, selecting Properties from the shortcut menu, and selecting the Data Entry tab in the dialog box. Figure 7 and Figure 8 show the C code and the LabVIEW property page that control how values are coerced once they are entered.

 


Figure 7. Input Coercion in C



Figure 8. Input Coercion in LabVIEW

 

Back to Top

4. Sorting Arrays

The LabVIEW and C programs both perform a bubble sort, a common O(n^2) sorting algorithm, in order to sort the given array. When the user selects the sort option in the C example, the bubbleSort() function is called. This function is passed to the array by reference. In the bubbleSort() function, nested For Loops and an If Statement are used to sort the array. Much like calling a function in C, the LabVIEW example calls the Bubble Sort VI as a subVI to perform the sorting. The array is passed to the subVI directly using a wire. When passing data to a subVI, LabVIEW uses pass-by-value semantics with pass-by-reference implementation. This means that logically LabVIEW is directly passing the array to the subVI, but in practice LabVIEW is passing a reference to that array, which improves performance. Now LabVIEW performs any modifications to the array from the subVI in place, eliminating the need for extra copies of the array to be made in memory. Within the Bubble Sort VI, the LabVIEW code can emulate the C code by using nested For Loops and a Boolean controlled Case structure. Figure 9 and Figure 10 show the equivalent C and LabVIEW implementations of Bubble Sort.


Figure 9. Bubble Sort Implementation in C



Figure 10. Bubble Sort Implementation in LabVIEW


Although LabVIEW can usually emulate C code in this way, in many cases LabVIEW can also offer a better solution. Figure 11 shows a slightly simpler and more efficient Bubble Sort implementation that takes advantage of the built-in Max & Min. function.


Figure 11. More Efficient Bubble Sort in LabVIEW


LabVIEW offers several time-saving VIs for a variety of tasks. In this example, instead of writing a new sorting algorithm in LabVIEW, the developer could have used the built-in Sort 1D Array VI included on the LabVIEW Functions palette as shown in Figure 12. This VI efficiently sorts and returns a 1D array of any type.

Figure 12. Even More Efficient Sort in LabVIEW

 

Additional Resources

Download the Can LabVIEW C - Example 1 Source Code
Learn how to implement standard software engineering practices in LabVIEW
Access the Can LabVIEW C overview whitepaper

Back to Top

Bookmark & Share


Ratings

Rate this document

Answered Your Question?
Yes No

Submit