×
Siemens Industry Online Support
Siemens AG
Entry type: FAQ, Entry ID: 57132412, Entry date: 08/22/2018
(18)
Rate

Tips and tricks for creating scripts in WinCC (TIA Portal)

  • Entry
  • Associated product(s)
Practical tips for programming scripts for WinCC Comfort and WinCC Advanced in the programming language VBScript.
General InformationWhy is a script, which is configured to occur in the event of a "change in value" of an internal tag, not executed?How do you access the individual elements of an array in a script?How do you efficiently copy process tag arrays backwards and forwards between a controller and a local script?How do you access dynamic libraries ("Dynamic Link Libraries", DLLs)?How do you determine the current mouse position by VBScript?Why can the "MsgBox" command not be used in a script?How do you assign values in hexadecimal notation to a tag in a script?How do you show or hide individual levels in a script?How do you link strings together in a script?How do you insert a wildcard character (space) between linked strings in a script?How do you configure return values and parameters for a script?How do you change the properties (color, length etc.) of an object in Runtime?What can cause the "Maximum nesting depth reached" message to be triggered?How do you determine the current screen number and screen name?How do you use multiplex tags in the script?How do you use HMI UDT tags in the script?How do you save tags in a script so that they are retained between two script calls?How do you change the content of a text field via a script?How do you read a user's password from the list of passwords or user view using a script?How do you use a script to end the WinCC Runtime and then shut down the PC or panel?How do you address objects from a template or screen via a script?How do you put the focus on a specific screen object in Runtime (an input field, for example)?


General Information

VBScripting can be used with the WinCC Comfort/Advanced Runtime.
Basic information and references for this are available

  • In the manual "SIMATIC STEP 7 Basic/Professional V15 and SIMATIC WinCC V15" (Entry ID: 109755202) under "Visualize Processes > Working with System Functions and Runtime Scripting". 
    Whether a script can be executed depends among other things on the Runtime station or operator panel used.  More information about device dependency is available in the manual mentioned above by going to "Visualize processes > Working with system functions and Runtime scripting > Reference".
  • In the SIMATIC WinCC WinCC Engineering V15 - Programming Reference (Entry ID: 109755216).
     

Why is a script, which is configured to occur in the event of a "change in value" of an internal tag, not executed?

If you use an internal tag to influence another internal tag, via which, for example, a script is executed (a change in value, for instance), then a safety mechanism prevents this function from being executed.
This safety function prevents scripts repeatedly calling each other in a loopback.

Example
In the Properties of the internal tag "HMI_Tag_01", a bit is set for the "Change in value" event in the internal tag "HMI_Tag_02".
At this second internal tag "HMI_Tag_02" a script is called through the Properties for the "Change in value" event.
Result: The script is not executed.

Remedy
Use an external tag which is linked to a controller. Please note that by using an external tag you circumvent the safety mechanism described above. This can lead to problems in Runtime.

How do you access the individual elements of an array in a script?

Access to separate array elements is made by means of the name of the array and the array index separated by brackets in this form: "SmartTags("Array Name")(array index) "

Example Access to the 2nd element of the array "DB10_HMI_Data"

Dim Tag_01    'Internal script tag
Tag_01 = SmartTags("DB10_HMI_Data")(2)

Note
The syntax is as follows for accessing elements of an array of string:

Dim Tag_01   'Internal script tag
Tag_01 = SmartTags("DB10_HMI_Data[2]")

Example Description of the first eleven elements of the array "DM10_HMI_Data" via a loop

Dim Tag_01
For Tag_01=0 to 10
    SmartTags("DB10_HMI_Data")(Tag_01) = 5
Next

Local script arrays (arrays that are available only in a script) can be selected in the same way. In the case of multi-dimensional local arrays, the individual dimensions of the array are separated by a comma.

Example Access to the two-dimensional array tag "HMI_Data" without controller link

Dim HMI_Data(10,10)
Dim Tag_01
Tag_01 = HMI_Data(0, 5)


Note
When using array elements with controller link, it is recommended to copy them to internal script tags at the beginning of a script and then continue working with these internal tags. Before ending the script, copy the internal tags back to the array elements.
  

How do you efficiently copy process tag arrays backwards and forwards between a controller and a local script?

To copy an array of process tags from the controller to a script you have to copy the array elements one by one, in a loop, for example. However, you can copy a local script array directly to a process tag array using a single instruction:

Example Copying from the controller array "CPU_Array" to the internal array "local_array" and back

