SITRAIN Training For: Automation and Industrial Solutions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

Contents Page

Training aims ....................................................................................................................................... 3


General information about programming languages .......................................................................... 4
Data types in WinCC and in C ......................................................................................................... 5
Important operators ........................................................................................................................ 6
if statement .................................................................................................................................... 7
Opening the C editor in WinCC .......................................................................................................... 8
C editor in the WinCC toolbar ............................................................................................................. 9
Reading and writing WinCC tags ............................................................................................. 10
Dynamizing graphics objects with C ................................................................................................... 11
Diagnostics options for C scripts ................................................................................................... 12
Diagnostics options for C scripts Error message in the diagnostics window ..................................... 13
Tips for writing C scripts ..................................................................................................................... 14
Exercise 1: C script on mouse click with diagnostics ......................................................................... 15
Exercise 2: Cyclic C script ........................................................................................................... 16
C scripts and cross reference ........................................................................................................... 17
C scripts and cross reference ........................................................................................................... 18
Visual Basic and WinCC .................................................................................................................. 19
VBS, VBA data types ...................................................................................................................... 20
VBA in WinCC ................................................................................................................................. 21
Comparison of VBA and VBS .................................................................................................... 22
Where do I find help on VBS?............................................................................................................... 23
Important operators in VBS ............................................................................................................. 24
If statement .................................................................................................................................... 25
Opening the VBS editor in WinCC ...................................................................................................... 26
VBS editor in the WinCC toolbar .......................................................................................................... 27

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 1 Scripts
Contents Page
Reading and writing WinCC tags .................................................................................................... 28
Dynamizing graphics objects with VBS ............................................................................................... 29
Diagnostics options for VB scripts Trace .......................................................................................... 30
Diagnostics options for VB scripts Error message in the diagnostics window .................................... 31
Diagnostics options for VB scripts Debugger ................................................................................... 32
Tips for writing VB scripts 1/4 IntelliSense ....................................................................................... 33
Tips for writing VB scripts 2/4 Ctrl + spacebar .................................................................................. 34
Tips for writing VB scripts 3/4 Extended tag browser ...................................................................... 35
Tips for writing VB scripts 4/4 Code templates .................................................................................. 36
Exercise 3: VB script on mouse click with diagnostics ....................................................................... 37
Exercise 4: Cyclic VB script ............................................................................................................... 38
Configuration rules in VBS actions ..................................................................................................... 39
Examples with VBS actions .............................................................................................................. 40
Configuration notes Target: Good performance ................................................................................ 41

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 2 Scripts
The participant will:
Get to know selected operators and commands of C
Be able to handle dynamization tasks with C
Know options for homing in on errors

Get to know selected operators and commands of VBS


Be able to handle dynamization tasks with VBS
Know options for homing in on errors

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 3 Scripts
Historical At the start of the 70s C was developed by Dennis M. Ritchie at the Bell
Laboratories.
This development is closely associated with the development of the UNIX
operating system with over 95% of it written in C.
Over the course of time, C has developed into a separate programming language
independent of the operating system.
In 1988 the ANSI committee X3J11 published a language description for C and in
December 1989 it was standardized by ANSI.
Advantages C combines the power of an assembler language with the convenience of a
high-level programming language:
- Contains the memory properties and constructs of other
programming languages such as block structures, pointers etc.
- Programming directly for the hardware is possible
- Suitable for time-critical applications
- Suitable for system programming
- Universally usable language
- Small language
- Functionally simple to expand

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 4 Scripts
WinCC The data types for a WinCC tag are specified in the tag management.
See the figure top right

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 5 Scripts
Note The list of operators is not complete. Nor is complete help on ANSI C part of the
WinCC documentation.

For beginners, it is therefore recommended that they purchase a specialist book


on ANSI C.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 6 Scripts
Conditional statement
With the if statement an instruction can be executed depending on a condition.

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 (2) an if-else if-else instruction is shown.

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).

Global color scheme


