Configuration (INI) Files


The Configuration File VIs (Programming>>File I/O>>Configuration File VIs palette) shown in Figure 14.25 allow you to read and write data as key-value pairs to a special file format generally referred to as a configuration or initialization file. Configuration files usually have the file extension .ini (short for "initialization"), and are often simply referred to as "INI" or "config" files.

Figure 14.25. Configuration File VIs palette


Configuration, or INI, files are commonly used in software applications to store important variables that are specific to a software installation.

A typical INI file might look something like the following:

[section1] HardwareType = USB MonitorResolution = 1024x768 key3 = value3 key4 = value4 [section2] key1 = value1 key2 = value2 


As you can see, the file contents are human readable and easy to decipher.

The elements of an INI file are as follows:

  • Sections Section start with '[' and end with ']' as in [section1] and [section2] in the preceding.

  • Key-value pairs The "HardwareType = USB" is an example of a key-value pair, which begin with a key ('HardwareType'), followed by an equals sign ('='), and a value ('USB').

There is no official file format for configuration files. For example, sometimes people use a semicolon (;) as the first character of a line that contains a comment; however, the LabVIEW Configuration File VIs do not support this feature.


Any configuration file created using the LabVIEW configuration file VIs can be read from or written to on any platform (Mac OS X, Windows, or Linux). So, this is an excellent choice for storing your application's configuration parameters. Additionally, if your application stores configuration parameters in a file, rather than constants on the block diagram, you can change your application's configuration without editing the code. That's a HUGE time saver!


Opening and Closing Configuration Files

You can open a reference to a configuration file, using Open Config Data.vi (Programming>>File I/O>>Configuration File VIs palette), shown in Figure 14.26. If you are creating a new file, make sure to wire a Boolean constant TRUE to the create file if necessary? input. When you are done accessing the configuration file, use Close Config Data.vi (Programming>>File I/O>>Configuration File VIs palette), shown in Figure 14.27, to close the configuration file reference.

Figure 14.26. Open Config Data.vi


Figure 14.27. Close Config Data.vi


Any changes you make to a configuration file, using the Configuration File I/O VIs, are only made in memory (not saved to disk) until you save the configuration file by calling Close Config Data.vi with the write configuration file? input set to TRUE.


Writing and Reading Key Values

To write and read values to and from keys of a configuration file, use Write Key.vi and Read Key.vi (Programming>>File I/O>>Configuration File VIs palette), shown in Figures 14.28 and 14.29.

Figure 14.28. Write Key.vi


Figure 14.29. Read Key.vi


Both of these VIs have section and key inputs, which are used to specify the key you wish to write or read. These two VIs are polymorphic VIs (which we learned about at the beginning of this chapter) and support values of Boolean, DBL, I32, Path, String, and U32 types. To change the type, simply connect a wire of the desired type to the value input of Write Key.vi, or the default value input of Read Key.vi and the VIs will automatically adapt to the wire type. You can also explicitly choose the type using the Polymorphic VI Selector, as shown in Figure 14.30. You can make the Polymorphic VI Selector visible, by right-clicking on the VI and choosing Visible Items>> Polymorphic VI Selector.

Figure 14.30. Using the Polymorphic VI Selector to explicitly choose a type of a polymorphic subVI


