Virtual Instrumentation-Concept of Strings and File I/O Dr. Jainy Sachdeva Associate Professor EIED, TIET, PATIALA Course: VIRTUAL INSTRUMENTATION (UEI-844) Faculty Name: Dr. Jainy Sachdeva (Associate Professor) (Course Coordinator) Lecture Contents: Concept of Strings & File I/O Introduction to Strings Functions of Strings Functions of File I/O Contents of this lecture are based on the following books: 1. Johnson, G., LabVIEW Graphical Programming, McGraw Hill (2006). 2. Sokoloft, L., Basic Concepts of LabVIEW 4, Prentice Hall Inc. (2004). 3. Wells, L.K. and Travis, J., LabVIEW for Everyone, Prentice Hall Inc. (1996). 2 Introduction A string is a sequence of displayable or non-displayable ASCII characters. Strings provide a platform-independent format for information and data. Strings often are used to send commands to instruments, to supply information about a test (such as operator name and date), or to display results to the user. Applications Creating simple text messages. Controlling instruments by sending text commands to the instrument and returning data values in the form of either ASCII or binary strings which you then convert to numeric values. Storing numeric data to disk. To store numeric data in an ASCII file, you must first convert numeric data to strings before writing the data to a disk file. Instructing or prompting the user with dialog boxes. String Controls / Indicators String Controls are used to input a text. String Indicators are used to display a text. The string control and indicator is located on the Controls»Text Controls and Controls»Text Indicators palettes to simulate text entry boxes and labels. Right-click a string control or indicator on the frontpanel to select from the display types. Fig:- Message for each Display Examples - Display Types Fig:-Normal Display Fig:-Code Display Fig:-Password Display Fig:-Hex Display String Functions String functions behave similarly to array functions; in fact, strings are really just arrays of ASCII data. Use the String functions to concatenate two or more strings, extract a subset of strings from a string, convert data into strings, and format a string for use in a word processing or spreadsheet application. Fig:-All String Functions in Law View String Length Returns in length the number of characters (bytes) string, including spaces & characters. For example, the String Length function returns a length of 19 for the following string:The quick brown fox. Fig:-String Length Concatenate Strings Concatenates input strings and 1D arrays of strings into a single output string. For array inputs, this function concatenates each element of the array. Add inputs to the function by right-clicking an input and selecting Add Input from the shortcut menu or by resizing the function. Fig:-Concatenate Strings String Subset Returns the substring of the input string beginning at offset and containing length number of characters. The offset of the first character in string is 0. For example, if you use the string Volts DC+1.22683E+1; as the input, the String Subset function returns the following substring for an offset of 6 and a length of 2: DC. Fig:-String Subset Match Pattern Searches for regular expression in string beginning at offset, and if it finds a match, splits string into three substrings. If no match is found, match substring is empty and offset past match is –1. For example, use a regular expression of + and use the following string as the input: VOLTS DC+1.22863E+1. The Match Pattern function returns a before substring of VOLTS DC, a match substring of +, an after substring of 1.22863E+1, and an offset past match of 9. A regular expression requires a specific combination of characters for pattern matching. Fig:-Match Pattern Match Regular Expression Searches for a regular expression in the input string beginning at the offset you enter. If the function finds a match, it splits the string into three substrings and any number of submatches. Resize the function to view any submatches found in the string. Array to Spreadsheet String Fig:-Match Regular Expression Converts an array of any dimension to a table in string form. They containing tabs separating column elements, a platform-dependent EOL character separating rows, for arrays of three or more dimensions, headers separating pages. Fig:-Array to Spreadsheet String Build Text Creates an output string from a combination of text and parameterized inputs. If the input is not a string, this Express VI converts the input into a string based on the configuration of the Express VI Carriage Return Constant Consists of a constant string containing the ASCII CR value. Empty String Constantnverts Consists of a constant string that is empty (length zero). Fig:-Build Text End of Line Constant Consists of a constant string containing the platform-dependent end-of-line value. Format Date/Time String Displays a timestamp value or a numeric value as time in the format you specify using time format codes. Time format codes include the following: %a (abbreviated weekday name) %b (abbreviated month name) %c (locale-specific date/time) %d (day of month) %H (hour, 24-hour clock) Fig:-Format Date and Time String %I (hour, 12-hour clock) . %m (month number) %M (minute) %p (a.m./p.m. flag) %S (second) %x (locale-specific date) %X (locale-specific time) Fig:-Format Date and Time String %y (year within century) %Y (year including century) %<digit>u (fractional seconds with <digit> precision). Format Into String . Formats string, path, enumerated type, time stamp, Boolean, or numeric data as text. Line Feed Constant Consists of a constant string containing the ASCII LF value. Replace Sub String Inserts, deletes, or replaces a substring at the offset you specify in string. Fig:-Replaced Sub String Thank you… Virtual Instrumentation-Concept of Strings and File I/O Dr. Jainy Sachdeva Associate Professor EIED, TIET, PATIALA Course: VIRTUAL INSTRUMENTATION (UEI-844) Faculty Name: Dr. Jainy Sachdeva (Associate Professor) (Course Coordinator) Lecture Contents: Concept of Strings & File I/O Introduction to Strings Functions of Strings Functions of File I/O Contents of this lecture are based on the following books: 1. Johnson, G., LabVIEW Graphical Programming, McGraw Hill (2006). 2. Sokoloft, L., Basic Concepts of LabVIEW 4, Prentice Hall Inc. (2004). 3. Wells, L.K. and Travis, J., LabVIEW for Everyone, Prentice Hall Inc. (1996). 20 Spreadsheet String to Array Array—Converts the spreadsheet string to an array of the dimension and representation of array type. This function works for arrays of strings and arrays of numbers. Scan from String Fig:-Spread Sheet String To Array Scans the input string and converts the string according to format string. Use this constant to supply a one-character space string to the block diagram. Fig:-Scan From String Search & Replace String Replaces one or all instances of a substring with another substring. To include the multiline? input and enable advanced regular expression searches, right-click the function and select Regular Expression. Space Constant Use this constant to supply a one-character space string to the block diagram. Fig:-Search and Relace String Constant Use this constant to supply a constant text string to the block diagram. Tab Constant Consists of a constant string containing the ASCII HT (horizontal tab) value. To Lower Case Fig:-To Lower Case Converts all alphabetic characters in string to lowercase characters. Evaluates all numbers in string as ASCII codes for characters. This function does not affect non-alphabetic characters. To Upper Case Fig:-To Upper Case Converts all alphabetic characters in string to uppercase characters. Evaluates all numbers in string as ASCII codes for characters. This function does not affect non-alphabetic characters. Trim WhiteSpaces Whitespace—Removes all white space (spaces, tabs, carriage returns, and linefeeds) from the beginning, end, or both ends of string. Fig:-Trim White Spaces Converting Numeric Values to Strings Build Text Express VI Use the Build Text Express VI to convert numeric values into strings. The Build Text Express VI,located on the Functions»Output palette, concatenates an input string. If the input is not a string, this Express VI converts the input into a string based on the configuration of the Express VI. When you place the Build Text Express VI on the block diagram, the Configure Build Text dialog box appears. Converting Strings to Numeric Values Scan from String The Scan From String function converts a string containing valid numeric characters, such as 0–9,+, –, e, E, and period (.), to a numeric value. This function scans the input string and converts the string according to format string. Use this function when you know the exact format of the input text. This function can scan input string into various data types, such as numeric or Boolean, based on the format string. Resize the function to increase the number of outputs. Fig:-Scan from String Converting Numeric Values to Strings Example An Express VI is configured to accept one input, voltage, and change it to a fractional number with a precision of 4. The input concatenates the resultant voltage to the string “Voltage is”. The parameters are defined in Configure Build Text dialog box. Fig:-Convert Numeric Values To String Fig:-Convert Numeric Values To String Format String specifies how to convert the input string into the output arguments Scan from String Remaining string returns the portion of the string that remains after scanning all arguments. Input String is the string, path, enum type, time stamp or numeric data to scan Offset past scan is the offset of input string after the scan (i.e position) Initial Scan location is the offset into the string where scan begins. Default is 0 Error out contains error information incase it runs into one Default 1..n specifies the type and default value for the output parameters. If this function cannot scan the input value from format string, the function uses the default. Output 1..n specifies the output parameters. Each output can be a string, path, enumerated type, time stamp, Boolean, or any numeric data type. You cannot use arrays and clusters with this function. Converting Strings to Numeric Example ● ● For example as shown in Figure 9.3 use a format string of %f, an initial search location of 8, and VOLTS DC+1.28E+2 as the input string, to produce an output of 128. Change the precision of the output by changing the precision of the indicator. In format string, % begins the format specifier and f indicates a floating-point numeric with fractional format. Fig:- String To Numeric Fig:- String To Numeric Example- Concatenate Strings This example takes two inputs, string & string 2 & concatenates them using the Concatenate String function discussed earlier. The output is then shown in a String Indicator. Fig:-Concatenate String Fig:-Concatenate String File Input and Output ( I/O) Four Hierarchy Levels: High-level File VIs Intermediate File VIs andFunctions Advanced File Functions sub-palette Express VIs High-level File I/O VIs ● Write to Spreadsheet File ● Read from Spreadsheet File ● Write Characters to File ● Read Characters from File ● Read Lines from File Write to File Example ● Open/Create/Replace opens the existing file TEST1.DAT and generates refnum and error cluster ● Write File writes the data Close File closes the file ● Simple Error Handler checks for errors. Reading Data from a File ● ● ● ● Open/Create/Replace opens the file Read File reads the specified number of bytes from the file Close File closes the file Simple Error Handler checks for errors Formatting a Spreadsheet String ● Spreadsheets are popular tools for data handling and analysis ● There are many formats for spreadsheet data. One of the ● most popular is tab-delimited: ● – Columns are separated by a tab character ● – Rows are separated by an endof-line character Example of Formatting a Spreadsheet String Creating a Spread Sheet Can replace Format Into String and Write File with Format Into File in above example Write LabVIEW Measurement File Includes the open, write, close and error ● Handling functions Handles formatting the string with either a tab or comma delimiter ● Merge Signals function is used to combine data into the dynamic data type ● Example Lab VIEW Measurement File Thank you…
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )