Professional Documents
Culture Documents
Most loops require a wait function, especially if that loop monitors user input on the front panel. Without wait function, the loop might run continuously and use all of the computer system resources. The wait function forces the loop to run asynchronously even if you specify 0 msec.
Advantages: make code smaller and easy to debug, each case determines the next state. Unlike sequence structures that must execute every frame in sequence. Disadvantage of the state machine is if two states in the structure are called at the same time, this model only handles one state, and skip states lead to errors and difficult to debug.
Note: you can not use wire to pass data between loop because doing so prevents the loops from running in parallel. Instead, you must use a messaging technique for passing information among processe
Use the producer/consumer design pattern when you must acquire multiple sets of data that must be processed in order. Suppose you want to create a VI that accepts data while processing the data sets in the order they were received.
Queuing (producing) the data occurs much faster than the data can be processed (consumed). You can use the producer/consumer design pattern to create a VI that analyzes network communication. This type of VI requires two process to operate at the same time and at different speeds. The first process polls the network line and retrieves packets. The second process analyzes the packets retrieved by the first process. This design pattern allows the consumer loop to process the data at its own pace, while the producer loop continues to queue additonal data.
Always use a timing function such as Wait (ms) function or the Wait Until Next ms in any design pattern. If you do not use, LabVIEW uses all the processor time, and background processes may not run. Wait (ms) is normally used in Master and Slave / producer and consumer loop design pattern Wait until multiple ms execute faster than wait (ms)
Execution timing uses timing functions to give the processor time to complete other tasks. For example wait (ms) function. Software timing involves timing a real-world operation to perform within a set time period. For example the Elapsed Time function.
NOTES: Always use the Get/Date Time in Seconds function instead of the tick count
function for this type of comparison because the value of the tick count function can rollover to 0 during execution.
Using Variables
For single processor / one computer: Use local variables to access front panel objects from more than one location within a single VI. Use Global Variables to access and pass data among several VIs. Reentrant, which multiple VIs can access and cause race condition. For Multiple computers on the networks: Use shared variable to share data across a network between multiple computers. To create single process shared variable 1. Launch project file 2. right click my computer 3. New >> Variable 4. select single-process variable type, double data type. Save the library and you can drag and drop into your block diagram.
NOTES: Over using local and global variables can slow the performance.
Initializing the variable before the loops begin to insure that the second loop does not immediately stop.
Functional global variable is not reentrant, meaning when a VI call the FG, it locks and wait until the VI executes. All other VIs call FG need to wait until its done. You can also use semaphore to eliminate the race condition. Semaphore can not pass data like queue or notifier.
Communicating Among Multiple Loops Single process shared variable is similar to a local variable or a global variable, but allows you to share data across a network. Use single process shared variable to share the variable information among VIs on multiple computers in the future. Use global variable to share data among VIs on the same computer, especially if you do not use a project file. To create global variable, on the function pallete Data Communications>> Global Variable
NOTES: Notifiers and queues are methods for passing data between parallel processes that have advantages over using variables because of the ability to synchronize the transfer of data. Using a notifier to pass data from the master to the slave removes any issues with race conditions. Queues are similar to notifiers, except that a queue can store multiple pieces of data. By default, queues work in FIFO manner. Use Queue when you want to process all data placed in the queue.
10
Initializing Variables
11
In this example, when enum data type is set, data passes into the VI and the shift register stores the data. If the Enum data type is set to Get, data is retrieved from the shift register.
Function Global Variable are not reentrant. This means that when a VI calls a FG, that FG is locked or reserved for use by the calling VI. Other VIs need to wait until it is done. Only one call can occur at a time.
12
RACE CONDITION
One way to eliminate race conditions is to identify and protect the critical sections in your code. There are many techniques for protecting critical sections. Two of the most effective are Functional Global Varialbes and Semaphores.
Semaphores
Semaphores are synchronization mechanisms specifically designed to protect resources and critical sections of the code. By default, the semaphore only allows one task to acquire it at a time. Therefore, after one of the tasks enters a critical sections, the other tasks can not enter their critical sections.
13
14
Both loops are synchronized to the master loop. The slave loop only executes when the master loop sends a notification. However using notifiers can have drawbacks. A notifier does not buffer the data. If the master loop sends another piece of data before the slave loop reads the first piece of data, that data is overwritten and lost.
15
The producer loop uses the Enqueue Element function to add data
Dequeue is to remove the data from the queue. Release Queue function marks the end of queue by destroying it. Queues have the ability to transfer any data type. A string is not a most efficient type for passing data in design patterns. A cluster is a more efficient data type for passing data.
Which of the following cannot transfer data? a. Semaphores b. Functional global variables c. Notifiers d. Queues Which of the following are user interface events? a. Clicking a button on the mouse b. Pressing a key on the keyboard c. While Loop d. Value change of a control
16
Note: A common problem when using the user event interface event handler is that it Computes the While loop termination before the event structure executes. This can Cause the While loop to iterate one more time than you expected.
17
The default is -1, which specifies to wait indefinitely for an event to occur. you must provide a Timeout Case if the timeout terminal is wired. Otherwise, VI cant run
any negative number on the timeout terminal will wait indefinitely for an event to occur.
18
19
In LabVIEW, you can use VI server to access the properties and methods of front panel objects. VI server is object-oriented programming. An object is a member of a class. A class defines what an object is able to do, what action it can performs (method), and what properties it has, such as color, size, and so son.
NOTE: You can not use property node to read value of the control bolean with latching mechanism.
20
21
To create a control reference for a front panel object, right-click the object or its block diagram terminal and select CreateReference from the shortcut menu. You can wire this control reference to a generic Property Node. You can pass the control reference to a subVI using a control refnum terminal.
22
Invoke Nodes
Unlike the Property Node, a single Invoke Node executes only a single method on an application or VI. You also create an implicitly linked Invoke Node by right-clicking the front panel object. Create >> Invoke Node.
An example common to all controls is the Reinitialize to Default Method. Use this method to reinitialize a control to its default value at some point in your VI. When this VI runs, it will set the start button and all boolean indicators to its default values
Select VI server reference >> right click >> create>> method for VI class>> default values>>reinit defaults all values.
23
NOTE: Using the This VI Server Reference you can access all the methods and properties of the current VI without having to explicitly open and close a reference.
24
File I/O
LabVIEW can use or create the following file formats: binary, ASCII, LVM, and TDM: Binary Files are the underlying file format of all other file formats ASCII file is a specific type of binary file that is a standard used by most programs. They are also called text files. LVM is a tab delimited text file you can open with spreadsheet application
TDM binary file created for national instruments. It is usually consists of two separate files: an XML and binary file for waveform.
When to Use Test (ASCII) Files Use test format files for your data to make it available to other users or applications if disk space and file I/O speed are not crucial. Text Files normally takes more memory than binary and datalog files since its ASCII takes one byte for each character. For example: -123.4567 takes 9 bytes
25
26