Tips and tricks for creating scripts in WinCC (TIA Portal)
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: 109773506) 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.
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.
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)
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")
Example Description of the first eleven elements of the array "DM10_HMI_Data" via a loop
For Tag_01=0 to 10
SmartTags("DB10_HMI_Data")(Tag_01) = 5
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
Tag_01 = HMI_Data(0, 5)
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
' Copy from the CPU to the script:
For i = 0 To 9
local_array(i) = SmartTags("CPU_Array")(i)
' Copy back to the CPU:
SmartTags("CPU_Array") = local_array
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.
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.
Tag_01 = &h45E ' Corresponds to the decimal value "1118"
How do you show or hide individual levels in a script?
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.
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
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?
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.
VB_Function_1= Parameter_1 * Parameter_2
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.
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.
>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).
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?
Example In the "MachineData" screen the content "NewText" is assigned to the text field "Text_Field_01"
Set obj = HmiRuntime.Screens("MachineData").ScreenItems("Text_Field_01")
obj.Text = "NewText"
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?
How do you use a script to end the WinCC Runtime and then shut down the PC or panel?
0 = hmiStopRuntime
|Ends on the Runtime|
1 = hmiStopRuntimeAndOperationSystem
|Ends both the Runtime and the OS|
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?
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 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
- You have a technical question / problem: Ask the Technical Support
- You want to discuss in our forum and exchange experiences with other users
- You want to create CAx data for one or more products
- You would like to send us feedback on this Entry