0
1
4
Because a refnum is a temporary pointer to an open object, it is valid only for the period during which the object is open. If you close the object, LabVIEW disassociates the refnum with the object, and the refnum becomes obsolete. If you open the object again, LabVIEW creates a new refnum that is different from the first refnum. LabVIEW allocates memory for an object that is associated with a refnum. Close the refnum to release the object from memory. 5
7
Use text format files (ASCII files) for your data: To make the data available to other users or applications If disk space and file I/O speed are not crucial If performing random access reads or writes is unnecessary If numeric precision is not important Use binary format files for your data: When numeric precision is important When you need to randomly access stored data When efficiency is important A specific type of binary file, known as a Datalog file, is the easiest method for logging cluster data to file Stores arrays of clusters in a binary representation The storage format for Datalog files is complex, and therefore the data is difficult to access in any environment except LabVIEW Use TDMS files for the following purposes: To store test or measurement data To create a structure for grouping your data To store information about your data To read and write data at high speeds 8 TDMS file format:
Point out the high-level functions and low-level functions on the palette. High-level and low-level I/O applies to many areas, such as DAQ and TCP/IP. 9
High-level file I/O VIs perform all the steps: opening/creation of a file, reading/writing, closing the file. They also offer additional features, e.g. they can perform formatting of the data before they get written to the file. 10
If writing to or reading from a file occurs inside a loop and has o be iterated many times, it is worth using low-level functions for file access. There are separate functions for each of the steps: opening/creation of a file, reading from a file, writing to a file, closing a file. This gives the programmer more control and ability to decide when each phase of file access should happen. For example, for streaming data to disk (as shown in the slide) it is best to open and close the file only once - outside the loop. Only write (or read) functions are executed inside the loop, which results in better performance. 11
What would happen if you used the Write to Measurement File Express VI inside a While loop? The VI would run slower and be more inefficient. Because the Write to Measurement File Express VI is a high-level File I/O VI, the While Loop would open, write, and close the file in every iteration of the While Loop. Instead, you should use low-level File I/O functions when streaming data to a file in a loop, so that you only open the file once at the beginning of the VI and close the file once at the end of the VI. The idea of high- and low-level VIs holds also to other resources that have to be opened, read from or written to, and closed. 12
14
LabVIEW represents each data type in specific ways when writing to a binary file. Here is some additional information on how the data is stored. See the next slide for the importance of knowing how the data is stored. Multi-byte integers are broken into separate bytes and are stored in files in either littleendian or big-endian byte order. Using the Write to Binary File function, you can choose whether you store your data in little-endian or big-endian format. For more information on endian-ness see this ni.com article: What Does the Term "Endian" Mean? (http://digital.ni.com/public.nsf/allkb/8224f2391418fdf6862568f00051d091) Because strings are stored as a series of unsigned 8-bit integers, each of which has a value in the ASCII Character Code Equivalents Table, there is no difference between writing strings with the Binary File Functions and writing them with the Text File Functions Clusters are best represented in binary files by using Datalog Files. Arrays are represented as a sequential list of the elements. Element representation depends upon the element type. A header contains a 32-bit integer representing the size of each dimension. Example : A 2D array with a header contains: number of rows (integer), number of columns (integer), then array data. 15
There are two main methodologies to reading/writing binary files: random and sequential. Random is when to pick a point in the file and read X amount, then you might go to another section and read Y amount. Sequential, on the other hand, starts at the beginning and continues until you say stop or the end of the file is reached. In either case, you need to have some knowledge of the file contents, meaning the data type expected. Therefore, the Read from Binary File can chunk the bytes appropriately and return logical data. If the file contains numeric (double) data and you tell it to read strings, the binary will be chunked as if it were doubles and will return gibberish data. For Random Access: Use the Set File Position function to set the read offset to the point in the file you want to begin reading The offset is in bytes; therefore, you must calculate the offset based upon the layout of the file For Sequential Access: You can call the Get File Size function and use the result to calculate the number of items in the file, based upon the size of each item and the layout of the file. You can then wire the number of items to the count terminal of the Read from Binary File function. Alternately, you can sequentially access the file one item at a time by repeatedly calling the Read from Binary File VI with the default count of 1. You can check for the End of File error after calling the Read from Binary File VI or calculate the number of reads necessary to reach the end of the file by using the Get File Size VI. 16
As you might imagine after reading the previous slide, File I/O on the binary level can be quite tedious. To maintain the precision and small file size benefits of binary files and to make reading/writing to the file easier, the TDMS file was developed. A TDMS is actually 2 files: the *.tdms file and the *.tdms_index file. The former holds all the data and the latter facilitates accessing the data, which increases the read/write speeds. Use TDMS files for the following purposes: To store test or measurement data To create a structure for grouping your data To store information about your data To read and write data at high speeds All the information in the TDMS_Index file is also contained in the TDMS file. This is why the TDMS_Index file can be automatically regenerated from the TDMS file. For this reason, you only need to distribute one file the TDMS file. The internal structure of the TDMS file format is publicly documented, so it is possible to create 3 rd party programs to write and read TDMS files. In addition, there is a TDM Excel Add-In Tool (located at ni.com/tdm) you can install to load *.TDMS files into Excel. 17
One thing unique about TDMS files is that you can organize them into channels and channel groups. For example, if you are logging temperature on three different set-ups and at various locations within the set-up, you can create a channel for each temperature sensor and then group them according to which set-up they belong to. Furthermore, you can add properties to either the file, the channel group, or the individual channel to record things such as the operator that installed the set-ups or the location of the sensor within the set-up. Now imagine if you had hundreds of sensors on fifty set-ups. You will need a way to keep everything organized, and the tdms file can help. 18
One thing unique about TDMS files is that you can organize them into channels and channel groups. For example, if you are logging temperature on three different set-ups and at various locations within the set-up, you can create a channel for each temperature sensor and then group them according to which set-up they belong to. Furthermore, you can add properties to either the file, the channel group, or the individual channel to record things such as the operator that installed the set-ups or the location of the sensor within the set-up. Now imagine if you had hundreds of sensors on fifty set-ups. You will need a way to keep everything organized, and the tdms file can help. 19
There are multiple ways to use TDMS files including: express Vis and LabVIEW functions (subvis, API). Also, the Excel Add-In tool is available. Express VI: you will need to select the TDMS radio button in the configuration window API: example block diagrams using the API are in the next couple slides.
You have several option in selecting a method for creating filenames and destination directories. Until now, we have used the File Dialog to specify a file name and location. However, it is often desirable to programmatically create folders and file paths.
With relative instead of hard-coded paths, it is easier to transfer the application to other computers or different folders on the same computer. Use the Application Directory VI to get project directory paths. If your VI is part of a project, Application Directory VI returns path to this project s directory. If your VI is not part of a project, Application Directory VI returns path to the directory with the top-level VI of your application. If your VI is not saved to disk, Application Directory VI returns <Not a Path>. Use the Get System Directory VI to get system directory paths, for example: User Documents, User Desktop, User Temporary Directory. Refer to Context Help or LabVIEW Help for more information about the presented functions and VIs. 23
You may use e.g. Write to Spreadsheet File VI or Write To Measurement File Express VI. When you finish writing the code, run your VI. Then find the file it just created, open it in a text editor and verify if the data were written correctly. If yes, you should also be able to open your text file in a spreadsheet application. 24
You may use the VIs Average Temperature or Temperature Limits created as previous homeworks (see homeworks for the module Implementing a VI ). Add streaming current temperature data to a text (ASCII) file. Remember to open and close the file outside the main program loop. Inside the loop, perform writing to the file (use Write to Text File function). You may want to use a function that converts numeric data to a string these functions are available in Functions >> Programming >> String >> Number/String Conversion palette. You might also find Format Into String function useful (Functions >> Programming >> String >> Format Into String) it can create strings that include data of various types written as text (just as printf function in C language). To create relative path to the file, please refer to slide Creating Relative Paths in this presentation. A more advanced version of this task is to write to the file more than one channel of data. If you are modifying Average Temperature VI, you might save to file the current and averaged temperatures; if you are modifying Temperature Limits VI, you may save current temperature, low limit and high limit (and/or information whether the temperature is within the limits). You may also log the times at which the samples were taken (either a real timestamp date and hour or the time that elapsed sinse the beginning of the measurement). 25 Use string functions to create a string of tab-delimited or comma-delimited values and
You may use Read From Spreadsheet File VI. If you read multicolumn spreadsheet files, remember to set the same separator (tab, comma, ) while reading as was used when the file was written. You may read the values as an array of strings or an array of numeric values. You have to remember that the file might contain a string header in the first row. If you want to get only the numeric values from such a file, you have to remove this header from the array that was read from the file before you use the numeric values. Run the VIs that you created for both previous homeworks: Write Spreadsheet File (if you used Write to Spreadsheet File VI in it) and Logging Data. You should be able to read the files created by them using your Read Spreadsheet File VI. 26