If you want to write a cluster or array to a configuration file, it can take a lot of work to unbundle and index these compound and complex data structures. However, OpenG (http://openg.org) has developed a library of Open Source VIs that can write and read anythingyes, anythingto and from a configuration file! This library is called the Variant Configuration File I/O VIs. See Appendix C, "Open Source Tools for LabVIEW: OpenG," for more information about OpenG and how to obtain this library of VIs.


Storing large data sets in configuration files (for example, by flattening a large cluster or array to string and writing the data using Write Key String.vi) is not recommendedthis is not what they are intended for. For large data sets, you will notice (sometimes significant) limitations in performance. If you need to store large data sets in a file, consider using a binary or datalog file.


Activity 14-3: Storing Data in a Configuration File

You will write a very powerful VI that can write and read data to and from a configuration file. This activity might take a little bit of time to complete, but this VI is so useful that it will certainly become a cornerstone of your LabVIEW toolbox.

1.

Open a new VI and create the front panel shown in Figure 14.31. Note that inside the Configuration Parameters clusters, Sampling Frequency is a DBL and Number of Samples is an I32.



Figure 14.31. Front panel of the VI you will create during this activity


2.

Create the block diagram shown in Figure 14.32. Note that the "False" frame of the Case Structure is shown below the "True" frame, only for illustrative purposes.

Figure 14.32. Block diagram of the VI you will create during this activity


Open Config Data VI

The Open Config Data VI (Programming>>File I/O>>Configuration File I/O palette) opens a reference to the configuration file. Note that the Write File? control terminal is wired to the create file if necessary? input terminal of Open Config Data. This will ensure that if you are writing the file, it will be created if it does not already exist. However, if you are reading the file, you do not want to create it if it does not exist; rather, you want an error to be generated if the file does not exist.

Write Key VI

The Write Key VI (Programming>>File I/O>>Configuration File I/O palette) is used to write the Sampling Frequency and Number of Samples keys to the configuration file. The Polymorphic VI Selector is made visible, by right-clicking on the VI and choosing Visible Items>>Polymorphic VI Selector. The instance of Write Key that writes the Sampling Frequency has been set to Write Key (Double), and the instance that writes the Number of Samples has been set to Write Key (I32).

Unbundle By Name Function

The Unbundle By Name function (Programming>>Cluster & Variant palette) is used to unbundle the elements from the Configuration Parameters cluster, which are to be written to the configuration file.

Read Key VI

The Read Key VI (Programming>>File I/O>>Configuration File I/O palette) is used to read the Sampling Frequency and Number of Samples keys from the configuration file. The Polymorphic VI Selector is made visible, by right-clicking on the VI and choosing Visible Items>>Polymorphic VI Selector. The instance of Read Key that reads the Sampling Frequency has been set to Read Key (Double), and the instance that reads the Number of Samples has been set to Read Key (I32).

Bundle By Name Function

The Bundle By Name function (Programming>>Cluster & Variant palette) is used to bundle the key values, read from the configuration file, into the Configuration Parameters cluster.

Close Config Data VI

The Close Config Data VI (Programming>>File I/O>>Configuration File I/O palette) closes the reference to the configuration file, and optionally saves the configuration data in memory to disk. Note that the Write File? control terminal is wired to the create configuration file? input terminal of Close Config Data. This will cause the file to be written, only if Write File? is set to TRUE. If you are only reading the file, you do not want to resave it.

3.

Enter a path into the configuration file path control, set the Write File? Boolean to TRUE, and run your VI.

4.

Open the file you just created in your favorite text editor application. The file contents should look something like the following:

[Measurement Configuration] Sampling Frequency=50000.000000 Number of Samples=1000 


Edit the values of either of the two keys (for example, change "Number of Samples" from "1000" to "2000") and then save the file.

5.

Now, set the Write File? Boolean back to FALSE, and run your VI. The new values in the file will appear in the Configuration Parameters (read) cluster.

6.

Congratulations! You just built a power tool that you can reuse in all of your applications. You can customize it, by adding additional keys into the Configuration Parameters clusters.

7.

Save the VI in your MYWORK directory as Read-Write Configuration File.vi.

Additional Configuration File Operations

If you are doing more than just writing and reading keys to and from a configuration file, the following VIs will be of great use to you. They allow you to remove sections and keys, as well as list keys and sections present in the configuration file.

Remove Key.vi (Programming>>File I/O>> Configuration File VIs palette) deletes the specified key, from the specified section of the configuration file (see Figure 14.33).

Figure 14.33. Remove Key.vi


Remove Section.vi (Programming>>File I/O>>Configuration File VIs palette) deletes the specified section, along with all of its keys, from the configuration file (see Figure 14.34).

Figure 14.34. Remove Section.vi


Get Key Names.vi (Programming>>File I/O>>Configuration File VIs palette) returns the names of all the keys in a specified section of the configuration file (see Figure 14.35).

Figure 14.35. Get Key Names.vi


Get Section Names.vi (Programming>>File I/O>>Configuration File VIs palette) returns the names of all the sections in the configuration file (see Figure 14.36).

Figure 14.36. Get Section Names.vi





LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net