What is Visual Basic Script in WinCC? Simple VBScript Example
VBScript or visual basic script is a scripting language used for developing and designed of automation and scripting tasks. vb script is commonly used for scripting within HMI environments to automate repetitive tasks, such as tag values, visualization task, alarm handling, network error handling, administrative tasks etc. vbscript is more user friendly scripting language so it is mostly used in industial control system. It is based on Microsoft's Visual Basic programming language and is primarily used for automating tasks. in wincc vbscript help to designing more user friendly graphical user interface. wincc hmi also support vbscript. this article only for wincc hmi scripting. you can use this script language in wincc software or wincc tia portal.
add component and tag for script
open or create your project in wincc flexible or wincc tia portal. select HMI and add component to your hmi in this article a button and two input output field and a text field is inserted. after insering these component add input1, input2 tag and select UINT data type in tag table. Assign input1 and input2 tag for input output fields.
how to add vbscript in wincc
to add vbscript in wincc, go to project tree open script and than click to add new vbscript now next step is write a script for hmi. in wincc have two type of script function you can add
Sub : A Sub is Subroutine function is used for executing a block of code that does not return a value. It performs a series of statements or actions.
Function : A Function is used for executing a block of code that returns a value. It's typically used when you need to perform some computation or operation and get a result back.
in vb script open property to select above script type. by default Sub function. first example is Subroutine function in vbscript and second example is a function vbscript without parameter.
how to write a vbscript in wincc
after adding script open script in wincc flexible and write a following code in this script example we simply add two input output field value. following are the script subroutine vb script example explanation
Explanation of above wincc vbscript
Sub VBFunction_3()
Sub : sub routine function
VBFunction_3() is name of subroutine function name you can change as per your program.
Dim: Declares variables to store data.
value1 and value2 will hold the values of SmartTags.
result will be used to reference a screen item (e.g., a text field) in the HMI.
SmartTag keywords used to Reads the value of the tag input1 and tag input2.
Set: Used to assign an object reference to a variable. Here, result refers to a specific screen item.
HmiRuntime.Screens("Screen_2") is Accesses the "Screen_2" of the HMI. you can replace root screen with your screen name.
ScreenItems("Text field_1"):Refers to a specific object (a text field) on the "Screen_2"."Text field_1" is the name of the screen item that will display the result.
result.Text:The Text property of the Text field_1 screen item is updated.
after creating script click to check script error, if any error occurred during error checking remove the error for next step.
declare vbscript in hmi component in wincc
after creating no error script select button and open button property and select event in event select button press event and select script
check script during simulation
in simulation you can check how your actual work in hmi. after assigning script in button event simulate program to test. when wincc simulation start and value of input output fild is changed. supposed in input field value 124 and input filed value is 124 and than add button is pressed result of these input output filed shown in text field.
simple example of vbscript function in wincc
in this simple vb script value of increment when increment button is pressed. each click of increment button increase value by 1. so first add button and rename button to increment and a numerical input output field where we show incremental value when button is pressed. Add a script and than add add following script in script editor.
above is the function vbscript in wincc without parameter. This VBScript code defines a function named VBFunction_4 that increments the value of a SmartTag named "tag1" by 1 and then returns the updated value. following is the line by line explanation of vbscript in wincc
Function VBFunction_4()
This declares the start of a function named VBFunction_4.
A function in VBScript can execute code and must return a value.
SmartTags("tag1") = SmartTags("tag1") + 1
This line increments the value stored in the SmartTag with the key "tag1" by 1.
SmartTags("tag1"): Refers to the value associated with the tag named "tag1".
+ 1: Adds 1 to the current value of SmartTags("tag1") and updates it.
VBFunction_4 = SmartTags("tag1")
This assigns the updated value of SmartTags("tag1") to the function's name, VBFunction_4.
In VBScript, this is how you specify the return value for a function.
End Function
Marks the end of the function.
declare vbscript function in wincc
after create above script define this vbscript function to button so you can use this script when button clicked. so first goto button property open button event tab and select script.and in input output field declare tag1 in process tag property tab.