If you have an application that accesses a large number of shared variables, you can read and write those shared variables programmatically with the Shared Variable functions. This programming technique often is cleaner and more scalable than using Shared Variable nodes because you can access multiple shared variables with a single Shared Variable function, whereas each Shared Variable node accesses a single shared variable only.
Note��You can access the following types of shared variables programmatically:
|
The following figure shows an example of reading shared variables programmatically, placing those values in a preallocated array, and processing those values.
The following events occur in the previous figure.
This application achieves the same functionality as one that uses Shared Variable nodes. However, because this application uses programmatic shared variable access, you can scale to hundreds or thousands of variables without updating the block diagram. For example, assume that My Library contains ten variables, Variable1 through Variable10. You could use the Batch Variable Creation dialog box to add Variable11 through Variable500 to My Library. After you redeploy My Library, the next time you run the VI, it reads Variable1 through Variable500 with no need to add Variable11 through Variable500 to the block diagram.
The following sections provide information about the programming techniques in the above example.
Use the Search Variable Container function to search for shared variables and variable containers that meet specific criteria. For example, the VI shown in the following block diagram uses this function to find all the shared variables under a given container that meet the following criteria:
Note��You can use the Browse Variable Object dialog box to browse for shared variables and variable containers at edit-time either on the network or in the current project. Click the drop-down arrow on a Variable control or constant, such as the container refnum in constant in the figure above, and select Browse to display the Browse Variable Object dialog box. |
Caution��When reading and writing shared variables that you find programmatically, you might read or write data of the wrong data type. Use the data type input of the Search Variable Container function to avoid reading or writing a shared variable of the wrong data type. |
You also can use the following Variable Container properties and method to find shared variables programmatically.
The VI in the following figure uses the All Variables property to find all the PSP variables hosted on the local network.
The following events occur in the previous figure.
Tip��You can use the Variable Property Node, which is preconfigured with the Variable Object class, as a starting point for building the block diagram in the previous figure. |
Use the Open Variable Connection function to open a connection to a variable programmatically, as shown in the following block diagram.
After the application finishes accessing the shared variables, use the Close Variable Connection function in a For Loop to close all the variable references. This function frees the memory resources allocated to each variable connection.
Use the Read Variable function to read live variable values programmatically. Unlike the Shared Variable node, which you must bind to a specific shared variable at edit time, the Read Variable function includes a shared variable refnum in input that you use to specify the shared variable to read at run time. For example, you can use the Read Variable function inside a For Loop to read multiple shared variables specified by an array of refnums, as shown in the following block diagram.
Use the Write Variable function to write live variable values programmatically. For example, you can use the Write Variable function inside a For Loop to write to multiple shared variables specified by an array of refnums.
Tip��You can optimize performance and streamline the block diagram by reading and writing I/O variable containers as arrays. |
Note��If you write less than 8 kilobytes of data to a shared variable, LabVIEW can take up to 10 milliseconds to send that data over the network. To eliminate this delay, use the Flush Shared Variable Data VI to send the shared variable data immediately. |