If the color of an object needs to be changed statically or dynamically, this works
only if the property "Global color scheme" is turned off. Otherwise the global
settings of the colors are used.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 7 Scripts
C scripts If a task, for example, cannot be solved with a simpler dynamization option (e.g.
Dynamic dialog or direct connection), a C script or a VB script makes sense.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 8 Scripts
Toolbar The toolbar provides other options apart from the known options such as "Copy".

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 9 Scripts
Reading/writing WinCC tags
A typical task is to read or write one or more WinCC tags.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 10 Scripts
Changing graphics objects in runtime
Another typical task is to change graphics objects depending on process tags. In
WinCC almost all properties can be dynamized: e.g. background colors, line color,
X position, visibility, size, fill level, ...

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()).

For further examples see above.

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(),

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 11 Scripts
Troubleshooting in C scripts
For testing C scripts, WinCC provides an
Application window -> Global Script-> GSC Diagnostics This is inserted in a
picture. As of this point in time, all printf functions from all C scripts are output in
the application window.
The printf command is part of the ANSI C library and has the following syntax e.g.
printf( \r\n Value1: %d, temp);
%d is a placeholder for integral values, this is replaced during runtime by the
current value of temp.

This makes it simple to troubleshoot.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 12 Scripts
Error message Syntactical errors are automatically reported by the C editor in the Graphics
Designer.
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:

szApplicationname: PDLRuntimeSystem => corresponds to Graphics Runtime

bCycle: acycle => acyclic, in other words not cyclic, for example mouse click

lpszPictureName: START.WORK_PIC:SCRIPT => In the Start.pdl there is a


picture window WORK_PIC and there the picture SCRIPT.PDL is shown in which
an error has occurred.

lpszObjectName: Button16 => The graphics object involved is "Button13"

szErrorText: Tag not found => tag was not found

szTagName: "S7 program(1)/User.Speed_set999" => Name of the tag

Tip When the picture has been opened, you can


search for the object directly at the top left:

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 13 Scripts
Effective writing of C scripts

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.

2. By double-clicking on the function name GetTagSWord the parameter


assignment dialog opens. The function above requires only one parameter: the tag
name.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 14 Scripts
Objective Two new buttons need to be added to the Motor picture to adjust the setpoint
speed: "+100" and "-100". The calculation is made with a C action. For
diagnostics, the "printf" command is used.

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).

3. Add a "Global Script - Diagnose" window:


Go to Smart Objects and select the Application Window object. After you have
placed the object in a suitable size in the Motor picture, a box appears
automatically in which you select "Global Script". A further box then appears in
which you select "GSC - Diagnose". Finally, go to the properties to the
Miscellaneous group and there set all the properties (Sizeable, Movable, Border
etc.) to Yes. As a result a toolbar is shown in runtime.

4. Test the function in runtime.

5. Copy the "+100" button and adapt the labeling and the C script so that the "-
100" function is executed.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 15 Scripts
Objective
A speed indicator is to be added to the Motor picture. This should be green in the
normal situation, with high speeds (higher than 1300 revolutions per minute) it
should be red. As an exercise, this simple task will be solved with a C script.

Exercise 1. Add a circle to the Motor.pdl picture.

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?

4. Change the trigger to 2 seconds.


How often are the printf() commands output now?

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?

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 16 Scripts
Tags in actions
Tags in actions can only be found and replaced by the Cross Reference if the
predefined configuration rules for tags and picture names are kept to in the C
scripts.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 17 Scripts
Tags in actions
Tags in actions can only be found and replaced by the Cross Reference if the
predefined configuration rules for tags and picture names are kept to in the C
scripts.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 18 Scripts
Introduction What is Visual Basic? The "Visual" part of the name refers to the method for
creating graphical user interfaces. Instead of writing innumerable program lines
that specify the appearance and the position of user interface elements, simply
move preassembled objects to the required location by dragging them. The "Basic"
part of the name refers to the language BASIC (Beginners All-Purpose Symbolic
Instruction Code). Visual Basic was developed from the original BASIC and today
contains several hundred instructions, functions and keywords many of which
relate directly to the Windows user interface. By learning a few keywords,
beginners can create practical applications. At the same time due to its
performance, the language allows professionals to implement everything that can
also be developed with all other Windows programming languages.

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.

