Blog

Getting Started with WinCC OA: Part 5 - Scripting Syntax

Getting Started with WinCC OA: Part 5 - Scripting Syntax

Welcome back to “Getting Started with WinCC OA”! If you aren’t familiar with the series’ previous topics, it’s recommended that you read the respective installments before proceeding:

In Part 5 of the “Getting Started with WinCC OA,” we’ll investigate the basics of OA’s scripting syntax. Once scripting syntax is understood, we can begin programming and adding functionality to our panels.

The Basics

  • Every executable statement must end with a semicolon;
  • // Use two forward slashes to comment out the remaining portion of a line
  • Ctrl + D will comment out a line of code
  • Ctrl + Shift + D will un-comment out a line of code
  • Ctrl + E will open all of a selected panel’s scripts

Variables

OA supports many types of variables. The most used of which are:

Type OA Name Array of <type> Comment
Any Type anytype dyn_anytype Adapts to the type of the first variable assigned to it
Boolean bool dyn_bool 0, 1, true, or false
Character char dyn_char  
Enumeration enum -----  
Floating Point float dyn_float  
Integer int dyn_int  
String string dyn_string  
Time time dyn_time  
Shape shape dyn_shape Pointer to a graphic element

Variable definitions take the form of:

<Type> <variableName>;

Variables can be given initial values:

<Type> <variableName> = <intialValue>;

Multiple variables can be defined for a single type:

<Type> <variable1Name>, <variable2Name>, …;

bool function

Variables must be defined before use, but can be so:

  • At the beginning of a script
  • Within a function
  • Within a statement
  • Within a function’s parameter structure

Best practice tip: Variable names should utilize camelCase or PascalCase and clearly indicate the variable’s function.

Casting
Casting is a way of interpreting a variable or value as an alternate type. This is done simply by preceding the target variable with “(<target type>)”. Casting is especially helpful with enumerations (which will be discussed later in this blog post).

Constants
Constants are defined in a manner similar to that of variables. However, by designating something as a constant, OA ensures that the value cannot be modified within a script. To designate a variable as a constant, simply place “const” before the type name.

Note: DMC’s best practice standards include capitalizing all constants.

const function

Functions
Functions take the following form:

<returnType> <functionName>(<parameterType> <parameter>,…)
	{
		//Function content
		return <returnedItem>;  //This is required unless your returnType is “void”
	}

function

Note: returnType takes the form of any variable type or is “void”

Logic Operators
The following operators can be used in WinCC OA scripting:

Operator Symbol
AND &&
OR ||
NOT !
Equal to ==
Greater than >
Greater than or equal to >=
Less than <
Less than or equal to <=
Not equal to !=

Statements
WinCC utilizes the traditional of IF, ELSE, FOR, WHILE, and SWITCH statements as follows:
IF and ELSE statements:

 if(<Boolean condition>)
{
    <code>;
}
else
{
    <code>;
}

if else statement

FOR Loop:

  for(<initialization of condition>; <Boolean testing condition>; <target variable modification>)
{
    <code>;
}

for loop

WHILE Loop:

while(<Boolean condition>)
{
    <code>;
}

while statement

SWITCH Statement

switch(<target variable>)
{
    case <value 1>:
        <code>
        break;
    •••
    case <value n>:
        <code>
        break;
}

switch function

Bonus Round: Ternary Statements
Ternary statements are condensed ways of writing simple if-else statements. They take the form:

<conditional statement> ? <value returned if true> : <value returned if false> 

Enumerations
Enumerations should be used to make code easier to interpret. For example, say there exists an integer variable, called iStatus, that indicates the condition of a motor where:

  • 0 = stopped
  • 1 = Error
  • 2 = Running Forward

Say we want the rectangle from our prior example (representing the motor) to change color based on the iStatus variable. A series of IF statements could be written:

if function

The program will work as expected, setting the rectangle’s background color to either grey, red, or white depending on the iStatus value. But someone troubleshooting or reviewing the code may be slowed as the code isn’t explicit as to what each iStatus case represents. This is where enumerations come in.

enum function

The enumeration “StatusType” has been implemented to make the code more explicit. Note that the enumeration has been cast as an integer to ensure that iStatus is given an integer to which it can compare.

Even further, constants have also been utilized for redundant clarity and (for reasons we’ll discuss later in this series) consistency.

What would even improve the code further? If you guessed “switch statement”, you’d be correct. While, in theory, this would be true, an unfortunate “feature” of WinCC OA is that its switch statements can’t handle casting. So while the following code would be optimal, saving and implementing it is not possible.

istatus

Now that the basics of scripting syntax have been covered, we’re ready to progress to the next aspect of scripting in WinCC OA: Para. In the next installment of “Getting Started with WinCC OA”, we’ll investigate the Para tool, OA tags, and how to interact with said tags.

Topics to look forward to in this series: 

Contact DMC to get started on your next WinCC project and learn more about our Siemens SIMATIC WinCC Programming as well as our WinCC Open Architecture Development.

Comments

There are currently no comments, be the first to post one.

Post a comment

Name (required)

Email (required)

CAPTCHA image
Enter the code shown above: