WritePersistentData

WritePersistentData 1:

If persistent variables are defined in one of the PLC's runtime systems, then their current values are stored in a file in the TwinCAT\Boot directory when the TwinCAT system stops (after the last PLC cycle). A file is created for every runtime system that is configured. The next time the system starts, this file is read, and the persistent variables in the runtime system are initialized with the values from the file. Using the function block "WritePersistentData" it is possible to initiate storage of the persistent data from the PLC program. The PORT parameter specifies the runtime system whose persistent data is to be stored. For additional info see: Writing of persistent data: System behavior. Internally, an instance of the ADSWRTCTRL function block is called.

 VAR_INPUT

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

NETID: It is possible here to provide the AmsNetId of the TwinCAT computer on which the ADS command is to be executed. If it is to be run on the local computer, an empty string can be entered.

PORT: Contains the ADS port number of the PLC run-time system whose persistent data is to be stored. The first PLC run-time system, for example, has port number 801, and the second has port number 811.

START: The function block is activated by a positive edge at this input.

TMOUT: States the length of the timeout that may not be exceeded by execution of the ADS command.

VAR_OUTPUT

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

BUSY: When the function block is activated this output is set. It remains set until and acknowledgement is received.

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

ERRID: Supplies the ADS error number when the ERR output is set.

 

PLC Example Program in ST:

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:=801, START:=bStart, TMOUT:=T#1s );
ELSE
    fbWritePersistentData( START:=FALSE);
END_IF;

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

Requirements

Development environment

Target system type

PLC libraries to include

TwinCAT v2.7.0

PC or CX (x86)

Standard.Lib; PLCSystem.Lib; TcPLCAds.Lib; PLCHelper.Lib; TcPlcUtilities.Lib

TwinCAT v2.8.0

PC or CX (x86)

TcUtilities.Lib

( Standard.Lib; TcBase.Lib; TcSystem.Lib are included automatically )

TwinCAT v2.10.0 Build >= 1301

CX (ARM)