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.
- 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
OA supports many types of variables. The most used of which are:
||Array of <type>
||Adapts to the type of the first variable assigned to it
||0, 1, true, or false
||Pointer to a graphic element
Variable definitions take the form of:
Variables can be given initial values:
<Type> <variableName> = <intialValue>;
Multiple variables can be defined for a single type:
<Type> <variable1Name>, <variable2Name>, …;
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 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 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.
Functions take the following form:
<returnType> <functionName>(<parameterType> <parameter>,…)
return <returnedItem>; //This is required unless your returnType is “void”
Note: returnType takes the form of any variable type or is “void”
The following operators can be used in WinCC OA scripting:
|Greater than or equal to
|Less than or equal to
|Not equal to
WinCC utilizes the traditional of IF, ELSE, FOR, WHILE, and SWITCH statements as follows:
IF and ELSE statements:
for(<initialization of condition>; <Boolean testing condition>; <target variable modification>)
case <value 1>:
case <value n>:
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 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:
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.
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.
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.