Scripts & Data Enrichment (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:
- Complete Solution (Tutorial) - uses TankFarm tags
- Basic understanding of C# or VB.NET syntax
In this page:
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 |
Create Script Task
- Go to Scripts → Tasks
- Click plus icon, name:
ShiftCounter
Use Server.DateTimeInfo.Hour as Trigger
- Double-click to open Code Editor
- Enter code (no method wrapper):
csharp
// 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;
}
Build Script Class
- Navigate to Scripts → Classes
- Create class:
TankCalculations
- Enter methods (no class declaration):
csharp
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";
}
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
Event-Driven Task
- Create task:
TemperatureMonitor
- Configure trigger on tag change:
csharp
if (@Tag.Tank1_Temp > @Tag.HighLimit)
{
@Tag.Alarm_Active = true;
@Tag.Alarm_Message = $"High Temperature: {@Tag.Tank1_Temp:F1}°C";
@Tag.Alarm_Time = DateTime.Now;
// Log to historian
@Script.Class.ServerMain.LogEvent(
"Temperature Alarm",
@Tag.Tank1_Temp
);
}
Configure:
- Trigger: Tag.Tank1_Temp
- Domain: Server
Add External References
- Go to Scripts → References
- Add DLL if using external libraries
- Open Code Editor
- Click Namespace Declarations
- Add required namespaces
Test and Debug
- Build verification:
- Check green checkmark in BuildStatus
- Review BuildErrors if red X
- Runtime monitoring:
- Go to Scripts → Monitor
- Check ExecutionCount
- Review LastCPUTime
- Debug with breakpoints:
- Enable debug in Runtime → Build and Publish
- Set breakpoints in Code Editor
- Attach debugger during runtime
Step-by-step guide to add calculations, automation, and data enrichment to your TankFarm solution using Scripts module capabilities.
In this section...