Create Script Expressions, Tasks and Classes.
Tutorials → Scripts | Tutorial | How-to Guide | Reference
This Tutorial Teaches you to:
- Create calculated tags using expressions
- Build reusable logic with Script Classes
- Automate processes with Script Tasks
- Handle events and data transformations
- Monitor script performance and debugging
Prerequisites:
- Designer Tutorial
- Basic understanding of C# or VB.NET syntax
1. Create Tag Expressions
- Navigate to Scripts → Expressions
- Add temperature conversion:
| Field | Value |
|---|---|
| Object | Tag.Tank1_TempF (create as Double) |
| Expression | Tag.Tank1_Temp * 1.8 + 32 |
| Execution | OnChange |
| Domain | Server |
Add status logic:
| Field | Value |
|---|---|
| Object | Tag.System_Status (create as String) |
| Expression | TIF(Tag.Tank1_Status = 2, "Running", "Stopped") |
| Execution | OnChange |
| Domain | Server |
2. Create Script Task
- Go to Scripts → Tasks
- Click plus icon, name:
ShiftCounter - Choose language CSharp
- Press OK
- Use
Server.DateTimeInfo.Houras Trigger - Double-click to open Code Editor
- Enter code (no method wrapper):
// Reset counter at shift change
int hour = @Server.DateTimeInfo.Hour;
if (hour == 6 || hour == 14 || hour == 22)
{
@Tag.Units_Previous = @Tag.Units_Produced;
@Tag.Units_Produced = 0;
}
3. Build Script Class
- Navigate to Scripts → Classes
- Create class:
TankCalculations - Enter methods (no class declaration):
public double CalculateVolume(double level, double diameter)
{
double radius = diameter / 2;
return Math.PI * radius * radius * level;
}
public double CalculateFlowRate(double volumeStart, double volumeEnd, int minutes)
{
if (minutes == 0) return 0;
return (volumeEnd - volumeStart) / minutes;
}
public string GetStatusText(double value, double limit)
{
if (value > limit)
return $"High: {value:F2} (Limit: {limit:F2})";
else
return "Normal";
}4. Use Class in Expression
- Go to Scripts → Expressions
- Create volume calculation:
| Field | Value |
|---|---|
| Object | Tag.Tank1_Volume |
| Expression | Script.Class.TankCalculations.CalculateVolume(Tag.Tank1_Level, 10.5) |
| Execution | OnChange |
Note: No @ symbol needed in expressions
Test and Debug
- Build verification:
- Check green checkmark in BuildStatus
- Review BuildErrors if red X
- Runtime monitoring:
- Go to Scripts → Scripts Monitor
- Check Counter
- Review LastRun
- Debug with breakpoints:
- Follow Code Editor Reference and Debugging Scripts Code with Microsoft Visual Studio to debug the code
→ Modules / Business Operations / Scripts Module → Tutorials / Business Operations / Scripts Module Tutorial → How-to Guides / I Business Operations / Scripts Module How-to Guide → Technical Reference / Business Operations / Scripts Module ReferenceScripts Module Links
Explanation - to understand concepts
Tutorials - to learn by doing
How-to Guides - to accomplish specific tasks
Reference - technical details
In this section...