For VBA scripts, the Microsoft Visual Basic Editor is used.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 19 Scripts
VBS and VBA in the WinCC environment
VBA is used in WinCC Configuration during the configuration in order to adapt the
Graphics Designer to your individual requirements and to simplify and automate
configuration. VBA programs only run in the configuration environment of WinCC.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 20 Scripts
VBA You can use VBA to expand the functionality of the Graphics Designer and to
automate configuration. A VBA editor is available in the Graphics Designer which
you can use to automate the configuration of pictures - and other tasks. The VBA
editor is identical to the products from the Microsoft Office family.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 21 Scripts
VBA VBA provides the option of automating engineering tasks in the WinCC Graphics
Designer e.g. creating user-defined menus and toolbars. Creating and processing
standard objects, smart objects and Windows objects. Accessing products that
support VBA. Configuring actions for pictures and objects etc.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 22 Scripts
Object model The WinCC object model of the graphic runtime system allows, for example,
access to graphics objects and tags in runtime.
The object model is described in the WinCC help.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 23 Scripts
Note The list of operators above is not complete. You will find further information in the
WinCC help.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 24 Scripts
Conditional statement
With the if statement an instruction can be executed depending on a condition.

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.

The right-hand example shows an if-elseif-else statement. If the first condition is


met, the color becomes red. Only if this is not the case is the second condition
queried. This then decides about the color yellow or green.

Global color scheme


If the color of an object needs to be changed statically or dynamically, this works
only if the property "Global color scheme" is turned off. Otherwise the global
settings of the colors are used.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 25 Scripts
VB Scripts If a task, for example, cannot be solved with a simpler dynamization option (e.g.
Dynamic dialog or direct connection), a C script or a VB script makes sense.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 26 Scripts
Toolbar The toolbar provides other options apart from the known options such as "Copy".

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 27 Scripts
Reading/writing WinCC tags
A typical task is to read or write one or more WinCC tags.

Above, two options are shown.

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.

The bottom example shows simple writing.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 28 Scripts
Changing graphics objects in runtime
Another typical task is to change graphics objects depending on process tags. In
WinCC almost all properties can be dynamized: e.g. background colors, line color,
X position, visibility, size, fill level, ...

To do this, in VBS an object reference to the required graphics object is created


first. Following this, it is very easy to access all properties e.g. with
oPoly.Backcolor.
For further examples see above.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 29 Scripts
Trace Similar to the printf() command in C scripts, a similar command can also be used
in VBS. HMIRuntime.Trace
The trace output is made in the same object as with the C scripts (Global Script -
Diagnose).

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 30 Scripts
Error message Syntactical errors are automatically reported by the VBS editor in the Graphics
Designer.

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:

Application: PDLRT => corresponds to Graphics Runtime

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).

Function: Sub Button14_OnLButtonDown => The first part of the name


corresponds to the name of the object in the WinCC picture (here Button_14), the
second part shows the event (OnLButtonDown corresponds to press mouse
click/left).

Line: 3 => line in the VBScript


