WritePersistentData

WritePersistentData 1:

If persistent variables are defined in a PLC runtime system, their current values are normally saved in a .bootdata file in the TwinCAT\Boot folder when stopping/shutting down the TwinCAT system (following the last PLC cycle). Before writing the current persistent data to the file, a backup of the old persistent data is made by renaming the system's old .bootdata file to .bootdata-old.

A file is created for every runtime system that is configured.

The next time the system starts, the .bootdata file is read and the persistent variables in the runtime system are initialized with the values from the file.

This backup file (.bootdata-old) of the persistent data is read at system startup if the file (.bootdata) containing the persistent data does not exist. This is an exception, but it can occur, for example, if an IPC without UPS experiences a power failure and TwinCAT could not shut down properly.

With the function block WritePersistentData you can initiate the saving of the persistent data from the PLC program and ensure that an up-to-date .bootdata file with the persistent data is available. The PORT input parameter specifies the runtime system whose persistent data is to be saved.

WritePersistentData 2: Inputs

VAR_INPUT
    NETID  : T_AmsNetId;
    PORT   : T_AmsPort;
    START  : BOOL;
    TMOUT  : TIME := DEFAULT_ADS_TIMEOUT;
END_VAR

Name

Type

Description

NETID

T_AmsNetID

Network address of the TwinCAT computer on which the ADS command is to be executed. An empty string can be entered for the local computer

PORT

T_AmsPort

ADS port number of the PLC runtime system whose persistent data is to be saved.

START

BOOL

The function block is enabled by a positive edge at this input.

TMOUT

TIME

Time-out period that may not be exceeded when executing the ADS command.

WritePersistentData 3: Outputs

VAR_OUTPUT
    BUSY   : BOOL;
    ERR    : BOOL;
    ERRID  : UDINT;
END_VAR

Name

Type

Description

BUSY

BOOL

When the function block is enabled, this output is set and remains set until a feedback is received.

ERR

BOOL

If an ADS error should occur during the transfer of the command, then this output is set once the BUSY output is reset.

ERRID

UDINT

Returns the ADS error number when the ERR output is set.

Example:

PROGRAM MAIN
VAR
    bStart                : BOOL;
    bError                : BOOL;
    bBusy                 : BOOL;
    nErrorId              : UDINT;
    fbWritePersistentData : WritePersistentData;
    fbR_Trig              : R_TRIG;
END_VAR
VAR PERSISTENT
    perA  : INT;
    perB  : BOOL;
    perC  : BYTE;
    perD  : STRING;
    perE  : ARRAY[0..10] OF INT;
    perF  : ARRAY[0..10] OF UDINT;
END_VAR
fbR_Trig( CLK:=bStart );
IF fbR_Trig.Q THEN
    perA := 24443;
    perB := TRUE;
    perC := 7;
    perD := 'Switch ON/OFF';
    perE[ 0 ] := 1;
    perE[ 10 ] := 11;
    perF[ 0 ] := 263;
    perF[ 10 ] := 23323;
    fbWritePersistentData(NETID:='', PORT:=851, START:=bStart, TMOUT:=T#1s );
ELSE
    fbWritePersistentData( START:=FALSE);
END_IF;

bBusy := fbWritePersistentData.BUSY;
bError := fbWritePersistentData.ERR;
nErrorId := fbWritePersistentData.ERRID;

See also: Appendix > System behavior when writing persistent data

Requirements

Development environment

Target platform

PLC libraries to be integrated (category group)

TwinCAT v3.1.0

PC or CX (x86, x64, ARM)

Tc2_Utilities (System)