Running SCADA Functions

Using SCADA Functions within Scripts

SCADA scripts allow you to call your project’s built-in functions (Tag value assignment, program execution, alarm triggering, etc.) directly from C# or VB code. This enables you to use operations available in the function block window programmatically.
The key to this operation is the SCADAFunctionRun (for C#) or SCADAFunction.Run (for VB) method.

Basic Syntax

The method structure accepts the function type and its parameters sequentially.

C# Syntax:

BOOL result = SCADAFunctionRun(“FunctionType”, Param1, Param2, Param3, …);

VB Syntax:

Dim result As BOOL = SCADAFunction.Run(“FunctionType”, Param1, Param2, Param3, …)

Unused Parameters

If you are using fewer parameters than a function requires (or if some are optional), 

  • C# – unused parameters must be written as null.
  • VB – unused parameters must be written as Nothing.
Defining SCADAFunction in VB (Important)

In VB script mode, SCADAFunction must be defined as an object within the MainClass.
If this object is not defined, SCADAFunction.Run(…) will not work.
The correct structure is shown below:

Public Class MainClass
    Public SCADAFunction As Object
    Public Sub Main
        Dim result As BOOL = SCADAFunction.Run("Tag", """true""", "=", "BooleanTag_0", Nothing, Nothing)
        MessageBox.Show(result.ToString(), "")
    End Sub
End Class

With this structure, the SCADA environment automatically initializes the SCADAFunction object before the script runs and ensures that function calls are handled correctly.

Main Example: “Tag” Function

Let’s examine the usage through the most common function, “Tag” (Tag Value Assignment).

C# Example:

/*
 * This function sets the value of the tag named "BooleanTag_0" to "true".
 * Function Type: "Tag"
 * Param 1: "BooleanTag_0" (Which tag will change?)
 * Param 2: "=" (Which operator?)
 * Param 3: true (Which value will be assigned?)
 * Param 4: null (Unused)
 * Param 5: null (Unused)
 */
BOOL result = SCADAFunctionRun("Tag", "BooleanTag_0", "=", true, null, null);

VB Example:

' This function sets the value of the tag named "BooleanTag_0" to "true".
Dim result As BOOL = SCADAFunction.Run("Tag", "BooleanTag_0", "=", true, Nothing, Nothing)

4 Ways to Feed the Value Parameter

The power of scripts comes from their ability to accept parameters (like the true value in the example above) in four different ways. When assigning a value to a parameter, the SCADA engine can distinguish between these 4 methods:
The table below shows 4 methods to feed the [VALUE] part in the function SCADAFunctionRun(“Tag”, “BooleanTag_0”, “=”, [VALUE], null, null).

MethodC# Code Example ([VALUE] part)Description
1. Literal Value…, “=”, true, null, null);The value is written directly into the code. It can be BOOL, INT, REAL, or STRING (e.g., “Hello”).
2. String Literal…, “=”, “true”, null, null);The value is written as text inside quotation marks. The SCADA engine automatically tries to convert this to the correct type (BOOL in this example).
3. In-Script Variablebool MyVar = true;…, “=”, MyVar, null, null);The value is taken from a variable defined within the script itself (MyVar). This variable must be defined and assigned a value within the script.
4. SCADA Tag Name…, “=”, “BooleanTag_2”, null, null);The value is taken from the instantaneous value of another SCADA tag (BooleanTag_2) written in quotes. The SCADA engine understands that this is a tag name, not a variable, and fetches its value.

In-Script Function Usage Examples

1. Tag

Scenario 1: Assigning a Fixed Value
Assign the value 1500 to the Motor_Speed tag.

// Prm. 0 = "Motor_Speed" (Tag)
// Prm. 1 = "=" (Operator)
// Prm. 2 = "1500" (Fixed Value)
C#BOOL result = SCADAFunctionRun(“Tag”, “Motor_Speed”, “=”, “1500”, null, null);
VBDim result As BOOL = SCADAFunction.Run(“Tag”, “Motor_Speed”, “=”, “1500”, Nothing, Nothing)

Scenario 2: Assigning One Tag’s Value to Another
Set the value of the Setpoint_Current tag equal to the value of the Setpoint_Saved tag.

// Prm. 0 = "Setpoint_Current" (Target Tag)
// Prm. 1 = "=" (Operator)
// Prm. 2 = "Setpoint_Saved" (Source Tag to get value from)
C#BOOL result = SCADAFunctionRun(“Tag”, “Setpoint_Current”, “=”, “Setpoint_Saved”, null, null);
VBDim result As BOOL = SCADAFunction.Run(“Tag”, “Setpoint_Current”, “=”, “Setpoint_Saved”, Nothing , Nothing)

2. Screen

Scenario 1: Opening a Screen in a New Window
Open the screen named Motor_Detail_Screen in a new window over the current screen.

// Prm. 0 = "Motor_Detail_Screen" (Screen Name)
// Prm. 1 = null (Dynamic tag not used)
// Prm. 2 = "New Window" (Open Mode)
C#BOOL result = SCADAFunctionRun(“Screen”, “Motor_Detail_Screen”, null, “New Window”, null, null);
VBDim result As BOOL = SCADAFunction.Run(“Screen”, “Motor_Detail_Screen”, Nothing, “New Window”, Nothing, Nothing)

Scenario 2: Closing a Dialog
Close the currently open screen (or dialog).

// Prm. 0 = null (Screen name not required)
// Prm. 1 = null (Dynamic tag not used)
// Prm. 2 = "Close" (Action)
C#BOOL result = SCADAFunctionRun(“Screen”, null, null, “Close”, null, null);
VBDim result As BOOL = SCADAFunction.Run(“Screen”, Nothing, Nothing, “Close”, Nothing, Nothing)

