FB_FileWrite

FB_FileWrite 1:

Data can be written into a file with this function block. The file must previously have been created and opened in the write mode.

VAR_INPUT

VAR_INPUT
    sNetId          : T_AmsNetId;
    hFile           : UINT;
    pWriteBuff      : DWORD;
    cbWriteLen      : UDINT;
    bExecute        : BOOL;
    tTimeout        : TIME;
END_VAR

sNetId    : Is a string containing the AMS network identifier of the target device to which the ADS command is directed.

hFile    : Is occupied by the file handle already created by FB_FileOpen.

pWriteBuff : Contains the address of the buffer containing the data to be written. The buffer can be a single variable, an array or a structure, whose address can be found with the ADR operator.

cbWriteLen : Contains the number of bytes to be written.

bExecute    : The ADS command is triggered by a rising edge at this input.

tTimeout    : States the time before the function is cancelled.

VAR_OUTPUT

VAR_OUTPUT
    bBusy       : BOOL;
    bError      : BOOL;
    nErrId      : UDINT;
    cbWrite     : UDINT;
END_VAR

bBusy:    This output remains TRUE until the block has executed a command, but at the longest for the duration supplied to the ‘tTimeout’ input. While bBusy = TRUE, no new command will be accepted at the inputs. Please note that it is not the execution of the service but its acceptance whose time is monitored.

bError:    This output is switched to TRUE if an error occurs during the execution of a command. The command-specific error code is contained in ‘nErrId’.

nErrId:    Contains the command-specific ADS error code of the most recently executed command.

cbWrite : Contains the number of bytes successfully written.

If a write error occurs the number of successful written data bytes is smaller than the requested length  (cbWriteLen) or zero. A write error occurs e.g. if the data medium is full. The outputs bError and nErrID  are not set if  write error occur. Because the PLC application knows the number of data bytes to be written, the real written length is compared with the requested length and write errors can be found. If a write error occurs the internal data pointer has an undefined position.

 

Function specific ADS error code

Possible reason

0x703

Invalid or unknown file handle.

0x70E

File was opened with wrong method ( e.g. with 'obsolete' FILEOPEN function block ).

 

Example of calling the block in FBD:

PROGRAM Test
VAR
    fbFileWrite      : FB_FileWrite;
    hFile            : UINT;
    bFileWrite       : BOOL;
    bFileWriteBusy   : BOOL;
    bFileWriteError  : BOOL;
    nFileWriteErrorId: UDINT;
    nFileWriteCount  : UDINT;
    FILEDATA         :ARRAY[0..9] OF BYTE;
END_VAR

FB_FileWrite 2:

In the example, after a rising edge at ”bFileWrite", 10 bytes of the array ”FILEDATA " are written to the file.

Requirements

Development environment

Target system type

PLC libraries to include

TwinCAT v2.8.0

PC or CX (x86)

TcSystem.Lib

TwinCAT v2.10.0 Build >= 1301

CX (ARM)

TcSystem.Lib