Summary
Reads the defined number of bytes from an address and writes the data to the defined file. Data storage modes include "New", "Append" or "Overwrite" as shown in the Storage Format Overview table.
Format
_CF_write/_SD_write/_USB_write (folder names, file names, read from addresses, offset, number of bytes, mode)
Parameter 1
Folder name: Fixed string (Maximum length is 32 single-byte characters)
Parameter 2
File name: Fixed string (Maximum length is 32 single-byte characters), Internal Device (Maximum length is 14 single-byte characters), Internal Device designated with offset (Internal Device + Temporary address, Maximum length is 14 single-byte characters)
Parameter 3
Read From Address: Device address, Device address + Temporary address
Parameter 4
Offset: Numeric Value, Device address, Temporary address (Maximum number that can be specified is 65535 for 16-bit length, 4294967295 for 32-bit length)
Parameter 5
Number of bytes: Numeric Value, Device address, Temporary address (Maximum 1280)
Parameter 6
Mode: Numeric Value, Device address, Temporary address (Available values are 0, 1, 2)
Storage Format Overview
Mode |
Name |
Description |
0 |
New |
Create a new file. If a file with the same name exists, it is deleted. |
1 |
Add |
Add the data to a specified file. If the specified file does not exist, a new file is created. |
2 |
Overwrite |
Overwrite part of the file. If the specified offset is larger than the file size, the surplus area is filled with 0s and the data is written after the area. If the offset is specified at the end of the file data, the operation is equivalent to adding the data to the file. If the file does not exist, an error occurs. For more information about this error, please see 22.11.7.7 Label Settings. |
Example expression:
[w:[#INTERNAL]LS0200] = 0 //Offset ("0" when the mode is "New")
[w:[#INTERNAL]LS0202] = 100 // Number of bytes (100 bytes).
[w:[#INTERNAL]LS0204] = 0 // Mode (new).
_CF_write ("\DATA", "DATA0001.BIN", [w:[#INTERNAL]LS0100], [w:[#INTERNAL]LS0200], [w:[#INTERNAL]LS0202], [w:[#INTERNAL]:LS0204])
In the previous example, 100 bytes of data is read from LS0100 and stored in the \DATA folder as DATA0001.BIN. You can indirectly define the byte count and mode by defining the offset, byte count, and mode with internal devices.
Defining the file name with an internal device
_CF_write ("\DATA", [w:[#INTERNAL]LS0100], [w:[#INTERNAL]LS0200], 0, 100, 0)
By storing the file name in LS0100, you can indirectly specify the file name. In this example, a file name is stored in LS0100 through LS0106 as follows.
For example, when Text Data Mode = 1
The above expression reads 100 bytes of data from LS0200 and creates a new file ("\DATA\DATA0001.BIN") to store the data.
For information about data storage order, please refer to the following.
22.10.7 Data Storage Mode
The error status is stored for any error that occurs at the time of access to external storage or during file operation. Even when errors occur, the process continues. Therefore, when using the file operation function, always write a script that checks for errors.
22.11.7.7 Label Settings
You can only use the 8.3 format (a maximum of 12 characters, with 8 characters for the file name, the period, and 3 characters for the extension) for the file name. You cannot use file names longer than 12 characters.
To set a root folder, specify " " (empty string) as the folder name.
Maximum length of parameter 1 folder name is 32 single-byte characters.
The folder name can be appended to the parameter 2 file name. Maximum length of parameter 2 folder name + file name is 32 single-byte characters.
For example, \DATA\01\DATA.bin
An Internal Device can be specified for the second parameter (File name). Specifying Internal Device allows indirect addressing of a file name. Also, when using internal devices the maximum folder name + file name length is 14 single-byte characters.
When you specify an internal device for the file name, the file name storage order is defined by the Text Data Mode.
7.9.1 System Settings [Device/PLC] Settings Guide
The offset setting is effective only in "Overwrite" mode. The offset setting is disabled in "New" and "Add" modes. Set the offset value to "0" in modes other than "Overwrite" mode.
When "New" mode is specified and a file with the same name already exists, it is overwritten.
When an internal device is specified for the File name, "Read From Address" is not counted as a D-Script address.
When a PLC device is defined as the "Read From Address", data is read from the PLC only once when the function is executed. If an error occurs during data read, it results in an external storage error status: [s:CF_ERR_STAT]/[s:SD_ERR_STAT]/[s:USB_ERR_STAT]. The error is cleared when the data read is successfully completed.
The data is divided into items and read from the source, although this depends on the number of bytes to be read. Therefore, even if a communication error occurs during data read, the data may have been partially written to the specified file.
To set a full path for a file name, specify "*" (asterisk) as the folder name. For example, _CF_read ("*", "\DATA\DATA0001.BIN", [w:[#INTERNAL]LS0100], 0, 10)
Storage format example expression
When "New" mode is specified
In the example above, 100 bytes of data are read from LS0100 and the DATA0001.BIN file is newly created in the \DATA folder.
When "Append" mode is specified
If the specified file (DATA0001.BIN in the example) already exists and the statement above is executed, 100 bytes of data are read from LS0100 and following areas and added to the DATA0001.BIN file in the \DATA folder.
When "Overwrite" mode is specified (1)
If the specified file (DATA0001.BIN in the example) already exists and the above statement is executed, 10 bytes of data stored in LS0100 and following areas are read and overwritten over the 10 bytes of data stored in the 17th and following bytes after the offset in the DATA0001.BIN file in the \DATA folder.
When "Overwrite" mode is specified (2)
(The file to be overwritten is less than the sum of the offset value and number of added bytes.)
The specified file (DATA0001.BIN in the example) already exists and the file size is 100 bytes. When the offset is set to 96 bytes and the number of bytes is set to 10 bytes for the overwrite operation, 10 bytes of data stored in LS0100 and following areas are read. Then, the first 4 bytes of readout data overwrite the 4 bytes of data stored in the 97th and following bytes in the file, and the remaining 6 bytes of data are added to the end of the file data. The resulting file contains 106 bytes of data.
When "Overwrite" mode is specified (3)
(The file to be overwritten is smaller than the offset value.)
The specified file (DATA0001.BIN in the example) already exists and the file size is 100 bytes. When the offset is set to 110 bytes and the number of bytes is set to 10 bytes for the overwrite operation, the area between the 101st byte and 110th bytes is filled with 0s and the 10 bytes of data read from LS0100 and following areas are written in the 111th and following bytes. The resulting file contains 120 bytes of data.