Flexible SCADA Help
Logic and events

Programs

Logic on the server: interlocks, calculations, sequences, counters. Programs are written in C# or in the IEC 61131-3 languages — ST (text) and FBD (block diagram) — and run by timer, on variable change, by condition or at start.

Create a program

Right-click a folder or the project → Create → Program → a language. An object type can have a type program: it runs separately for every object with its own fields, timers and counters; the object variables in code are $Name.

Program languages.
Program languages.
The program name is the class and file name: letters, digits and “_”, not starting with a digit.
The program name is the class and file name: letters, digits and “_”, not starting with a digit.
LanguageFileUsed for
C#.csany logic, calculations, strings, collections; can be opened and edited in VS Code
ST (IEC 61131-3).stthe Structured Text familiar to automation engineers; exchange with CODESYS and TwinCAT through PLCopen XML
FBD (IEC 61131-3).fbda function block diagram in the Flexible SCADA editor with live values on the connections
Block library (ST).st without PROGRAMyour own FUNCTION_BLOCKs and FUNCTIONs for all ST programs and FBD diagrams (the “Project library” palette)

Editor

A C# program: code on the left, on the right — state, when to run, settings, files; below — problems and the log.
A C# program: code on the left, on the right — state, when to run, settings, files; below — problems and the log.
PartWhat is there
Savesaves and rebuilds the program; errors — in the “Problems” tab with the line
Run oncecall Run() now — a check without waiting for a trigger
VS Codeopen in VS Code (with hints for the project variables through the Flexible SCADA extension)
Staterunning, stopped, error; the time of the last call
When to runenabling and triggers (see below)
Call timeout, mslonger — the call is aborted with an error in the log (protection against endless loops)
Logthe program Log / Warn / Error messages and run-time errors

When to run: triggers

TriggerSettingsWhen Run() is called
By timerevery N ms (default 1000)periodically
On variable changea list of variableswhen any of them changes
By condition (edge)an expression, for example SIM.P1.Fault > 0when the condition becomes true (rising edge)
On start—once at project start or when the program loads

There can be several triggers; Trigger in code tells which one fired (timer, variable, condition, start or manual).

C#

public class PumpLogic : ScriptBase
{
    TON delay = new();                       // IEC 61131-3 timers: TON, TOF, TP, R_TRIG…

    public override void Run()
    {
        bool fault = Tags["SIM.P1.Fault"] > 0;   // reading a variable
        // the fault holds for 5 s — start the standby pump (writing — by assignment)
        if (delay.Update(fault, TimeSpan.FromSeconds(5))) Tags["Substation.Standby.Start"] = 1;
        if (!Tags["SIM.PT1.P"].Good) Warn("No connection to the pressure sensor");
    }
}
In codeWhat it is
Tags["Channel.Tag"]a project variable: a number when read, written by assignment; .Text — a string, .Good — the quality
Tags[TagNames.Channel.Tag]the same through name constants — with hints and a check at build time
Tags["$Freq"]in a type program — a variable of its own object
Init()called once after loading — field preparation
Run()the main logic — by triggers
Log(…), Warn(…), Error(…)messages to the program log
Triggerwhat started the call: Trigger.Kind (Cyclic, Change, Condition, Startup, Manual), Trigger.Tag
class fieldskeep values between calls (counters, states)
TON, TOF, TP, R_TRIG, F_TRIG, CTU…IEC 61131-3 blocks as class fields: t.Update(IN, PT) returns Q, the PT time is a TimeSpan; or set IN, PT and call t.Call()

ST

An ST program.
An ST program.
PROGRAM PumpST
VAR
    Fault AT 'SIM.P1.Fault' : BOOL;      (* binding to a project variable *)
    Reserve AT 'Substation.Standby.Start' : BOOL;
    T1 : TON;
