Variables in Basic Scripts

In the Movicon Basic Scripts you can use both the Movicon Real Time DB variables, which are global variables for all the project, and local Basic Script variables, which are only visible within Basic routines and destroyed once the Basic Script has terminated.  

The project's variables (Movicon Real Time DB) can be directly used by their names or by using the "DBVariableCmdTarget" (GetVariableValue(), SetVariableValue()) functions. When structure variables are used directly, the (":") colon character used to separate the variable name and the member name can no longer be used and must be replaced with the ("_") underscore or (".") point characters.  However, it can still be used when enclosed within square brackets ([Structure:Member]). The standard colon character is used in all the basic functions which require a variable as a parameter (ie. GetVariableValue(), SetVariableValue(), etc.).

 

For more information on syntax and the use of Structure variables in the Basic script, please see chapter "Pointing Variables Structure".

 

It is not possible to directly use the name of variables declared in the project's Real Time DB which start with a number or an underscore ("_") character. This is due to the fact that the VBA interpreter does not allow variable declarations of this type and, therefore, in cases such as these it will be necessary to use the GetVariableValue(), SetVariableValue()) functions.

 

Variables in Basic Scripts have priority over those of the project's.  So when the name of the project variable is used directly in basic script code that already has variable with the same name, it will be ignored and the already existing one will be set or read.  In this case, you should use the GetVariableValue(), SetVariableValue() basic functions.

 

Bit type variables, when used directly with their name, are converted into boolean (true or false) type.  This means that a project variable with value "1" will be read as a "true" variable in the Basic Script and, therefore, with a numeric value equal to "-1". The "0" value which is interpreted as "false" by the Basic Script will always  be a numeric value equal to "0". This mechanism  also applies to both the read and write of bit type variables in Basic Scripts. The GetVariableValue() and SetVariableValue() basic script functions can always be used to immediate this and, therefore the function's  return value will always be the numeric value of the bit, "1" and "0", and not the boolean value.

 

Example:

IWhen wanting to read the bTest variable declared in the Movicon DataBase as bit and set to "1", the result will be:

 

Sub Main()

Debug.Print bTest 'Result = -1

Debug.Print GetVariableValue("bTest") 'Result = 1

End Sub

 

 

IntelliSense use for RealTimeDB variables

The VBA IntelliSense can be populated by the project's global variables (the RealTimeDB variables). This will allow you to retrieve variables by using the drop-down list, which appears when pressing the "Ctrl+Spacebar", and obtain direct access to the structure type variables or byte array type variables.

An "IntelliSense" option has been added to the variable properties so that you can add variables to the VBA IntelliSense list. The default value for this option is "disabled", but it can be enabled for all the variables that you wish to make available in the VBA IntelliSense.

 

 When the "IntelliSense" property has been enabled, the Bytes Array elements and Structure variable members that have been defined in the project's RealTimeDB can be accessed in the basic script code by using the following syntax:

 

ArrayVarName.eMemberIndex (where MemberIndex starts from "0" to "array size -1". Example:  "Motor.e5" indicates the sixth "Motor" array element )

StructureVarName.MemberName

 

LIMITATIONS: the IntelliSense does not show the local screen variables and will not populate with global variables when used in the "Dynamic Properties Explorer".

 

We suggest that you enable the "IntelliSense" option only for those variables you intend to use in VBA scripts. The process to populate the IntelliSense when opening or running a script may be slower if the number of  variables enabled with this option  is in the thousands.

 

 

Variables without sign

The WinWrap does not provide the use of variables without sign.  Therefore, in order to use a RealTimeDB WORD variable type (without sign) in the Basic Script, it must be converted to prevent overflow errors. Overflow errors occur when the variable used in the script exceeds the INT type value (ie. 32767 for one Word) because the basic script engine does not manage variables without sign.  To avoid this from happening, a WORD type variable will always be passed to the WinWrap as a Long type and a DWORD type variable will be is passed to the WinWrap as a "Hauge_" type, being a "64 bit integer value".

 

WORD type variables can be managed in the WinCE environment but not DWORD type (when exceeding a Long value)  as WinCE does not support "VT_18" data types.

 

 

See Also