Persist

Persist provides a convenient and compact way of storing variables to permanent memory. Persist can store variables and structures of any type or scope, even local variables in other programs.

Usage

To use Persist, a template of type Persistence_typ will need to be declared, as well as a USINT array with Retain selected.

Inputs

  • pPersistentVariable - Pointer to the USINT array where the data will be persistently stored.
  • sizeofPersistentVariable - The size of the above USINT array in bytes.
  • pDataValid - Pointer to a data valid boolean. If the value of this boolean is true before the init function runs, persist will restore the data from the persistent variable to the variables monitored in the WorkingVariableList.
  • WorkingVariableList - This is a list of variables that should be monitored and stored/restored. For a locally scoped variable, use the following syntax - MyProgram:MyLocalVariable.

Outputs

  • RequiredMemory - The size of the memory required to store the variables monitored in WorkingVariableList.
  • Initialized - Will be TRUE when PersistFN_Init has been run successfully.
  • Error - Will be TRUE if Persist has an error.
  • ErrorID - The most recent error ID.
  • ErrorString - The most recent error description.

Initialization

Declare a variable template of type Persistence_typ and a USINT array large enough to store your variables. The USINT array needs to have Retain selected.

VAR
	persist : Persistence_typ;
END_VAR
VAR RETAIN
	persistentData : ARRAY[0..9999] OF USINT;
END_VAR

In your INIT routine, initialize the persister inputs.

//Initialize variable list
persist.IN.WorkingVariableList[0]:=	'gGlobalVariable';
persist.IN.WorkingVariableList[1]:=	'MyProgram:myLocalVariable';

//Initialize storage
persist.IN.pPersistentVariable:=	ADR(persistentData);
persist.IN.sizeofPersistentVariable:=	SIZEOF(persistentData);

//Get data valid
persist.IN.pDataValid:=			ADR(gDataValid);

Then run the initialization function. This will copy the stored data to the monitored variables if the value at pDataValid is TRUE.

PersistFn_Init(persist);

Operation

The cyclic function needs to be placed in your cyclic code. This function will gather the information from the watched variables and store the data to the array defined in pPersistentVariable.

PersistFn_Cyclic(persist);

Errors

  • 0 - OK
  • 50000 - Invalid input parameters.
  • 50001 - Out of memory. The permanent memory location is smaller than the size of the monitored variables.
  • 50003 - Data moved or changed from previously stored location.