END_VAR
T1(IN := Fault, PT := T#5s);
Reserve := T1.Q;
END_PROGRAM

A project variable is declared as Name AT 'Channel.Tag' : TYPE; — read at the start of a call, written at the end. In a type program — Level AT '$Level' : REAL;. Block instances (TON1…) in FBD are declared by themselves.

FBD

An FBD diagram: blocks from the palette, wires, live values on the pins.
An FBD diagram: blocks from the palette, wires, live values on the pins.

Blocks are dragged from the palette on the left and their pins are connected with wires; the diagram inputs and outputs are project variables. Running, live values are visible on the wires. The diagram is stored as PLCopen XML and opens in CODESYS.

IEC 61131-3 function blocks

BlockWhat it does
TONOn-delay timer: Q = TRUE when IN stays TRUE for at least PT; ET — elapsed time
TOFOff-delay timer: Q = TRUE with IN and for PT more after it is removed
TPPulse: on an IN edge — Q = TRUE for exactly PT
R_TRIGRising edge: Q = TRUE for one call when CLK became TRUE
F_TRIGFalling edge: Q = TRUE for one call when CLK became FALSE
CTUUp counter: CU edge — CV + 1; R — reset; Q = CV >= PV
CTDDown counter: CD edge — CV - 1; LD — load PV; Q = CV <= 0
CTUDUp/down counter: CU — up, CD — down, R — reset, LD — load PV; QU = CV >= PV, QD = CV <= 0
SRSet-dominant flip-flop: Q1 = S1 OR (NOT R AND Q1)
RSReset-dominant flip-flop: Q1 = NOT R1 AND (S OR Q1)

Standard functions

FunctionCallWhat it does
ABSABS(IN : ANY_NUM) : ANY_NUMAbsolute value
SQRTSQRT(IN : ANY_REAL) : ANY_REALSquare root
LNLN(IN : ANY_REAL) : ANY_REALNatural logarithm
LOGLOG(IN : ANY_REAL) : ANY_REALDecimal logarithm
EXPEXP(IN : ANY_REAL) : ANY_REALExponent e^IN
SINSIN(IN : ANY_REAL) : ANY_REALSine (radians)
COSCOS(IN : ANY_REAL) : ANY_REALCosine (radians)
TANTAN(IN : ANY_REAL) : ANY_REALTangent (radians)
ASINASIN(IN : ANY_REAL) : ANY_REALArcsine
ACOSACOS(IN : ANY_REAL) : ANY_REALArccosine
ATANATAN(IN : ANY_REAL) : ANY_REALArctangent
ATAN2ATAN2(Y : ANY_REAL, X : ANY_REAL) : ANY_REALArctangent of Y/X with the quadrant
EXPTEXPT(IN1 : ANY_REAL, IN2 : ANY_NUM) : ANY_REALRaise to power IN1^IN2 (same as **)
TRUNCTRUNC(IN : ANY_REAL) : DINTDiscard the fractional part
MAXMAX(IN1, IN2, …) : ANY_ELEMENTARYLargest of the values
MINMIN(IN1, IN2, …) : ANY_ELEMENTARYSmallest of the values
LIMITLIMIT(MN, IN, MX) : ANY_ELEMENTARYIN, limited below by MN and above by MX
SELSEL(G : BOOL, IN0, IN1) : ANYG = FALSE — IN0, G = TRUE — IN1
MUXMUX(K : ANY_INT, IN0, IN1, …) : ANYSelect an input by number K (from 0)
MOVEMOVE(IN) : ANYValue of IN (assignment)
SHLSHL(IN : ANY_BIT, N : ANY_INT) : ANY_BITShift left by N bits
SHRSHR(IN : ANY_BIT, N : ANY_INT) : ANY_BITShift right by N bits
ROLROL(IN : ANY_BIT, N : ANY_INT) : ANY_BITRotate left
RORROR(IN : ANY_BIT, N : ANY_INT) : ANY_BITRotate right
LENLEN(IN : STRING) : INTString length
LEFTLEFT(IN : STRING, L : ANY_INT) : STRINGL characters from the left
RIGHTRIGHT(IN : STRING, L : ANY_INT) : STRINGL characters from the right
MIDMID(IN : STRING, L : ANY_INT, P : ANY_INT) : STRINGL characters from position P (from 1)
CONCATCONCAT(IN1 : STRING, IN2 : STRING, …) : STRINGConcatenate strings
INSERTINSERT(IN1 : STRING, IN2 : STRING, P : ANY_INT) : STRINGInsert IN2 into IN1 after position P
DELETEDELETE(IN : STRING, L : ANY_INT, P : ANY_INT) : STRINGDelete L characters from position P
REPLACEREPLACE(IN1 : STRING, IN2 : STRING, L : ANY_INT, P : ANY_INT) : STRINGReplace L characters from position P with IN2
FINDFIND(IN1 : STRING, IN2 : STRING) : INTPosition of IN2 in IN1 (from 1; 0 — none)
LOWER_BOUNDLOWER_BOUND(ARR, DIM : ANY_INT) : DINTLower bound of an array dimension
UPPER_BOUNDUPPER_BOUND(ARR, DIM : ANY_INT) : DINTUpper bound of an array dimension

Flexible SCADA functions (ST, FBD)

FunctionCallWhat it does
LOGLOG(MSG : STRING)message to the script log (with a STRING argument; with a number — decimal logarithm)
WARNWARN(MSG : STRING)warning to the script log
READ_REALREAD_REAL(NAME : STRING) : LREALvalue of a project variable by name (the name can be computed)
READ_BOOLREAD_BOOL(NAME : STRING) : BOOLvalue of a project variable as BOOL
READ_STRINGREAD_STRING(NAME : STRING) : STRINGvalue of a project variable as a string
TAG_GOODTAG_GOOD(NAME : STRING) : BOOLproject variable quality is good (connected, the value is reliable)
WRITE_TAGWRITE_TAG(NAME : STRING, VALUE : ANY)write a value to a project variable by name

Programs run on the server: one slow call does not disturb the others — every program has its own execution thread and timeout. An error in one program does not stop the others.