3. Script

Scenario: Triggering Another Script
Run the script named Generate_Daily_Report.

// Prm. 0 = "Generate_Daily_Report" (Script Name)
// Prm. 1 = null (Dynamic tag not used)
C#BOOL result = SCADAFunctionRun(“Script”, “Generate_Daily_Report”, null, null, null, null);
VBDim result As BOOL = SCADAFunction.Run(“Script”, “Generate_Daily_Report”, Nothing, Nothing, Nothing, Nothing)

4. Report

Scenario: Generating and Emailing a Report
Generate the Monthly_Consumption_Report and send it to manager@factory.com using the SMTP_Settings account with unlimited file size (-1).

// Prm. 0 = "Monthly_Consumption_Report" (Report Name)
// Prm. 1 = "SMTP_Settings" (Email Account)
// Prm. 2 = "manager@factory.com" (Recipient Address)
// Prm. 3 = -1 (No limit)
C#BOOL result = SCADAFunctionRun(“Report”, “Monthly_Consumption_Report”, “SMTP_Settings”, “manager@factory.com”, -1, null);
VBDim result As BOOL = SCADAFunction.Run(“Report”, “Monthly_Consumption_Report”, “SMTP_Settings”, “manager@factory.com”, -1, Nothing)

5. Email

Scenario: Sending Email to a Dynamic Recipient
Send an alarm message to the email address stored in the Shift_Supervisor_Email tag using the SMTP_Settings account.

// Prm. 0 = "SMTP_Settings" (Email Account)
// Prm. 1 = "Shift_Supervisor_Email" (Dynamic Recipient)
// Prm. 2 = "Emergency Notification" (Subject)
// Prm. 3 = "Boiler 1 Pressure Critical!" (Content)
C#BOOL result = SCADAFunctionRun(“Email”, “SMTP_Settings”, “Shift_Supervisor_Email”, “Emergency Notification”, “Boiler 1 Pressure Critical!”, null);
VBDim result As BOOL = SCADAFunction.Run(“Email”, “SMTP_Settings”, “Shift_Supervisor_Email”, “Acil Emergency Bildirimi”, “Boiler 1 Pressure Critical!”, Nothing)

6. Notification

Scenario: Sending Telegram Notification
Send the message in the Production_Message tag to the target in the Group_Chat_ID tag using the General_Telegram_Group notification setting.

// Prm. 0 = "General_Telegram_Group" (Notification Setting)
// Prm. 1 = "Group_Chat_ID" (Dynamic Target)
// Prm. 2 = "Production_Message" (Dynamic Content)
C#BOOL result = SCADAFunctionRun(“Notification”, “General_Telegram_Group”, “Group_Chat_ID”, “Production_Message”, null, null);
VBDim result As BOOL = SCADAFunction.Run(“Notification”, “General_Telegram_Group”, “Group_Chat_ID”, “Production_Message”, Nothing, Nothing)

7. Print

Scenario 1: Printing the Entire Screen
Print the Summary_Report_Screen on the printer Printer_Office_1 in Portrait mode at 300 DPI.

// Prm. 0 = "Printer_Office_1"
// Prm. 1 = "Portrait"
// Prm. 2 = 300
// Prm. 3 = "Summary_Report_Screen"
// Prm. 4 = null (Entire screen)
C#BOOL result = SCADAFunctionRun(“Print”, “Printer_Office_1”, “Portrait”, 300, “Summary_Report_Screen”, null);
VBDim result As BOOL = SCADAFunction.Run(“Print”, “Printer_Office_1”, “Portrait”, 300, “Summary_Report_Screen”, Nothing)

Scenario 2: Printing a Specific Object
Print the object named Graph_Boiler_1 located on the Trends_Screen.

// Prm. 3 = "Trends_Screen"
// Prm. 4 = "Graph_Boiler_1" (Object Name)
C#BOOL result = SCADAFunctionRun(“Print”, “Printer_Office_1”, “Portrait”, 300, “Trends_Screen”, “Graph_Boiler_1”);
VBDim result As BOOL = SCADAFunction.Run(“Print”, “Printer_Office_1”, “Portrait”, 300, “Trends_Screen”, “Graph_Boiler_1”)

8. Recipe

Scenario 1: Loading a Recipe
Load the record named Product_A from the Packing_Settings recipe group to the PLC.

// Prm. 0 = "Packing_Settings" (Recipe Name)
// Prm. 1 = "Product_A" (Record Name)
// Prm. 2 = "Load" (Action)
C#BOOL result = SCADAFunctionRun(“Recipe”, “Packing_Settings”, “Product_A”, “Load”, null, null);
VBDim result As BOOL = SCADAFunction.Run(“Recipe”, “Packing_Settings”, “Product_A”, “Load”, Nothing, Nothing)

Scenario 2: Saving a Recipe (Dynamic Name)
Save the current PLC settings to the Packing_Settings recipe group using the name stored in the Recipe_Name_To_Save tag.

// Prm. 1 = "Recipe_Name_To_Save" (Dynamic Record Name)
// Prm. 2 = "Save" (Action)
C#BOOL result = SCADAFunctionRun(“Recipe”, “Packing_Settings”, “Recipe_Name_To_Save”, “Save”, null, null);
VBDim result As BOOL = SCADAFunction.Run(“Recipe”, “Packing_Settings”, “Recipe_Name_To_Save”, “Save”, Nothing, Nothing)

Leave a Reply