Program Tag

Help Contents

Introduction

The Program Tag runs an IEC 61131-3 Program.

Note: The Program Tag runs the IEC 61131-3 Code using a continuous execution model. For more information see the Interval and Offset below.

Configuration Properties

The Program Tag supports the configuration properties of the Core Tag plus these properties:

Property Type Version Description
InstructionLimit UDINT 2.0 The maximum number of instructions before the program is terminated. The intention of the InstructionLimit property is to prevent accidental infinite loops.
EnableTrackingEvents BOOL 3.77 Whether tracking events are generated from this program:
  • False - No tracking events are generated from this program.
  • True - The program can generate tracking events.
Interval TIME 2.0 The interval each time the code is run. See Interval and Offset for information on how Intervals and Offsets define the time of activities.
Offset TIME 2.0 The offset added to the Interval to get the actual time the code is run. See Interval and Offset for information on how Intervals and Offsets define the time of activities.

EnableTrackingEvents Property

When EnableTrackingEvents is True, the program can generate tracking events which are stored in the Event Log. Sources of tracking events include:

When EnableTrackingEvents is False, the program does not generate tracking events for any of the above sources.

A program with EnableTrackingEvents set to True can generate a large number of events. For example, this statement in a Program Tag with an Interval of 1 second would generate 1 tracking event per second:

%"Target Setpoint" := NewSetpointValue;

Most of the events would be duplicates, which makes the Event Log less useful.

If you want to track the actions of a Program Tag, and avoid duplicate events in the event log, use change detection:

VAR
    FirstRun : BOOL := TRUE;
    NewSetpoint, PreviousSetpoint : LREAL;
End_VAR

...
IF FirstRun THEN

    FirstRun := FALSE;
    %"Target Setpoint" := NewSetpoint;
    PreviousSetpoint := NewSetpoint;

ELSE IF NewSetpoint != PreviousSetpoint THEN

    %"Target Setpoint" := NewSetpoint;
    PreviousSetpoint := NewSetpoint;

END_IF

Interval and Offset Properties

Code in the Program Tag is run using a continuous execution model. This is similar to how a PLC runs code.

The Interval and Offset properties define how often the program code is run. In most applications the default values for Interval (T#1s) and Offset (T#0s) should be used.

If you want a program to time events, consider using a TON Function Block. For example:

PROGRAM MAIN
VAR
    EventSource AT %"Event Tag" : BOOL;
    EventDelay : TON;
END_VAR

    (* Run the TON Function block *)
    EventDelay( IN:=EventSource, PT:=T#10s );

    (* Has the timeout completed? *)
    IF EventDelay.Q THEN

        (* Code to run 10 seconds after EventSource transitions from FALSE to TRUE *)

    END_IF;
END_PROGRAM

Data Properties

PropertyTypeDescription
Quality INT The quality of the tag
Timestamp DATE_AND_TIME The timestamp of the last program execution.
ErrorMessage STRING The first compilation error. An empty string indicates no compilation errors.
ExecErrorCode DINT The result of the execution. A zero value shows success. A non-zero value shows an error occurred.
ExecErrorLocation STRING The location of any execution error. An empty string indicates there was no error.
ExecInstructionCount UDINT The number of instructions processed by the program during the latest execution.
ExecutionTimer TimerStatus The status of the program execution timer.

Tag Commands

The Program Tag supports these tag commands:

Tag Command Version Description
Run 2.0 Runs the program.

Relationships to Other Tag Types

The Program Tag builds on the features of these tag types:

Edit Program Code

To Edit the program code:

  1. Right click the Program tag in Tag Window.
  2. Select Edit source code.

In the code editor you can use any of the features in the IEC 61131-3 Common Elements to create code.

To access Fernhill SCADA from the program you can also use these additional function blocks:

Example Program

This example uses the TagWriteValue Function Block to reflect the value of one tag to another:

(* Note: Set the EventSeverityOperator of this Program tag to 0 (zero). 
    This avoids writing a large number of similar events to the event log. *)
Program Main
    Var 
        (* Function block to write a value to a tag. *)
        T : TagWriteValue;

        (* Flag set to true to start the update. *)
        StartUpdate : BOOL;
        
        (* Timestamps of the source tag used for change detection. *)
        PreviousTimestamp, NewTimestamp : DATE_AND_TIME; 
    End_Var

    (* Clear StartUpdate. StartUpdate is set to TRUE for one cycle if:
        * the TagWriteValue function block is not busy, and
        * The source tag timestamp is different to the last timestamp.
        When StartUpdate is set to TRUE, a the TagWriteValue function block will be triggered. *)
    StartUpdate := False;
    
    (* If the TagWriteValue function block is not busy... *)
    if Not( T.BUSY ) Then
    
        (* Get the timestamp of the source tag. *)
        NewTimestamp := %".Source.Timestamp";

        (* Has the timestamp of the source tag changed ? *)
        if NewTimestamp <> PreviousTimestamp Then
            
            (* We want to start the update. *)
            StartUpdate := True;
        End_If

    elseif T.DONE AND NOT T.ERROR Then
    
        (* The TagWriteValue function block has completed without error, 
            update the previous timestamp. *)
        PreviousTimestamp := NewTimestamp;
    End_If
    
    (* Remember to substitute an appropriate tag name. *)
    T( Req:=StartUpdate, TagName:="-- Full Name of target --", Value:=%".Source.Value" );
End_Program

Further Information

Interval and Offset

To learn how activities are scheduled using Interval and Offset.

IEC 61131-3 Program

To learn about Programs you can edit.

Core Data Driver

To learn about the Core Driver.

Glossary

For the meaning of terms used in Fernhill SCADA.