WRITE_FILE
Function Name |
WRITE_FILE |
---|---|
Description |
Allows data to be written to an open file or serial port. When the data has been written, the file or port needs to be closed by using the CLOSE_FILE function (see CLOSE_FILE). If the data needs to contain a 0 byte, you should use WRITE_FILE_BYTE instead (see WRITE_FILE_BYTE). |
Arguments |
FILEID {UDINT} The FILEID is a number that identifies the file or port to which data is to be written. DATA {STRING} The data that is to be written to the file or serial port. For more information on the data types for the inputs and outputs, see Data Type Hierarchy. |
Returns |
Zero {UDINT} The WRITE_FILE function returns zero. |
NOTE: You should only use the WRITE_FILE function in ST Programs.
Example:
ST Program - WRITE_FILE:
To use a WRITE_FILE function in an ST program, you need to use this syntax:
- WriteExport := WRITE_FILE( FILEID, DATA );
Where WriteExport, FILEID, and DATA are variables that are declared earlier in the ST program. The FILEID variable has to be a UDINT and identifies the file or port to which the data is to be written and the DATA string is the actual data that is written.The WriteExport variable will store the value that is returned by the WRITE_FILE function.
A common use for the WRITE_FILE function is to provide a serial ASCII export process. To achieve this, an OPEN_PORT function is used to open a serial port and then a WRITE_FILE function is used to write data to the port:
- FileId := OPEN_PORT('COM1,9600,8,N,1,NONE');
- ErrorCode := WRITE_FILE(FileId, WriteString);
- FileId := CLOSE_FILE(FileId);
In this code, the ST Program opens the defined COM port and then performs a WRITE_FILE function. The WRITE_FILE function writes the string that is stored in the WriteString variable to the port (the FileId in the WRITE_FILE function declaration corresponds to the FileId that is returned by the OPEN_PORT function. When the string has been written to the port, the CLOSE_FILE function is used to close the port. This allows data stored in a string to be written to a serial port so that it can be accessed by other applications etc.