Persistent Variables

Normally, variable values are kept in RAM, which means that their value will be lost whenever VTScada is stopped such as in the event of a power failure. This is usually not a problem for most values since they will be read in automatically from the plant I/O devices when the program is restarted. However, certain values such as process setpoints must be maintained, regardless of any interruption. The use of persistent variables solves this problem.

Persistent variables (sometimes referred to as "static" or "persisted" variables) function identically to other variables except that a copy of their value is kept on disk. The file name matches the module name in which the variable is declared, with a .VAL extension. When VTScada is restarted, the persistent values will be read from the .VAL file(s) thereby maintaining the values across restarts.

To define a variable as persistent, add the keyword "Persistent" in the definition:

[
  Persistent LastOKColor     { Last selected OK color              };
  Persistent LastErrorColor  { Last selected Error color           };

]

For arrays, all elements of the array become persistent values if the array itself is defined to be persistent. But, note that if you have an array (A=New[10];), and one element changes (A[0] = 1;), the modified value will not be written to disk until the module stops, as "A" itself did not change, but rather its element did. In the event of an unexpected shutdown, array elements will not persist.

Also, If A is to be a persistent array, then when defining A as an array with the New function, use the following expression:

A = PickValid(A, New(10);

This allows the persisted values from the last run of the module to be used rather than defining A as a new, empty array.

Use persisted values with caution. Each change in value must be written to disk rather than just to RAM and therefore requires a significantly greater length of time. They should be used only for values that are updated infrequently, such as operator-entered setpoints.

It does not require any additional time to reference or use the value in a persistent variable since a copy of its value is also kept in RAM.

Persistent variables are automatically considered to be shared as well. (See following topic, Shared Variables.)

Any data values supported by Pack may be saved to disk (e.g. arrays, streams, link lists, etc.). Recompiling a module does not delete the persistent variables.

Examples of persistent variables

  1. Create a new script application. (Workspace for Script Applications)
  2. Edit the Graphics module within AppRoot.SRC as follows:
Graphics
[
  Persistent X                  { a persisted variable };
]
Main [
  ZText(100, 100, "Enter a setpoint:", 5, 0);
  ZText(100, 120, "(Press <enter> after typing)", 5, 0);
  WinEditCtrl(220, 105, 280, 85, 0, X, 1); 
]
{ End of System\Graphics }
>
  1. Compile and run the run the application.
  2. Type something into the edit field. Be sure to press enter or tab after typing.
  3. Stop the application, then restart. The edit field should show the value you entered earlier.