Scripting MOC
Scripting MOC
Movicon NExT
WWB NET Language
Ver.3.4
Basic Script
Table of Contents
1. WWB.NET LANGUAGE ............................................................................ 1
1.1. PREFACE ........................................................................................................................................... 1
1.2. INTRODUCTION ............................................................................................................................... 1
1
2
Basic Script
1. WWB.NET Language
1.1. Preface
All the information contained in the Platform.NExT documentation is based on the
assumption that:
1.2. Introduction
Platform.NExT Basic Script Language Programming Guide Contents
1
Basic Script
WWB.NET Support
The WWB.NET is the predefined language which is enabled through the '#Language
"WWB.NET" directive inserted for default at the start of a script resource. This permits
direct access to the .NET assemblies with VB code. The dialog window used for adding
reference objects to be used in the code proposes a list of all the selectable assemblies.
The example code assumes that you have added a reference to the
System.Windows.Forms assembly:
'#Language "WWB.NET"
Sub Main
t = New System.Windows.Forms.Timer
t.Interval = 1000
t.Enabled = True
Wait 1
End Sub
Restrictions:
3
• The signatures of the routines in the objects cannot be renames such as would be
allowed with the .NET programming. Le firme delle routine negli oggetti non
possono essere rinominate così come consentirebbe la programmazione .NET
• Not more than one event can be linked to the same procedure using a list of
"Handles"
Variable Script Events cannot be managed in WWB:NET scripts
In WinWrap Basic version 9, the basic script is single thread and the script code must
be executed from the thread that created it. For example, a .NET delegate can not be
passed to an object that manages this delegate in another thread. A good example of
this would be some of the "System.IO.FileSystemWatcher" class functions that create
additional threads for monitoring file modifications. In this case the methods of this
class will not support other thread calls. This is also the case for the
"System.Net.Sockets" class.
In WinWrap Basic version 9, the basic script do not support nested types, being a type
defined in the ambient of another type. For example, it is not possible to use the
"System.Net.WebRequestMethods.Ftp" functions.
Unicode Support
The code editor consents you to insert strings in Unicode format. Therefore you can
view the Unicode strings in the basic script dialog window or assign Unicode texts to the
project's string variables.
UTF8 or UTF16 Unicode files can be read and/or written by writing one of the two new
"vbUTF8BOM" and "vbUTF16BOM" constants, added for this purpose, in the first
character of a text file in order to determine its code.
A Basic Script resource must contain the Main (Sub Main) procedure inside. The
instructions contained in this subroutine will be executed when the basic script is
launched from the project's logic. At the end of the subroutine, without any
programmed loop cycles, the basic is terminated and made ready for the next call.
The Main procedure does not have configurable parameters but a parameter can be
associated the moment the Basic Script is called. In the command that calls the Basic
Script you should find a parameter that can contain several arguments separated by a
separator character. The StartupContext.Parameter() function in the basic script can be
used for reading the vlaue of the parameter with which the basic script was
called. The vb.net “split” function can then be used to read each argument by passing
the character used as the separator.
4
Basic Script
After a Basic Script resource has been put into execution for the first time, the
resource will remain active after the Sub Main() has terminated. Therefore any
variables referred to within the resource will be counted for license purposes. If
you wish to terminate the execution of a Basic Script completely, you will need to use
"Stop" command available in the Platform.NExT "Command List".
The stop command of a basic script resource unloads only those basic
resources which are being run in separate threads from memory. The
other basic script resources being run in the same thread are only
stopped. As a consequence of this, the "Unloading" event is no longer
executed following a stop command for those basic script resources
which are not in separate threads. In addition to this the variables used by
the basic script always remain in use once the basic script has been run at
least once.
A Basic Script resource introduced into the project can be put into execution in various
ways according to what is required. The following paragraphs illustrate these
methodologies.
Execute on command
Execute on command can be easily set by means of using object, menu or resource
command lists that provide this possibility.
For example, if you wish to associate a Basic Script routine execution to a button, go to
“Common Property Editor”->"Commands"->"Scripts" property and select the Basic
resource desired among those proposed on the list referring to the ones introduced,
and then define the relevant properties as desired.
The execution on command of a Basic Script can also be established from "Events"
resource, whereby the basic routine will be executed upon the occurrence of a
determined event and not by a operator command.
• Normal: this is the predefined value, the script is executed in asynchro mode in respect
to the call.
• Synchro: the script is executed in sychro mode, the call waits until the execution is
completed before taking control (the debug is disabled in this mode)
• Shared:The script shares the same execution thread of the other scripts that have been
started up in the same way. This consents a more limited use of the machine's
resources
• Stop: the script is unloaded from memory
Execute at startup
A Basic Script routine can automatically be started up at project runtime. In order to
use this functionality, you need to access the project's "Startup Script List" property
settings.
The Basic Script resource must be selected from those previously inserted in the project
using the selection window.
5
5.2. Basic Scrip in Project
You can use Basic Script routines inside projects in different circumstances and
modalities.
It is best to use them in those circumstances where the same operations are not
available with other resources or methods: execution/animation properties of symbols,
Events and Schedulers for example. The unconditional use of the basic scripts in
projects is very handy in the project design time phase but may slow down project
execution and consume more of the project's resources.
The basic code can be used in more areas of the project: as resource, directly in the
execution properties of drawing objects, as codes associated to the events of objects
(alarms, drawings or symbols), in screens.
These functionalities are discussed in more detail in the relating sections.
6
Basic Script
The window which opens shows the name of the interface in the "Data Type" box and
the methods and properties relating to that interface are listed in the
"Methods/Properties" list box:
Another interface can be selected by using the "Data Type" list box.
To get a function's help just select the function and click the question mark if found in
the functions' Browse window, or if inside a basic code just select the function and click
the F1 key. The various fields in the Browse window mean:
Back
Returns one step back on the libraries hierarchy scale. Some libraries are set with
objects which methods and properties are associated to, for instance when displaying
the property of an object and clicking on the back button should return you back to the
list of objects set in the library.
Paste
Copies the contents of the box at the side, in the point where the cursor is situated in
Script's editor window. If the command is disabled indication will be given by telling you
it is impossible to copy the contents in the position indicated by the cursor.
Close
Closes the browse window and the Script's editor returns active.
Library
Allows you to select one of the library proposed by the list. When ActiveX/OCX have
been added by using the references, these will also be on the list.
Data Type
Allows you to select a data type from the list proposed. The list refers to the library
selected in the Library box.
Methods/Properties
Allows you to select a method or a property from the list proposed. The list refers to the
data type selected in the Data Type box.
7
?
Accesses the help of the property or method selected in the box at the side. Some
external libraries, not setup by Progea and enabled through the references, do not
install their help files.
Parameters
This displays any eventual list of parameters set for the method selected in the Data
Type box.
Routines can be created in basic scripts which are basically portions of code enclosed in
a block and come in two types:
6. Sub
7. Function
The difference between these two types or routines is very slight. Both can be called by
parameter passing. The difference of the "Function" compared to the "Sub" is that is
can return a value type set by the programmer (Bool, Int, String, etc.).
Example 1:
The Sub Test is called inside the Sub Main of a Basic Script:
Sub Main
Call Test()
End Sub
Sub Test()
MsgBox("Test Sub", vbInformation + vbOkOnly,GetProjectTitle)
End Sub
Example 2:
The Function Test is called inside the Sub Main of a Script and the key pressed by the
user in the MsgBox is put on log:
Sub Main
Debug.Print Test()
End Sub
8
Basic Script
The routines described above can be called by other routines, creating nested calls
between them. There must always be a "Sub Main" in the Basic Script Resource which
is the routine automatically executed by the Basic Script when run. However, it is the
programmer's job to insert the right codes inside this routine and call any other Sub or
Function they themselves have created. Once all the instructions obtained in the Sub
Main() have been executed, the basic script is terminated and will have to be called
again in order to run it another time. It is also possible to insert loops in the Sub Main()
in order to keep the Basic Script running.
Uses Clause
Function within other scripts can be referenced within scripts.
This is done by using the #Uses clause in the script within which you wish to reference
functions belonging to other script resources.
In cases where the called script also resides within a different folder, the syntax
in which the #Uses clause will become:
'#Uses "*..\General\PublicFunctions"
Events
There are Sub routines provided by the system (Platform.NExT or any inserted ActiveX
component ) which are automatically called by the system at the forefront of certain
events. These routines, which are actually called "Events", can be inserted in the script
and it is up to the programmer to add the desired codes inside them.
Esempio:
When the following code is inserted inside the a rectangle design's script, a MsgBox
with the word "Click" will appear every time the rectangle is clicked on with the mouse:
'#Language "WWB.NET"
Public Sub Control_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Input.MouseButtonEventArgs) Handles Control.MouseUp
9
MsgBox("Hello!",vbInformation, "Platform.NExT")
End Sub
In this case the Public Sub Control_MouseUp() is an event triggered by the system when
performing a Mouse Up on the rectangle, while its internal code must be inserted by the
programmer.
A series of events made available by the system can be selected within basic code of
screen symbols and objects. These events exclusively concern the component in which
the code is being edited being the events in question which regard those triggered by
the component. These events are listed in the "Proc" list when the "(Control)" item is
selected in the "Object" list as shown in the image above.
When the Tag's code has been set with a breakpoint, a winwrap debug
window will open at Server startup even when the method has not yet been
invoked.
The same result can be achieved by adding a "Call Method" command to the client
button and selecting the method to be called in the "OPC UA Browser" card.
The eventual parameters will be displayed in a window after the variable's method has
been dragged from the server window onto the button to which the "Call Method"
command is to be applied.
As an alternative, by using the "OPC UA Browser" card with the "Data Settings" button
you will be able to set the parameter's value.
While associating the method to the button you will be asked to set the method with a
value if an input parameter exists.
10
Basic Script
If you come across an input parameter during the operation in order to associate the
method to the button, you will be asked to set a value to associate to the button.
These routines are executed within the server context.
The variables can then be defined as method type (as data type) in relation to a driver
that exposes methods that can be invoked by means of the variable.
Eg:
If fi.Exists() Then
fi.Delete()
End If
11
However, you must be careful when writing single array elements as the Server will not
notify this change to any connected Clients. In order to make sure that the notification
takes place properly, you will need to update the whole array variable completely. The
best thing to do would be to copy the array in the script's temporary support variable,
modify it and then recopy the temporary variable in the server's array variable. For
example:
The variables internal Basic Scripts have priority over those of the project. Meaning
that when using the name of a project variable directly in the basic script code
where a variable already exists with the same name, this will be ignored and the
already existing one will be set or read. In this case you will have to use the
GetVariableValue(), SetVariableValue() basic functions.
Bit type variables, when used directly with their name, are converted into boolean (true
or false). This means that a project variable with value "1" will be read as a "true"
variable inside the Basic Script and therefore with a numeric value equal to "-1". The "0"
value is interpreted as "false" by the Basic Script which will always be a numeric value
equal to "0". This mechanism also goes for both the reading and writing of bit variables
inside Basic Scripts. The GetVariableValue() and SetVariableValue() basic script functions
can always be used to obviate this mechanism. In this way the function's return value
will always be the numeric value of the bit, "1" and "0", and not the boolean value.
Example::
When reading the bTest variable declared in the Platform.NExT DataBase as bit and set
to "1", the result will be:
12
Basic Script
In cases for variables contained inside a folder, for example if there is a "Var1" variable
in the "F1" folder, the direct notation from script to refer to this variable would be:
"F1_Var1"
Based on the script syntax, it isn't possible to differentiate the variables contained in a
folder from those that have names beginning with the same name of the folder.
For example "F1\Var1" and"F1_Var1" would both be referred with the direct "F1_Var1"
notation.
Therefore great care must be taken to avoid creating overlaps such as these or
otherwise indirect notation should be used to avoid misinterpretation:
ScriptDocument("F1\Var1") for the variable inside the folder and
ScriptDocument("F1_Var1") for the variable in the root.
Array Variables
In cases where Array variable types are used, each array element can be accessed by
specifying its number between rounded brackets by using direct tag access syntax. For
example, let's suppose that we have created the 'var_01' array tag type in the server's
address space, in order to access the tag's elements we would use the following syntax:
Var_01(0) = 1
Var_01(1) = 2
However, direct syntaxes can only be used in script on the Client side and not in that of
the Server side.
Despite this, it is also possible to access Array elements using script functions with the
following syntax:
However, please note that even though the script functions are used to write an Array
element, it will not be notified to the Client which will continue showing the old
value. In order for the Server to notify the Client of this change, the entire Array variable
will have to be set. Therefore, you will need to use a temporary variable within the
script as in the following example where “Folder1\Tag_ArrayFloat” is the Server's Array
variable:
13
'#Language "WWB.NET"
Option Explicit
Sub Main Handles .Main
Dim myArr() As Single
Dim nLen As Integer
Dim iCounter As Integer
nLen = UBound(Folder1_Tag_ArrayFloat)
ReDim myArr(nLen)
myArr = Folder1_Tag_ArrayFloat
For iCounter = LBound(myArr) To UBound(myArr)
myArr(iCounter) = myArr(iCounter) + 1
Next iCounter
Folder1_Tag_ArrayFloat = myArr
End Sub
Structure Variables
In cases using structure type variables, the dot (".") separation character is used between
the variable name and its member names to access them directly. However, when
using the GetVariableValue() and SetVariableValue() functions, the colon (":") separation
character must be used to separate the variable and its member names to access
them. For example, when defining a “TagStruct01” variable as a structure type with a
prototype with “Member_1” and “Member_2” as members, the syntax to use to access
this variable will be:
• Direct access:
TagStruct01.Member_1 = 1
At the moment only simple members defined in the prototype's root can
be accessed by script and not members defined in the prototype's folder or
members defined as prototype types (embedded structures).
• Direct access:
Folder1_Var1 = 1
• Direct access:
Folder1_Folder2_Folder3_Var1 = 1
14
Basic Script
When accessing tags directly you need to make sure that you don't define the names of
folders and variables that might result as homonyms. For example, If you define
“Motor1” variable inside the “Machine1” folder in the root of the address space, and a
second variable directly in the address space's root as “Machine1_ Motor1”, the script
intellisense will have problems distinguishing them from each other because the syntax
you would have to use to access them both would be the same: “Machine1_
Motor1”. In situations like this, it would be better to use the GetVariableValue() and
SetVariableValue() in order to make distinctions between the two tags.
• TemporaryVariables.LocalTag1
• SystemVariables.CurrentUser
Intellisense in Scripts
The Basic Script resources are able to identify the project's variables when written
directly and in those cases where the tag name is written in blue. This comes in handy
when using the “CTRL+Space Bar” to open the list of available methods and properties
as well as the project's tags where you will be able to select the name of a tag instead of
having to write it. Therefore when you press the “CTRL+Space Bar” within the script, a
list box will appear like the one below allowing you to scroll and select the tag desired:
15
7.3. WWB.NET™Into Objects
Symbols in screens and screens can contain basic script code. Basic codes inserted in
objects are managed differently to those from Basic Script resource, whose execution is
controlled in the project's logic. A series of events have been provided in the project
that when selected and then inserted inside the basic script editor allow you to insert
codes. The basic routines in these events will be executed when the events are
triggered during project runtime,
Each object may have different properties, methods and events according its function
type. Please refer to the lists in the specific paragraphs to get further details on these.
Please keep in mind that the codes inside drawings are not initialized straight away
when the screen page is loaded but only when needed. If a symbol contains
16
Basic Script
the "Loaded" event Movicon NExT is obligated to initialize the basic script code
contained in that drawing straight away. This means that page loading is quicker when
the drawings associated to it do not contain the "Loaded"" event. However this does
not mean that the "Loaded" event should not be used altogether but only when
necessary. The project variables (the Platform.NExT Address Space) can be used directly
with their name or by using the appropriate functions from the
"Document.GetVariableValue()”, “Document .SetVariableValue()” for the code edited at
screen object level.
The script codes of drawings are loaded only when they are needed and not
when the page is being loaded.
'#Language "WWB.NET"
Imports System.Windows.Forms
Dim WithEvents t As System.Windows.Forms.Timer
Sub Main
t = New System.Windows.Forms.Timer
'Preset Timer
t.Interval = 1000
'Start/Stop Timer
If t.Enabled Then
t.Enabled = False
Else
t.Enabled = True
End If
End Sub
'This event is triggered at the end of each set interval
Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
ScriptDocument.SetVariableValue("ActualTime", Format(Now, "dddd dd-mm-yyyy") &
vbLf & Format(Now, "hh:nn:ss"))
End Sub
'#Language "WWB.NET"
Imports System.IO
Sub Main Handles .Main
Try
Dim path As String= "c:\Test"
'WatchTest.Log è il file che viene monitorato
Dim watch As New FileSystemWatcher(path, "WatchTest.Log")
'Add an event handler for the "Change" event provided"
AddHandler watch.Changed, AddressOf watch_Changed
17
watch.EnableRaisingEvents = True
'the loop keeps the script active and enables the event to be triggered that in cases
of the FileSystemWatcher is run from a distinct thread by the caller
Do
DoEvents
Loop Until ScriptDocument.IsInStoppingMode
Catch ex As System.Exception
Debug.Print ex.Message
Finally
End Try
End Sub
Private Sub watch_Changed(ByVal sender As Object, ByVal e As System.EventArgs)
Dim actualVal As Integer
'VariableChange will be the variable that counts the number of changes
actualVal= ScriptDocument.GetVariableValue("VariableChange")
ScriptDocument.setVariableValue("VariableChange",actualVal+1)
End Sub
18
Basic Script
The basic script editor is composed of a window containing an area with a white
background in which you can edit codes, plus a series of commands as described below.
The window can be resized with the possibility to open more than one window at the
same time so that Copy&Paste operations from different projects can be performed
faster. To open the editor simply click on the Basic Script resource.
The script code editing of objects and screens is done using the Platform.NExT "Script
Explorer" window. The contents of this window change dynamically based on the
component selected, by show its associated script code.
Both the editing windows shown above provide two drop-down lists, "Object:" and
"Proc:".
The "Object:" list displays a list of basic set objects in Platform.NExT which corresponds
to "(General)", "Control" and "ScriptDocument". The selection of one object in
respect to another changes the list of procedures in the "Proc:" list available for that
specific object.
"(General)" : identifies the procedure programmed for that specific object and the list
may change according to the object selected on screen.
"Document": this is an interface that refers to the screen. If for example the
"VariableChanged" event is used, variable changes will be notified on the screen.
"Control": identifies a screen object's events and is only available when the code
associated the screen object is being edited.
"Entity": identifies the control as framework object.
19
"Parent": this is an interface that refers to the project.
A list of events available in the "Proc:" list is displayed according to the selection made
in the "Object:" list.
Browse
This command is used for accessing the list of functions and properties available in the
basic script. The window which appears is called Browse function.
Start/Resume
This command starts the basic script. If the code is referred to a project's basic
resource, the contents of the Sub Main procedure are executed. Otherwise, if the
code is referred to an object, it enables the events management in that object. It is
used to run a check on the syntax used in the written code, in cases where it is not
referred to a project resource, which requires the project to be started up in runtime
(eg. to read variables) where the code will be executed as well (eg. to read values from
DB).
Pause
This command pauses the basic script by positioning the cursor on the line being
executed which will automatically be highlighted in yellow.
End
This command aborts the basic script run. The code must be paused beforehand in
order for this command to work.
Break Point
This command inserts or removes a breakpoint on the line in which the cursor is
present. This function is also made available by clicking outside the extreme left
editor window border which coincides with the line in which the breakpoint is to be
inserted. The F9 key can also be used as an alternative. The moment in which the
basic has to execute a line of code which has a break point, the execution will be
automatically placed in pause mode.
Quick Watch
This command returns the result of the highlighted function, in a basic script dialog
window or in the debug window (Watch window).
20
Basic Script
Step Into
This command executes the step into one function at a time, each time it is pressed.
Step Over
This command steps over one function at a time, each time it is pressed.
Step Out
This command executes the all the functions up to the line in which the cursor is
situated.
Edit UserDialog
This command opens the tool for creating the basic script's dialog window graphics.
When exiting with OK, all that has been created graphically will be translated into
code. When positioning and activating the command on this window all the new
graphics will be reproduced from the translated basic script code.
Some basic script functions verified in programming mode may return with
different values when executed in runtime, therefore we advise you to use the
debug in the programming phase to check codes roughly and repeat the test
by executing the project in runtime to get an accurate test.
21
22
Basic Script
Each "Basic Script" routine inserted as a resource in the "Project Explorer" window can
receive in association Properties to setup how they function during a run. In order to
this just select the "Basic Script" desired and then change its settings through the
Movicon "Properties Window".
General
For performance reasons, the Script starts running without waiting until the
Cycle Time Tag has been subscribed to the Server. This may mean that the
Cycle Time Tag may start updating after the Script has gone into execution.
For performance reasons, the Script starts running without waiting until the
Cycle Time Tag has been subscribed to the Server. This may mean that the
Cycle Time Tag may start updating after the Script has gone into execution.
Thread Priority
A basic resource can be executed with different priorities: High, normal and low:
23
The property is set to false for default. When set to true, the script will force the writing
of the value of a valuable unconditionally to that of the previous one. This will permit
same value to be rewritten within a tag.
Execution
Sleep Time
The sleep time is set in milliseconds and is used to lighten the processor's basic script
execution workload. The higher the sleep time, the less the basic script will engage the
processor to execute it. As a consequence the script will take longer to execute.
Connection Settings
Session Name
When specifying a name in this field the Script will run in a different Server connection
session to the one used by the Movicon Client. When the Script runs in a different
session this means that the Server connection parameters will be those defined in the
Script resource. However, if the Script is run in the same session used by the Movicon
Client, the 'Session Name' parameter is left empty and the Server connection
parameters will be the same as those of the Movicon Client which are defined in the
project's 'Connection Settings' properties.
24
Basic Script
Publishing Interval
Nofication time of Tags towards Server.
Debug
Enable Log
Enabling this selection box, an xml file will be created in the script's folder for the Basic
Script routine during runtime. This file will contain printed messages belonging to the
script such Debug.Prints. This file will reaming in use until the script stops running due
to performance reasons.
This file will always be regenerated at startup.
Enable SysLog
when enabling this property, the messages from the Debug.Print function will also be
printed in the project's Historical Log as well.
25
26