This guide walks you through configuring the Scripts module for custom automation and business logic. You'll create reusable classes, automated tasks, simple expressions, and integrate external libraries using C#, VB.NET, or Python.
Prerequisites:
- Basic programming knowledge (C#, VB.NET, or Python)
- Understanding of event-driven programming
- Tags configured for script interaction
In this page:
Table of Contents maxLevel 2 minLevel 2 indent 10px exclude Steps style none
Configuration Workflow
- Create ScriptClasses - Build reusable function libraries
- Create ScriptTasks - Implement automated processes
- Add ScriptExpressions - Simple one-line actions
- Add References - Include external DLL libraries
- Test and Debug - Verify script execution
Step 1: Create ScriptClasses
ScriptClasses are libraries of reusable methods available throughout your solution.
Creating a Class
- Navigate to Scripts → Classes
- Click Plus icon
- Configure:
- Name: Class identifier
- Code: Language (C#, VB.NET, Python)
- Domain: Server or Client execution
- ClassContent: Methods or Namespace
- Click OK
- Double-click to open Code Editor
Built-in Classes
Class | Purpose | Domain |
---|---|---|
ServerMain | Methods for all server tasks | Server |
ClientMain | Methods for all client displays | Client |
Example Class Method
csharp
public class ServerMain
{
public double CalculateEfficiency(double input, double output)
{
if (input == 0) return 0;
return (output / input) * 100;
}
public void LogEvent(string message)
{
@Alarm.AuditTrail.AddCustomMessage(message);
}
}
Step 2: Create ScriptTasks
ScriptTasks execute code based on triggers or schedules.
Creating a Task
- Go to Scripts → Tasks
- Click Plus icon
- Configure:
- Name: Task identifier
- Code: Language selection
- Domain: Server (default) or Client
- Trigger: Execution condition
- Period: Time interval (if periodic)
- Click OK
- Double-click to edit code
Built-in Tasks
Task | Trigger | Purpose |
---|---|---|
Server Startup | Solution starts | Initialize server resources |
Server Shutdown | Solution stops | Cleanup operations |
Client Startup | Client connects | Setup client environment |
Client Shutdown | Client disconnects | Client cleanup |
Trigger Options
Trigger Type | Example | Use Case |
---|---|---|
Tag Change | Tag.Temperature | React to value changes |
Condition | Tag.Level > 100 | Monitor thresholds |
Interval | 00:00:30 | Periodic execution |
Schedule | Daily at 06:00 | Scheduled reports |
Startup | Built-in | Initialization |
Example Task Code
csharp
// Task: MonitorProduction
// Trigger: Tag.ProductionRun (on change)
public void RunTask()
{
if (@Tag.ProductionRun == 1)
{
@Tag.StartTime = DateTime.Now;
@Tag.BatchNumber = @Tag.BatchNumber + 1;
// Call class method
@Script.Class.ServerMain.LogEvent("Production Started");
}
else
{
@Tag.EndTime = DateTime.Now;
TimeSpan duration = @Tag.EndTime - @Tag.StartTime;
@Tag.BatchDuration = duration.TotalMinutes;
}
}
Step 3: Add ScriptExpressions
Expressions are single-line statements for simple operations.
Creating an Expression
- Navigate to Scripts → Expressions
- Add new row with:
Property | Description | Example |
---|---|---|
Object | Target tag | Tag.Result |
Expression | Calculation/action | Tag.Input1 + Tag.Input2 |
Execution | When to run | OnChange |
Trigger | Optional trigger | Tag.Calculate |
Expression Methods
IIF Method (all parameters evaluated):
Tag.Status = IIF(Tag.Value > 100, "High", "Normal")
TIF Method (conditional evaluation):
TIF(Tag.Mode == 1, @Script.Class.Method1(), @Script.Class.Method2())
Common Expression Examples
csharp
// Mathematical
Tag.Average = (Tag.Value1 + Tag.Value2) / 2
// Conditional
Tag.Alarm = Tag.Temperature > Tag.Setpoint
// Method call
@Script.Class.ServerMain.CalculateEfficiency(Tag.Input, Tag.Output)
// String manipulation
Tag.FullName = Tag.FirstName + " " + Tag.LastName
Step 4: Add External References
Adding DLL References
- Go to Scripts → References
- Click Plus icon
- Browse and select DLL file
- Configure:
- Name: Reference identifier
- Domain: Server or Client
- Click OK
DLL Location Best Practices
Location | Macro | Use For |
---|---|---|
ThirdParty folder | _ThirdParty_ | Server-side DLLs |
WpfControls folder | _WpfControls_ | Client-side controls |
Solution folder | _SolutionPath_ | Solution-specific DLLs |
Adding Namespaces
- Open Code Editor
- Click Namespace Declarations button
- Add using/import statements:
csharp
using System.Data;
using MyCustomLibrary;
Step 5: Test and Debug
Enable Debugging
- Go to Runtime → Build and Publish
- Enable Debug Information
- Save solution
Using the Debugger
- Start runtime (F5)
- Go to Runtime → Startup
- Click Connect
- Open script in Code Editor
- Click Attach .NET Debugger
- Set breakpoints by clicking line numbers
- Use step controls when stopped
Monitoring Execution
Check script performance:
csharp
// In expressions or displays
@Script.Task.MyTask.ExecutionCount // Number of executions
@Script.Task.MyTask.LastCPUTime // Last execution time
Code Editor Features
IntelliSense
- Type
.
after objects for property list - Automatic method completion
- Parameter hints
Productivity Tools
Tool | Shortcut | Purpose |
---|---|---|
Format Document | Toolbar | Auto-format code |
Comment | Ctrl+K,C | Comment selection |
Uncomment | Ctrl+K,U | Uncomment selection |
Compile | F6 | Check for errors |
Toolkit Methods
Common operations via TK
namespace:
csharp
// Type conversion
double value = TK.ConvertTo<double>("123.45");
// Tag to DataTable
DataTable dt = TK.CopyTagToDataTable(@Tag.MyTemplate);
// Dynamic property access
object val = TK.GetPropertyValue(@Tag.MyTag, "Value");
Common Issues
Script Not Executing
- Check trigger configuration
- Verify InitialState is Enabled
- Review BuildStatus for errors
- Confirm domain (Server/Client)
Compilation Errors
- Check BuildErrors column
- Verify references resolved
- Review namespace declarations
- Ensure language syntax correct
DLL Not Found
- Use macros instead of absolute paths
- Place in correct folder (ThirdParty/WpfControls)
- Restart script module after DLL update
- Check Resolved status in References
Performance Issues
- Monitor LastCPUTime property
- Avoid infinite loops
- Use async operations for long tasks
- Optimize database queries
Best Practices
? Use descriptive names - Clear task and class identification
Handle exceptions - Try-catch blocks for error handling
Reuse code - Create methods in classes
Comment complex logic - Document purpose and parameters
Test incrementally - Verify each script separately
Monitor performance - Check execution times
Avoid circular references - Classes shouldn't reference each other
Script Examples
Data Calculation Task
csharp
// Runs every 1 minute
public void CalculateKPIs()
{
double production = @Tag.ProductionCount;
double runtime = @Tag.RuntimeHours;
if (runtime > 0)
{
@Tag.ProductionRate = production / runtime;
@Tag.Efficiency = @Script.Class.ServerMain.
CalculateEfficiency(@Tag.PlannedProduction, production);
}
}
Alarm Response Expression
// Object: Tag.AlarmActive
// Expression:
TIF(Tag.Critical == 1, @Script.Class.ServerMain.SendAlert(), 0)
Client Display Initialization
csharp
// Client Startup Task
public void InitializeClient()
{
@Display.MainPage.UserLabel = @Client.UserName;
@Display.MainPage.LoginTime = DateTime.Now;
}
Next Steps
- [Code Behind →] Scripts in displays
- [Python Integration →] Configure Python scripts
- [API Integration →] External system connections
In this section...
Page Tree | ||||
---|---|---|---|---|
|