TOPICS
ni.com/training
0
TOPICS
ni.com/training
File Input and Output
A. Understanding Resources
B. File I/O
C. File Formats
D. Creating File Paths
1
ni.com/training
A. Understanding Resources
Definition of Resources
Accessing Resources in LabVIEW
2
ni.com/training
What are Resources?
• LabVIEW includes VIs and functions to
allow you to access your resources.
• Resources are known to the system by a
path, name, port, or other identifier.
3
Resource
– An addressable file,
hardware device, object, or network
connection available on your system
ni.com/training
Accessing Resources in LabVIEW
A typical resource operation involves the following
process:
4
Open,
Initialize
or Create
Read
and/or
Write
Close
Check for
Errors
• Specify the path or device name.
• LabVIEW creates a refnum as a
unique identifier to the resource.
• A refnum is a temporary pointer to the
resource.
• The refnum becomes obsolete.
ni.com/training
Accessing Resources in LabVIEW
Sample refnum wires:
5
ni.com/training
B. File I/O
Understanding File I/O
File Formats
High-Level and Low-Level I/O
6
ni.com/training
Understanding File I/O
• File I/O writes to or reads from a file.
• A typical file I/O operation involves the following
process:
7
Open/
Create/
Replace File
Read
and/or
Write to File
Close
File
Check for
Errors
ni.com/training
File Formats
LabVIEW can use or create the following file formats:
• Binary—Efficient, compact, and allows random access
reading but not a human-readable file. Commonly used
for high-speed and multi-channel DAQ.
• ASCII (text)—Human-readable text file where data is
represented as strings. Commonly used for low-speed
DAQ.
• LVM—Format built on ASCII, the LabVIEW measurement
data file (.lvm) is a tab-delimited text file you can open
with a spreadsheet application or a text-editing
application.
• TDMS—An NI-specific binary file format that contains
data and stores properties about the data.
8
ni.com/training
High-Level File I/O
• High-level
File I/O functions:
− Perform all three steps (open,
read/write, close) for common
file I/O operations.
− Might not be as efficient as the
functions configured or
designed for individual
operations.
• Low-level
File I/O functions:
− Provide individual functions for
each step.
− Are efficient for writing to a file
in a loop.
9
ni.com/training
High-Level File I/O
Write to Spreadsheet File
• Converts an array of double-precision
numbers to a text string and writes the string
to an ASCII file.
Read From Spreadsheet File
• Reads a specified number of lines or rows
from a numeric text file and outputs a
2D array of double-precision numbers.
Write To/Read From Measurement
File
• Express VIs that write data to or read data
from an LVM or TDMS file format.
10
ni.com/training
Understanding Low-Level File I/O
Functions
11
DISCUSSION
Exercise:
Logging Data
What would happen if you used the Write to
Measurement File Express VI inside a While loop?
ni.com/training
C. File Formats
Text (ASCII) Files
Binary Files
TDMS Files
13
ni.com/training
Text (ASCII) Files
• Use ASCII files in the following situations:
− You want to access the file from another
application.
− Disk space and file I/O speed are not crucial.
− You do not need to perform random access reads
or writes.
− Numeric precision is not important.
14
ni.com/training
Binary Files: Writing
Boolean
•
LabVIEW represents
Boolean values as 8-bit
values in a binary file
•
Eight zeroes represents
False : [00000000]
•
Any other value represents
True: [00000001],
[01000110], [11111111],
etc.
Integers
Binary Value U8 Value
00000000
0
00000001
1
00000010
2
11111111
255
Strings
• series of unsigned 8-bit integers, each of which has
a value in the ASCII Character Code Equivalents
Table
Arrays
• represented as a sequential list of the elements
(representation dependent on data type of element)
• a header contains a 32-bit integer representing the
size of each dimension
ni.com/training
Binary Files: Reading
Two methods of accessing data:
• Sequential Access—Read each item in order, starting at the
beginning of a file
• Random Access—Access data at an arbitrary point within the
file
Random
Sequential
ni.com/training
TDMS Files
(Technical Data Management
Streaming)
TDMS file
• Binary file (.tdms) that contains data and stores properties
about the data
TDMS_Index file
• Binary index file (*.tdms_index) that provides consolidated
information on all the attributes and pointers in the TDMS
file
• Speeds up access to the data while reading
• Automatically regenerated if lost
TDMS file format internal structure is publicly documented
ni.com/training
File I/O: TDMS
Channel
• Stores measurement signals or raw data in a TDMS file
• Each channel can have properties describing the data
• The data stored in the signal is stored as binary data on disk to
conserve disk space and improve efficiency
Channel Group
• Segment of a TDMS file that contains properties and one or
more channels
• Use channel groups to organize your data and to store
information that applies to multiple channels
ni.com/training
File I/O: TDMS
Properties
• You can assign properties to
• entire file
• Channel Groups
• individual Channels
• Examples:
• File: Date/Time of logging, user signed into system
• Channel Groups: Location of sensor group (top of the
bridge), types of sensors (strain, temperature, etc.)
• Channel: Sensor ID, max/min readings
ni.com/training
File I/O: TDMS Functions
•Use the Express VIs :
•
quick, but little control of organization
--- Write to Measurement File
--- Read from Measurement File
•Use the TDM Streaming API
• Total control over organization, more involved set-up
•Use the TDM Excel Add-In Tool
• Allows you to read TDMS files in Microsoft Excel
20
ni.com/training
D. Creating File Paths
Methods of Creating Paths
Creating Relative Paths
21
ni.com/training
Methods of Creating File and Folder
Paths
Hard-coded paths
− Useful for quick prototypes.
− Not recommended for applications.
File Dialog
− Allow user to specify the path to a file or directory.
− Customize dialog options to limit file types (*.txt).
Programmatic creation
− Create consistent filenames and extensions.
• Example: testdata_001.txt, testdata_002.txt, etc.
− Specify a consistent location.
ni.com/training
Creating Relative Paths
•
Relative paths set paths relative to the application or
system directory.
•
Paths differ based on the operating system and user.
•
Look up these functions and subpalette on File I/O
functions palette:
ni.com/training
Homework:
Write Spreadsheet File
Use a high-level file I/O VI to write
to a spreadsheet-readable file.
ni.com/training
Homework:
Logging Data
Modify a VI to create an ASCII file using disk
streaming.
Save (create) the file in a path relative to the VI or
project.
ni.com/training
Homework:
Read Spreadsheet File
Use file I/O VIs or functions to read spreadsheet
files created in the previous homeworks.