Control programming

The CTRL program of WinCC OA has a very similar syntax to C. It is therefore recommended to refer to the relevant manuals on this topic.

main()

Each script contains a function main(). When and how often this is executed depends on the execution mode. Unlike in C, main() does not return a value and can only contain arguments in some cases. E.g. when programming editable table cells or programming alerting actions as a part of an alert class.

Scripts for graphics elements differ in particular:

User input

If the execution of the script is linked, for example, to a mouse-click on a graphics object (equivalent for instance to the graphics attributes "when clicked" or "command"), then the script is executed whenever the object is clicked, but not when the panel is opened.

Syntax

main()

{

//...

}

Syntax in CTRL

A script consists of instructions and variable declarations. If there are more than one instruction, these are grouped into instruction blocks. Instructions are either functions or assignments (commands). Instructions are terminated with a semicolon (";").

Syntax of a script

[<Declaration of global variables>]

Function declaration

Variable declaration:

<Datatype> <Variablename 1 [,<Var 2>...];

Function declaration

[<Returntype>]Functionname([Parameter])<Instruction block>

Instruction block

{

[<Variable declaration>]

instruction1;

[instruction2;..]

}

Example

int g_number = 3; /* Script global variable (can be initialized at this point) */

void afterLogin(string user, string password, string newLocale)

// Function with arguments

{

string module = "WinCC OA"; // Declaration

string panel = "vision/wccoa"; // of variables

string locale = getLocale(getActiveLang());

...

if( newLocale == locale) // Control expression

{ // Start of the instruction block

ModuleOn(module,0,0,1000,600,0,0,"Scale"); // Instruction 1

RootPanelOnModule(panel,"",module,""); // Instruction 2

} // End of the instruction block

else

{

string exec = isMotif() ? "WCCOAui" : "WCCOAnv";

//Variable declaration with instruction and expression

...

}

ModuleOff(myModuleName());

} // End of the function

Syntax notation

The following rules apply to the syntax in the manual:

  • "|" separates alternatives,

  • "<" and ">" enclose terms that must be substituted in the script (for example, int, string etc. is substituted for <data type>)

  • "[" and "]" enclose optional sections

  • "{" and "}" enclose optional sections that can also appear more than once

  • ";" ends a rule

Return value/void

Functions with return value are declared as follows.

int add(int a, int b)

{

return a+b;

}

This declares add() as a function that takes an integer followed by another integer, and returns an integer. This instruction says nothing about what add() does with the integers. The return value is not specified until the definition.

In the Control script this reads as follows:

main()

{

int c;

c = add(5,6);

DebugN(c); // c is thus 11

}

If a function does not return anything its return value should be declared as void. This is the case for example for values that are passed as start values (for example, srand()) or for closing panels or dialog boxes, resp. if you use waiting control functions in the function.

  1. void srand(unsigned seed)

Recursive Function Call

Since Version 2.9 recursive function calls can be used insiede CTRL scripts.

Example

Following example shows a recursive function call.

The function recursionTst() is used inside itself and sends the counter value "cnt" to the logviewer. "cn" represents the count of calls for the function recursionTst().

main()

{

int i = 0;

recursionTst(i);

}

recursionTst(int cnt)

{

if(cnt <= 100)

{

cnt++;

DebugN("Call: " + cnt);

recursionTst(cnt); //The function calls itself => Recursion

}

else

{

DebugN("Done");

}

}