Dim local_array(10)
Dim i
...
' Copy from the CPU to the script:
For i = 0 To 9
    local_array(i) = SmartTags("CPU_Array")(i)
Next
...
' Copy back to the CPU:
SmartTags("CPU_Array") = local_array


Note
Working with arrays can lead to high communication loads and data inconsistency. Refer here to the note in the WinCC (TIA Portal) system manual in Entry ID 109755202 under "Visualize Processes > Working with tags > Working with arrays > Array Basics".
 

How do you access dynamic libraries ("Dynamic Link Libraries", DLLs)?

WinCC Comfort and Advanced cannot access DLLs.
Functions from DLLs (DLL = Dynamic Link Library) can only be used in user-defined C functions in WinCC Professional.
For this refer to the SIMATIC STEP 7 Basic/Professional V15 and SIMATIC WinCC V15 system manual (Entry ID 109755202) under "Visualize processes > Working with System Functions and Runtime Scripting > Creating user-defined C functions > Calling functions from DLLs in user-defined C functions".
 

How do you determine the current mouse position by VBScript?

In order to read the current mouse position, you have to access the API (Application Programming Interface) of the operating system.
Access to the operating system's API by means of DLLs is not supported in VBScript.

Why can the "MsgBox" command not be used in a script?

The "MsgBox" command cannot be used in WinCC Comfort and WinCC Advanced for safety reasons. The display might "lay" itself over operating elements, for example, and thus prevent rapid operation of the plant. Furthermore, the message box might block the script and thus prevent further execution.

Remedy
Alternatively you can use the ShowSystemAlarm system function.
 

How do you assign values in hexadecimal notation to a tag in a script?

Put the character combination "&h" before a constant in the script editor to identify it as hexadecimal.

Example

Dim Tag_01
Tag_01 = &h45E     ' Corresponds to the decimal value "1118"
  

How do you show or hide individual levels in a script?

Working with levels is a function of the WinCC (TIA Portal) development system. The levels are configuration aids and can be shown and hidden only there.
This function is not available on the operator panel or on a PC Runtime system. Therefore you cannot show or hide the levels using a script.

Remedy
In the "Properties > Animation > Visibility" tab of the relevant object you show and hide an object depending on the value of the configured tag.
 

How do you link strings together in a script?

You use the linking operator "&" to link strings together. 

ExampleLinking together of the strings "Tag_Text_01", "Tag_Text_02" and "Tag_Text_03" to a new string "Tag_01"

Dim Tag_01, Tag_Text_01, Tag_Text_02, Tag_Text_03
Tag_01 = Tag_Text_01 & Tag_Text_02 & Tag_Text_03

Note
The VB function "CStr(Expression)" converts random (numerical) expressions into the "String" type. By doing this conversion before linking the character strings you can avoid errors in Runtime.
 

How do you insert a wildcard character (space) between linked strings in a script?

To insert any number of spaces between two texts you use a string constant with two quotation marks between which are enclosed the desired number of spaces.
A link with the next character is done again using the linking operator "&".

Example 1 Linking together of the strings "Tag_Text_01", "Tag_Text_02" and "Tag_Text_03" with enclosed spaces to the new string "Tag_01"

Dim Tag_01, Tag_Text_01, Tag_Text_02, Tag_Text_03
Tag_01 = Tag_Text_01 & " " & Tag_Text_02 & " " & Tag_Text_03

How do you configure return values and parameters for a script?

The script must be of the "Function" type for it to be able to return a value. For this, you go to "Properties > General > Settings" in the TIA Portal and for "Type" you select "Function" in the drop-down list box.
 In the "Parameter" table you create a list with script tags to which the internal or controller tags are copied when the script is called.

To configure a return value, in the script you assign to a tag with the name of the script the expression to be assigned.

Example
Configure the type, parameter and return value of a script "VBFunction_1" as follows in the TIA Portal:


Fig. 01

The script is called as follows by linking the "Click" event to a button, for example:


Fig. 02

In the script the "Parameter_1" has the value of "HMI_Tag_1", and "Parameter_2" has the value of "HMI_Tag_2". There must be one line in the script which defines the return value.

The return value in the following example is the product of the two transferred parameters:

VB_Function_1= Parameter_1 * Parameter_2

where "VB_Function_1" is the name of the function. After processing of the script, "HMI_Tag_3" receives the return value of the function, in this case "HMI_Tag_1" * "HMI_Tag_2".

Note
Defining a parameter "by reference", this means as "By_Ref" parameter, only has an influence if the referenced parameter is a tag of another calling script, but not if it is an HMI or controller tag.


