Topic: DMD0389

Help File Version: 2.10.0.636

FILEREAD - Read from File


The Read from File (FILEREAD) instruction is used to retrieve data from an open file and store that data in a String or a numeric data block.

 

 

File Handle Struct - when a file is opened the File Handle Structure that is associated with that file is initialized and remains valid until it is closed using the Close File (FILECLOSE) instruction. The File Handle Struct is used by other FILE instructions when accessing the file. File Handle Struct names must be unique, and consist of 1 to 16 characters in length and consist of any combination of alphanumeric characters and underscores ('_', 'a-z', 'A-Z', 0-9), no spaces or punctuation marks are allowed, and must begin with a letter or an underscore.

 


Complete When ... - this group of options sets the condition (or conditions) that are used to signal the completion of the Read From File instruction. Once the Complete When... condition is met, the data from the beginning of the file to the specified character is copied from the file to the Data Destination location.

 

Length - specifies the number of characters to receive in order to signal completion of the instruction. This can be any positive constant value or any readable numeric location.

 

Delimiters - specifies from one to three characters that signal the completion of the instruction.

Exact Sequence - the specified characters must be receive in the order specified.

 

Any One Delimiter - receipt of any of the specified characters will signal completion.

 

Trim Delimiter(s) from Output String - specifies whether the delimiter characters should be removed from the input data before it's stored in the specified Data Destination.

 


Data Destination - selects where to store the data that has been read from the file. The option chosen will depend on the type of data that is received from the file. If the data is ASCII text the appropriate choice is String Structure. If the data is binary, or simple bytes of data the appropriate choice is a numeric block of bytes.

 

  • String Structure - designates a String where the data read from the file will be stored. This can be any of the system-defined Short Strings, or system-defined Long Strings, or any of the user-defined Strings.

     
  • Numeric Data Block - designates a memory block to store the data read from the file.

Start Address - the offset into an existing numeric data block to begin storing the data read from the file.

 

Buffer Size in Bytes - the maximum number of Bytes of data that can be placed in the data block. This can be any numeric location or any constant value. This value is NUMBER OF BYTEs; for data block of Words there are 2 Bytes per element, for DWord data blocks there are 4 Bytes per element.

 

Number of Bytes to Read - designates a location to store the number of bytes that were read from the file and stored in the designated location. This can be any writable numeric location.

 

Create Byte Buffer - many communication protocols are constructed using Bytes of data, but the Do-more controller does NOT have a preconfigured block of Unsigned Bytes. If a Byte buffer is needed, clicking Create Byte Buffer will open the Create Unsigned Byte Buffer Block dialog .where one can be created.

 

Data Block Name (1 to 16 letters) - Block names must be unique, and consist of 1 to 16 characters (A-Z, a-z; no numbers, no spaces). The default name FILEREADBuff can be changed if desired.
 

Number of Elements - specifies the size of the data block to create. Data blocks must be created on a DWord (4-byte) boundary.

 

Unsigned Byte Data Block Range - displays the first and last element of the block that will be created based on the current entries for Data Block Name and Number of Elements.

 

Data Type - the data block will consist of Unsigned Bytes.
 

Make data block retentive (retain values after power loss) - a data block marked as retentive will hold its state through a power cycle or a Program-to-Run mode transition. The status of memory NOT marked as retentive will be cleared at power up and during a Program-to-Run mode transition.

 


The On Success and On Error parameters specify what action to perform when this instruction completes. You do not have to use the same type of selection for both On Success and On Error.

 

If the Set Bit selection is used for either On Success or On Error, the specified BIT location will be SET OFF when the instruction is first enabled and will remain OFF until the instruction completes. Once complete, the appropriate Success or Error bit location will be set ON. The specified Bit location is enabled with a SET (Latch) operation (not an OUT operation) meaning that it will remain ON even if this instruction's input logic goes OFF.

 

If the JMP to Stage selection is used for either On Success or On Error the target Stage must be in the same Program code-block as this instruction, you cannot specify a target Stage that exists in a different Program code-block. When the operation finishes, the target Stage will be enabled the same way as a standalone Jump to Stage (JMP) instruction would do it. The JMP to Stage option will only be selectable if this instruction is placed in a Program code-block.

 

On Success selects which of the following actions to perform if the operation is successful:

  • Enable Set Bit then specify any writable bit location.
  • Enable JMP to Stage then specify any Stage number from S0 to S127 in the current Program code-block.

 

