Y_RW_Read_CSV_File
This function block will read CSV (ASCII) data from a file on the controller's flash or ram disk. The raw file data will be parsed and copied into a user defined data structure. This function block requires customization to accommodate application specific data requirements. Any variety of rows and columns and datatypes can be specified.
Parameters:
| * | Parameter | Data Type | Description | Default |
|---|---|---|---|---|
| VAR_IN_OUT | ||||
| V | Data | Yt_RW_MyDataStruct | A user customized data structure containing the definition of the rows and columns of data to be processed. | |
| VAR_INPUT | ||||
| B | Execute | BOOL | Upon the rising edge, all other function block inputs are read and the function is initiated. To modify an input, change the value and re-trigger the execute input. | FALSE |
|
V
|
FileName | STRING | The file to be read. See path example in Memory area available for user files in ic92xx | STRING#'' |
| V | Separators | Yt_RW_SeparatorList | Optional. If unconnected, the default separator is a comma (BYTE#44) to detect each value column by column. If a different or multiple characters must be treated as a value separator, populate the Yt_RW_SeparatorList with up to four byte values equating to the ASCII value of the separators. | Comma (BYTE#44) |
| V | HeaderRows | UINT | Optional. If connected, the value indicates the number of rows this function block must ignore before starting to look for actual data. | UINT#0 |
| V | VerifyVersion | BOOL | Optional. If TRUE, this function block will expect the first line of the file to contain a version code for identifying the data format of the file, i.e columns, datatypes, etc. This allows for future changes to the Yt_RW_MyDataStruct while retaining the ability to parse older files created before a change was made to the structure of the file. | FALSE |
| V | BufferSize | UDINT | Specifies the number of bytes in the file to process at one time. If unconnected, the default is 2048 bytes. BufferSize can be adjusted up or down if necessary to accommodate various file sizes and will depend upon the CYCLIC task in which the Y_RW_Read_CSV_File function block is executed. | UDINT#2048 |
| VAR_OUTPUT | ||||
| E | Done | BOOL | Set high when the commanded action has completed successfully. If another block takes control before the action is completed, the Done output will not be set. This output is reset when Execute goes low. | |
| B | Busy | BOOL | Set high upon the rising edge of the Execute input, and reset when Done, CommandAborted, or Error is true. In the case of a function block with an Enable input, a Busy output indicates the function is operating, but not ready to provide Valid information. (No Error) | |
| B | Error | BOOL | Set high if an error has occurred during the execution of the function block. This output is cleared when 'Execute' or 'Enable' goes low. | |
| E | ErrorID | UINT | If Error is true, this output provides the Error ID. This output is reset when 'Execute' or 'Enable' goes low. | |
| V | ErrorRow | INT | If Error is true and pertains to a problem with the source data, this value will indicate the location of processing when the error occurred. | |
| V | ErrorCol | INT | If Error is true and pertains to a problem with the source data, this value will indicate the location of processing when the error occurred. | |
Notes:
- The filename must conform to 8.3 format, but is not case sensitive.
- Any separator can be specified provided it is an ASCII byte, and will not be confused with the actual data.
- Header rows are not required to contain the same number of separators as the data content. (Separators are not checked in the header rows.)
- Supports Carriage Return and Line Feed as end of line delimiters.
- It takes 6 scans per processing of each BufferSize of data. If a file has 20480 bytes, and the BufferSize is 2048, and the function block is placed in a 100mSec scan, then the total time to process the file will be 60 scans, or 6 seconds. (20480/2048 * 6 * 100) = 6000 mSec.
Error Description:
See the Function Block ErrorID List .
Examples:
Consider a file as shown below.
- Copy & paste the Yt_RW_MyDataStruct and associated datatypes into the main project, and rename them to avoid conflict with Yt_RW_MyDataStruct in the DataType file of the Y_FileRW_Toolbox.
- Initialize the data structure with the Column and MaxRecords fields.
- This file is read using the Y_RW_Read_CSV_File FB as shown.
Customization:
- To customize the function block, go to the variables grid and rename the datatype used as the VAR_IN_OUT to the datatype you customized above.
- Locate the comments near the middle of the Y_RW_Read_CSV_File function indicating the area to be customized. Modify the lines that convert the STRING data from the file into the Yt_RW_MyDataStruct structure.
Versioning:
The function has the capability to read multiple versions of the same file. For example, assume that initially, the design requires a data file to contain 4 columns of data to be used as INT. Later, after some machines are in the field, a design change requires that the data file must now contain 5 columns of DINT. If a version code is applied as the first row, the function block can determine how to read the file for any number of variations. That may come later. This will allow the use of older data files as well as newer formats.
- Set the VerifyVersion function block input to TRUE.
- The first line of the data file must contain a version code. The version code does NOT count as a header row. See the graphics above showing original and modified file specification
- Customize the DataType to reflect the most current data specification.
- Customize the Y_RW_Read_CSV_File function block to determine if the version code detected is supported.
To use file versioning, follow the steps below:
- Customize the Read_CSV_File function block to read multiple versions.
- NOTE: The capability of the function block to read multiple file versions is limited by the changes that can be made to the DataType Definition. It is not practical to use the version code to read completely different data formats. Make two copies of the Y_RW_Read_CSV_File and customize accordingly.