How do you change the properties (color, length etc.) of an object in Runtime?

The properties of objects and notes for your application available for scripts are given in the manual:
SIMATIC WinCC WinCC Engineering V15 - Programming Reference (Entry ID 109755216) under "VBScripting > VBS Object Model > Properties".

What can cause the "Maximum nesting depth reached" message to be triggered?

The maximum nesting depth can be reached if too many scripts call themselves or one another before they end themselves.

Remedy
Check the processing of the scripts and, if necessary, reduce the nesting of the scripts.
  

How do you determine the current screen number and screen name?

The predefined VB function "HmiRuntime.BaseScreenName" provides the name of the current screen as return value.

Notes
>Direct reading out of the screen number via script is not possible.
Alternatively you can use the "Screen number" area pointer.

How do you use multiplex tags in the script?

Multiplex tags cannot be used in a script.

How do you use HMI UDT tags in the script?

You can use the "SmartTags" keyword to access the elements of tags with the data type of an HMI UDT (User Defined Data Type).
 
Example Access to the element "x" of the UDT tag "Recipe"

SmartTags("Recipe.x")

Note
This applies for the instances of the library type HMI UDT created in the project. There can be no direct access to the library type.

How do you save tags in a script so that they are retained between two script calls?

Data of a script that is to be retained between two calls has to be stored in global tags. Global tags can be HMI or PLC tags.

Tags defined in a script are always deleted when the script ends.
 

How do you change the content of a text field via a script?

The text of a text field can be changed via a script in Runtime in that the corresponding object property is overwritten.

Example In the "MachineData" screen the content "NewText" is assigned to the text field "Text_Field_01"

Dim obj

Set obj = HmiRuntime.Screens("MachineData").ScreenItems("Text_Field_01")
obj.Text = "NewText"

Note
The text change is only "temporary". After a screen change the changed text is overwritten by the text stored in the configuration.
 

How do you read a user's password from the list of passwords or user view using a script?

Access data cannot be read out for security reasons.

How do you use a script to end the WinCC Runtime and then shut down the PC or panel?

You can end the WinCC Runtime and operating system with the system function "StopRuntime".
You transfer an additional parameter to "StopRuntime" to end just Runtime or both Runtime and OS:
   
ParameterMeaning

0 = hmiStopRuntime

Ends on the Runtime

1 = hmiStopRuntimeAndOperationSystem

Ends both the Runtime and the OS

Examples
StopRuntime hmiStopRuntime
StopRuntime 1

Note
The operating system of a Windows-based panel cannot be shut down in this way.
  

How do you address objects from a template or screen via a script?

In conjunction with the Runtime object property "ActiveScreen" any screen objects can be addressed with their configuration name and their properties changed.

Example The object "Circle_01" is made invisible in the active screen

 HmiRuntime.ActiveScreen.ScreenItems("Circle_01").Visible=False

Note
The configured objects in the screens must have unique names.
The property of the object is only changed temporarily. After a screen change the view stored in the configuration is used.
 

How do you put the focus on a specific screen object in Runtime (an input field, for example)?

The focus can be put on an object via the "Activate" function. Text inputs are then directed automatically to that object.

Example In the screen "Screen_1" the focus is put on the input/output field "IOField_1"

HMIRuntime.Screens("Screen_1").ScreenItems("IOField_1").Activate


Further Information
The following FAQ responses also deal with scripting in WinCC Advanced with reference to various problem scenarios.

  • Which VBS information and VBS programming aids are there in WinCC (TIA Portal)?"
    Entry ID: 59885894
  • "How can you provide new tags for the separate trends of an f(t) trend display by means of a C or VB script in WinCC Runtime Professional V11 and higher?"
    Entry ID: 55741262
  • "How do you access an SQL database in WinCC Runtime Advanced using a script?"
    Entry ID: 61883659
  • "How do you write data to a file or read data from a file by means of a script on a Comfort Panel with SIMATIC WinCC (TIA Portal)?"
    Entry ID: 59604194
  • "How do you use scripts to dynamize objects in WinCC Comfort and WinCC Advanced?"
    Entry ID: 53752382

 

Security information
In order to protect plants, systems, machines and networks against cyber threats, it is necessary to implement – and continuously maintain – a holistic, state-of-the-art industrial security concept. Siemens’ products and solutions constitute one element of such a concept. For more information about industrial security, please visit
http://www.siemens.com/industrialsecurity.