On Error selects which of the following actions to perform if the operation is unsuccessful:

  • Enable SET Bit then specify writable bit location.
  • Enable JMP to Stage then specify any Stage number from S0 to S127 in the current Program code-block.

 

If either the On Success or On Error selections are set to JMP to Stage, Automatically create the SG box for any NEW stage number will be enabled which will automatically create any target stage that does not already exist.

  • Below this rung will create the new target stage on a new rung following this instruction.
  • At end of code-block will create the new target stage on the last rung of this Program.

 


Error Codes

Improper use of any FILE instruction or any file system hardware problem will generate a warning message at runtime. When any of these warnings are generated, the On Error parameter in the instruction will be ON and the System ERR variable will contain one of the following text messages. You will also be alerted to this condition by yellow "Warning" text displayed in the Status Bar of Do-more Designer. Clicking that text will open the System Status to display the full text of the warning message.

 

 

Text displayed in the ERR System variable

 

How To Solve / What To Do

SdCard not installed, or has an error

 

On PLC systems that have an SD Card slot, this indicates the SD card is either missing or an error occurred while trying to mount the file system on that media.

 

Note: you can use the File System structure's .MediaInstalled and .MediaMounted in permissive contacts with the input logic of the FILEOPEN instruction to make sure the removable media is properly installed and available before an attempt is made to execute the instruction.

 

Attempted to use a device that hasn't been opened

 

An attempt was made to read from a file using a File Handle that is not initialized. This could be because it was never initialized, or a Close File (FILECLOSE) operation was used before the Read from File operation was attempted, or the SDCard was removed after the Open File (FILEOPEN) was executed.

 

 

The following actions will not generate a Warning or set the On Error condition, but you might not get the data that's expected in the Data Destination.

 

The Read from File is configured to read more data than is contained in the file.

All of the data that was read from the file that will fit in the Data Destination will be stored. Any data that will not fit will be lost. You can prevent this from happening by using the .FileSize and .FilePointer structure members to determine how much data the Read from File operation has access to.

The Read from File is configured to read more data than can be stored in Data Destination.

the amount of data that will fit in the destination will be placed there and any remaining data will be lost. You can prevent this from happening by using the .FileSize and .FilePointer structure members to determine how much data the Read from File operation has access to, and using the .Length member of a String structure or the size of the data block to make sure you can store all of the data that can be read.

The Read from File is configured to look for Termination Characters but they are not found in the file.

The Data Destination will contain as much data as is read from the file and can fit in the destination.

The Read from File attempts to run with the file pointer at the end of the file.

There is no data read and subsequently no data stored in the Destination. You can prevent this from happening by using the .EndOfFile structure member.

 


File Handle Structure

 

Each time the Open File (FILEOPEN) instruction is executed the structure associated with the File Handle is initialized. The contents of the structure's fields will remain valid until a Close File (FILECLOSE) instruction is executed, which will uninitialize the structure. The File Handle structure has the following member fields which can be used in the ladder logic.

.DeviceID (read only) - a 16-bit value that uniquely identifies this file handle.

 

.Open (read only) - a boolean value that will be ON if the associated file is currently open, and will be OFF if the associated file is not open.

 

.Success (read only) - not used.

 

.Error (read only) - not used.

 

.EndOfFile (read only) - a boolean value that will be ON if the file pointer is at the end of the file, that is, after the last byte of data in the file.

 

.FileSize (read only) - a 16 bit value that contains the current number of Bytes in the file.

 

.FilePointer (read only) - a 16 bit value that contains the current location (Byte offset from the beginning) of the file pointer.

 


Status Display

 

The red triangle in the upper left corner of the status display indicates this is a Fully Asynchronous instruction.

 

The gray triangle at the right end of the input leg indicates the input is edge-triggered, meaning this instruction will execute each time the input logic transitions from OFF to ON.

 


See Also

FILECLOSE - Close File

 

FILECOPY - Copy File

 

FILEDEL - Delete File

 

FILELOG - Log to File

 

FILENEWFLDR - Make New Folder

 

FILEOPEN - Open File

 

FILEQUERY - Query File or Folder Information

 

FILEREAD - Read from File

 

FILESEEK - Seek to Position in File

 

FILESYSCMD - Perform File System Command

 

FILETRUNC - Truncate File

 

FILEWRITE - Write to File

 

EMAIL - Send Email (with file attachment)

 


Related Topics

File System Reference

 

Browse PLC File Systems

 


Example