Error : => brief error description

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 31 Scripts
Microsoft debugger
You can use the "Microsoft Visual Studio 2008" debugger to test your VB scripts in
Runtime.
The debugger is installed during setup of the SQL Server.
It provides options such as the setting of breakpoints and the line by line execution
of VB scripts.
The debugger can be started manually at any time. As an alternative it can also be
started automatically if an error occurs (this does, however, need to be enabled in
the WinCC Explorer in the properties of the computer).
Logical errors are best detected with a debugger.
Example: The condition of an if instruction was incorrectly formulated.
Procedure Start the debugger e.g. manually. Then create a new project in the Visual Studio.
In the menu "Debug/ Attach to Process..." connect either pdlrt.exe (Graphics
Runtime) or gscrt.exe to the required process (with global script actions).
In the Solution Explorer (see above, there are two threads under PDLRT:
Script.pdl_Triggers (here, cyclic VB scripts are executed, e.g. a more complex
color change) and Script.pdl_Events (here the events of the picture Script.pdl are
processed, e.g. a mouse click on a graphics object). By double clicking on one of
these lines, the source codes of all VB scripts of this picture are displayed. In the
example above, a breakpoint (red point at the left margin) was set in the procedure
Circle3_BackColor_Trigger (Circle3 = name of the graphics object, BackColor =
name property, Trigger = cyclic VB script). Via the menu Debug/Step Over or with
F10 the script can be executed line by line. The line with the yellow background
will execute with the next F10 command.

Caution If a VB script is stopped, other VB scripts are not executed but entered in a queue.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 32 Scripts
IntelliSense If an object is given a period, a list with the properties and methods of this object
opens automatically. This is therefore an aid to completing a source code.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 33 Scripts
As can be seen from the example above, it is possible to create a source code
quickly and without typing errors. Further tips follow on the next pages.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 34 Scripts
Tag browser This can insert the tag name in a source code.

Extended tag browser


This dialog adds two lines to the source code. In the first it declares a tag and in
the second it links this tag with an object reference to a WinCC tag.

Other browsers The other browsers can be used to adopt picture names and graphics object
names in the source code free of errors.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 35 Scripts
Code template By double-clicking on one of the code templates, the "framework" of this
construction is inserted. This then needs to be filled in manually or adapted.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 36 Scripts
Objective Two further buttons need to be added to the Motor picture to adjust the setpoint
speed: "+ 20%" and "- 20%". The calculation is made this time with a VB script.
For diagnostics, the "HMIRuntime.Trace" command is used.

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.

4. Test the function in runtime.

5. Copy the "+20%" button and adapt the labeling and the VB script so that the "-
20%" function is executed.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 37 Scripts
Objective
A second speed indicator is to be added to the Motor picture. However this time,
the solution will be achieved with a VB script. This is green in the normal situation,
with high speeds (higher than 1300 revolutions per minute) it should be red.

Exercise 1. Add a circle to the Motor.pdl picture.

2. Add the VB script above to the object properties in the Properties tab under
Colors/ Background Color.
Use the tag trigger.

3. Start Runtime and test the functionality.

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.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 38 Scripts
Tags in VBS actions
The CrossReference from WinCC can be used to quickly find all the places of use
of tags and picture names, even in VBS actions. Tags in actions in the Graphics
Designer can be "linked" using CrossReference, i.e. replaced by other tags at all
or selected points.
Configuration rules for VBS scripts
All tags that you address with the standard formulation
HMIRuntime.Tags("tag name")
are automatically detected by the CrossReference of WinCC and then listed in the
picture properties.
If tags are addressed with different formulations in your code, you can make this
known by the following section of the CrossReference:
' WINCC:TAGNAME_SECTION_START
Const TagNameInAction = "TagName"
' WINCC:TAGNAME_SECTION_END
Note Between the single quote WINCC, there must be a space!
All the pictures which are addressed with the standard formulation
HMIRuntime.BaseScreenName("Screenname")
are automatically detected by the CrossReference of WinCC and then listed in the
picture properties.
If pictures are addressed with different formulations in your code, you can make
this known by the following section of the CrossReference:
' WINCC:SCREENNAME_SECTION_START
Const ScreenNameInAction = "ScreenName"
' WINCC:SCREENNAME_SECTION_END
Note Picture names must be written without the file extension ".PDL".

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 39 Scripts
VBS actions
With VBS actions, very extensive actions, checks and queries can be processed.

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.

Local script variables


Local script variables must be defined by the Dim, Private or Public statement and
only apply within the VBS action.

Picture-specific script variables


In the declaration area of the action window, picture-specific variables can be
declared.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 40 Scripts
Writing multiple tags
One SetTagMultiWait() for multiple tags is better than many individual
SetTagxxx().
The values of several tags are set in one write job. The function is ended after the
AS has reported back acceptance of the value.

Example BOOL ok;

ok=SetTagMultiWait("%d%d%f",
Setpoint1", 16,
Setpoint2", 500,
Setpoint3", 55.4711);

With the format descriptors, the following type is expected:


%d = DWORD / Int
%f = double
%s = char*

Note When accessing the I/Os a considerably higher reaction time must be expected.
Remedy: Addressing via memory bits or data blocks.

SITRAIN Training for ST-BWINCCS


Automation and Industrial Solutions Page 41 Scripts

You might also like