Tag States

As with all VTScada modules, the work is done with state code.

While it is common practice to name a module's initialization state, "Init" and the main state "Main", you should instead use distinctive names for these states in your tags. It will be easier to for you to debug your code if the state names reflect the tag modules they are a part of.

Initialization State

The first state in a module will always run automatically. In the case of a tag, this state has three mandatory tasks:

  1. Set the variable Root to "Self".
  2. Launch the Refresh module
  3. Transfer control to the main module.

Other initialization tasks may be required depending on the tag.

CustomTypeInit [
  If 1 CustomTypeMain;
  [
    CriticalSection(
       { Set up initial values }
      Root = Self();
      Refresh();
    );
  ]
]

Note that these mandatory tasks must be enclosed in a CriticalSection.

Main State

A tag's Main state may be complex, depending on the tag's function. Common tasks include updating the tag's value and other variables. The following partial example is taken from the Analog Status tag.

AnalogInMain [
  SitePoint = Scope(Root, DeviceTag);
  Style         = PickValid(Scope(Root, StyleTag), Variable(\#SYSTEM_STYLE));

  LowScaleValue  = PickValid(RangeMin, ScaledMin);
  HighScaleValue = PickValid(RangeMax, ScaledMax);
]

Other tasks include monitoring the tag's link to the I/O device driver, calculating scale, updating the value in an OPC server, etc. In later topics within this chapter, you will see code to develop this state further.