SITRAIN Training For: Automation and Industrial Solutions
SITRAIN Training For: Automation and Industrial Solutions
SITRAIN Training For: Automation and Industrial Solutions
C data types The data types in the C programming language are defined differently.
Some of these definitions originate from the ANSI C programming language:
e.g. char, short, long, etc.
Other data types are additionally defined in WinCC. These are, however, only alias
names for the actual data types.
Examples: BOOL, SHORT, UINT, etc.
Table In the table above, the assignment of WinCC tag types and the C tag types along
with their ranges of values can be seen.
In the example on the left (1), simply the color value for red is returned if the speed
is higher than 1300. If this is not the case, the program continues with the next
line.
In the example (3), two if instructions are nested. If several instructions need to be
executed in the TRUE branch or FALSE branch, this instruction block must be
placed in curly brackets (see else branch).
To use this, the object to be dynamized must first be selected. "C Action" can then
be selected in the shortcut menu. In this way the C editor can be started.
Every program begins with "{" and ends with "}". Within the two outer curly
brackets, the required C program code can be inserted.
Comments are automatically shown in green. The comments shown above are
inserted automatically in every new C script. Their significance is explained later.
Event If a C action is configured for an event, the stored action is represented by a green
lightning symbol.
If the C action has not yet been compiled, the lightning symbol is shown yellow.
Compiling Once a program code has been entered, this needs to be compiled. If you attempt
to close the editor without having compiled you will automatically be asked
about it.
An uncompiled C script is ignored in runtime.
Trigger If the C script is linked to a mouse click event (Events tab), no trigger can be
defined. A trigger can only be defined for cyclic C scripts (in the Properties tab).
With a cyclic C script, for example more complex color changes can be made.
Browser With the tag or picture name browser, it is possible to search for a name and to
adopt this text in the program. This helps to avoid typing errors.
For a binary tag, there are the commands GetTagBit() and SetTagBit(). With the
read command only the tag name is transferred as the parameter. As the return
value, the current value of this tag is returned.
When writing, two parameters are required: the tag name and the value to be
written.
To do this, there are numerous corresponding C functions for reading out the
current status (e.g. GetBackColor()) or for changing the status (e.g. Set
GetBackColor()).
Apart from these very concrete functions, there are also functions that are kept
general that can also be used for reading out and changing:
SetPropertyBOOL(), SetPropertyWord(),
bCycle: acycle => acyclic, in other words not cyclic, for example mouse click
Copy / Paste As in most editors it is also possible to work here with Copy and Paste.
Calling C functions
To avoid typing errors e.g. with WinCC tag names (these are case sensitive!), an
aid can be used.
1. To do this, first search for the C function in the tree structure. The function
GetTagSWord is located for example in Internal functions/tag/get.
3. So that the tag to be read can be read with the tag browser, first click on the "..."
button and then on "Tag selection".
4. Here, every WinCC tag can be selected. Then close the screens with OK.
5. As the result, you will see the function with the necessary parameters.
Exercise 1. Add two buttons with the texts "+100" and "-100"
2. For the button "+100" edit a C script for event/mouse click as shown in the
picture above. Attempt to type in as little text as possible yourself, but rather use
the parameter dialog (see previous page).
5. Copy the "+100" button and adapt the labeling and the C script so that the "-
100" function is executed.
2. Add the C script above to the object properties in the Properties tab under
Colors/ Background Color.
3. Start Runtime and test the functionality. When doing this, note
the output of the printf() commands in the diagnostics window.
How often are these output?
5. Change the trigger to tag trigger with the tag "S7-Program/User.Speed_set" and
a cycle of 1 second.
How often are the printf() commands output now?
Note The configuration rules are taken into account by the Dynamic Wizards when
the scripts are generated.
When creating C scripts, a comment allows the configuration rule
to be recognized.
You will find further information in the online help of the WinCC Information
system, Index tab, keyword: Cross Reference
.
The configuration rules can also be generated with the WinCC Cross Reference
Assistant. The WinCC Cross Reference Assistant is a tool that searches for scripts
according to picture names and tags and it extends the scripts so that the WinCC
component Cross Reference finds the picture names and tags and enters them in
the cross reference list.
Note The configuration rules are taken into account by the Dynamic Wizards when
the scripts are generated.
When creating C scripts, a comment allows the configuration rule
to be recognized.
You will find further information in the online help of the WinCC Information
system, Index tab, keyword: Cross Reference
.
The configuration rules can also be generated with the WinCC Cross Reference
Assistant. The WinCC Cross Reference Assistant is a tool that searches for scripts
according to picture names and tags and it extends the scripts so that the WinCC
component Cross Reference finds the picture names and tags and enters them in
the cross reference list.
Application The Visual Basic programming language can not only be used for Visual Basic.
The Visual Basic for Applications (VBA) in Microsoft Excel, Microsoft Access
and in many other Windows applications uses the same language. VBA was
developed to automate software applications.
The Visual Basic programming system Scripting edition (VBScript) for
programming the Internet.
VBA and VBS are a subset of the Visual Basic language.
VB in WinCC In WinCC both VBS and VBA are used. For VBS, a separate editor was
developed.
In contrast to VBA, VB scripts only run in WinCC Runtime and allow you access to
graphics objects and tags there. Objects and pictures can be neither created nor
modified on a permanent basis in VBS, in contrast to VBA.
The main language-related differences between VBA and VBS are e.g.:
+ VBS was developed for use in the Internet, VBA for the automation of
software applications
+ The data type of VBS tags is always VARIANT. VBA on the other hand
distinguishes the individual data types such as INT, DOUBLE, STRING etc.
+ Certain language constructs from VBA have been removed from or added to
VBS.
+ Errors are handled differently in VBS compared to VBA.
Although an editor for macros was only implemented in the WinCC editor Graphics
Designer, the possibilities extend way beyond the possibilities of the Graphics
Designer, for example in the VBA scripts, new tags can also be created in the
WinCC project.
In VBA, it is also possible to create your own menus in the Graphics Designer.
Unfortunately this topic is not consolidated in the system course, only in the
extended course.
VBS The VBS scripts are based on the Microsoft Scripting host that is installed in every
Windows 2000, Windows XP and Windows ME system.
e.g. configuring the setting of setpoints for tags with the manipulation of a graphics
object. Making the switchover of the runtime language dependent on the
manipulation of a graphics object. Configuring changes of color, e.g. cyclically
(flashing) or displaying statuses (motor on). Transferring data to other applications
(e.g. Excel, Word, Access). Starting external applications from WinCC etc.
Objects With the objects and lists you have access to all the objects in the graphic runtime
systems: graphics objects, pictures, layers and tags.
Properties The properties of the individual objects can be used to modify specific graphics
objects and tags at runtime, for example enabling an operating element with a
mouse click or triggering a color change when a tag value changes.
Methods Methods that are applied to individual objects can be used, for example, to read
tag values for further processing or display diagnostics messages at runtime.
e.g.
+ HMIRuntime.Stop
+ HMIRuntime.Trace "Customized error message"
+ Expression.Write [Value]
+ Expression.Read
+ etc.
Examples of VBS Apart from examples directly for WinCC such as reading and writing tags, here
you will find further examples:
+ Access to MS Excel
+ Access to any SQL database
+ etc.
In the example on the left, simply the color value for red is returned if the speed is
higher than 1300. If this is not the case, the color value green is returned.
To use this, the object to be dynamized must first be selected. "VBS Action" can
then be selected in the shortcut menu. In this way the VBS editor can be started.
Every program must be inserted between the first line (Sub ...) and End Sub.
Comments begin with a single quote at the start of the line and are automatically
displayed green.
Event If a VBS action is configured for an event or a property, the stored action is
represented by a blue lightning symbol.
Syntax check Here, the syntax can be checked manually. When the editor is closed with OK, this
check is made automatically. Only if an error is detected, is it reported and the
editor remains open. This means that no scripts with errors can be saved.
If an error cannot be corrected immediately, temporary commenting out of the
problem area is recommended.
Trigger If the VB script is linked to a mouse click event (Events tab), no trigger can be
defined. A trigger can only be defined for cyclic VB scripts (in the Properties tab).
With a cyclic VB script, for example more complex color changes can be made.
Browser With the tag or picture name browser, it is possible to search for a name and to
adopt this text in the program. This helps to avoid typing errors.
The extended tag browser will be explained later.
In the top example a tag is declared with Dim Speed. With the following line, this
is referenced with an object and therefore linked to a concrete WinCC tag.
Afterwards, all properties and methods (Read and Write) related to this WinCC tag
in other words with Speed.Read the current value of the WinCC tag is read in and
stored in the Speed.Value property.
After the calculations, the value in the Speed.Value property is written to the
WinCC tag with Speed.Write.
Character strings and also tags are simply linked by a &. The constant vbCrLf
stands for Carriage Return and Line Feed and produces a new line.
This Trace command can, for example, be used to output interim results of an
extensive calculation or to check whether a certain branch in an IF-Then-Else
structure is run through.
Runtime errors, on the other hand, are only detected during runtime.
Example: The name of a tag has been written incorrectly.
In the example above, a runtime error is found and reported in the diagnostics
window. The precise location of the problem is shown:
Picture: Script.pdl_Events => the picture name is script.pdl, Events means that the
problem is with the events (e.g. mouse click). Script.pdl_Triggers would mean that
the problem is at the properties and (cyclic scripts).
Caution If a VB script is stopped, other VB scripts are not executed but entered in a queue.
Other browsers The other browsers can be used to adopt picture names and graphics object
names in the source code free of errors.
Exercise 1. Add two buttons with the texts "+20%" and "-20%"
2. For the button "+20%" edit a C script for event/mouse click as shown in the
picture above. Attempt to type in as little text as possible yourself, but rather use
the aids provided by the VBS editor (see previous pages).
3. The "Global Script - Diagnose" window still exists from the previous exercise
and can also be used for the VB scripts.
5. Copy the "+20%" button and adapt the labeling and the VB script so that the "-
20%" function is executed.
2. Add the VB script above to the object properties in the Properties tab under
Colors/ Background Color.
Use the tag trigger.
OPTIONAL
4. Start the debugger and then set a breakpoint in the line "Speed.Read". Then
have the script executed line by line to check which branch of the if loop is run
through.
Configuration
VBS actions can be used where the direct connection to individual tags is not
adequate or several properties need to be changed at the same time.
With the aid of the VBS actions, the user has complete access to all available
object properties and tag contents from WinCC.
Function procedures
To group repeating or often used functions, procedures can be used. Functions
normally have a return value.
Sub procedures
A sub procedure is a code block that is executed as a reaction to an event and
does not supply a return value.
ok=SetTagMultiWait("%d%d%f",
Setpoint1", 16,
Setpoint2", 500,
Setpoint3", 55.4711);
Note When accessing the I/Os a considerably higher reaction time must be expected.
Remedy: Addressing via memory bits or data blocks.