You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Add calculations, business logic, and data transformations using FrameworX's scripting capabilities. Create expressions, tasks, and classes for advanced functionality.



What You'll Learn

  • Write tag expressions
  • Create script tasks
  • Build script classes
  • Use multiple languages (C#, VB.NET, Python)

Prerequisites

  • Basic programming knowledge
  • Tags configured in UNS

Step 1: Simple Tag Expressions

  1. Navigate to Unified Namespace → Tags
  2. Add calculated tag:

Temperature Conversion:

  • Name: Tank1_TempF
  • Expression: @Tag.Tank1_TempC * 9/5 + 32
  • Type: Double
  • Description: Temperature in Fahrenheit

Conditional Logic:

  • Name: System_Status
  • Expression: @Tag.Motor1_Running AND @Tag.Pump1_Running ? "Running" : "Stopped"
  • Type: Text

Step 2: Create Script Task

  1. Go to Scripts → Tasks
  2. Add new task:

Production Counter:

csharp

// Reset counter at shift change
DateTime now = DateTime.Now;
if (now.Hour == 6 || now.Hour == 14 || now.Hour == 22)
{
    if (@Tag.ShiftReset == false)
    {
        @Tag.Units_Previous = @Tag.Units_Produced;
        @Tag.Units_Produced = 0;
        @Tag.ShiftReset = true;
    }
}
else
{
    @Tag.ShiftReset = false;
}

Configure:

  • Trigger: Timer
  • Rate: 60000ms (1 minute)
  • Domain: Server

Step 3: Build Script Class

  1. Navigate to Scripts → Classes
  2. Create class:

TankCalculations:

csharp

public class TankCalculations
{
    public static double CalculateVolume(double level, double diameter)
    {
        double radius = diameter / 2;
        return Math.PI * radius * radius * level;
    }
    
    public static double FlowRate(double volumeStart, double volumeEnd, int minutes)
    {
        return (volumeEnd - volumeStart) / minutes;
    }
    
    public static string GetAlarmText(double value, double limit)
    {
        if (value > limit)
            return $"High: {value:F2} (Limit: {limit:F2})";
        else
            return "Normal";
    }
}

Step 4: Use Class in Expressions

In tag expressions:

csharp

@Script.Class.TankCalculations.CalculateVolume(@Tag.Tank1_Level, 10.5)

Step 5: Python Integration

  1. Scripts → Settings
  2. Enable Python
  3. Create Python script:

python

import numpy as np
from datetime import datetime

def calculate_statistics(values):
    """Calculate statistical values"""
    arr = np.array(values)
    return {
        'mean': np.mean(arr),
        'std': np.std(arr),
        'max': np.max(arr),
        'min': np.min(arr)
    }

def predict_value(history, hours_ahead):
    """Simple linear prediction"""
    # Implementation here
    return predicted_value

# Set tag values
tags['Stats_Mean'] = calculate_statistics(history)['mean']

Step 6: Event-Driven Scripts

Create event handler:

csharp

// On tag change event
public void OnTemperatureChange()
{
    if (@Tag.Tank1_Temperature > @Tag.HighLimit)
    {
        @Tag.Alarm_Active = true;
        @Script.Class.Notifications.SendEmail(
            "operator@company.com",
            "High Temperature Alert",
            $"Tank 1: {@Tag.Tank1_Temperature}°C"
        );
    }
}

Bind to tag:

  • Tag: Tank1_Temperature
  • Event: OnValueChange
  • Script: OnTemperatureChange

Step 7: Database Operations

Script for database logging:

csharp

public void LogProduction()
{
    string query = @"
        INSERT INTO ProductionLog 
        (Timestamp, Product, Quantity, Operator) 
        VALUES 
        (@time, @product, @qty, @user)";
    
    @Dataset.DB.SqlExecuteCommand(query,
        "@time", DateTime.Now,
        "@product", @Tag.Current_Product,
        "@qty", @Tag.Units_Produced,
        "@user", @Client.UserName
    );
}

Step 8: Test Scripts

  1. Start Runtime with debugger
  2. Set breakpoints in scripts
  3. Monitor script execution:
    • Scripts → Monitor
    • Check execution time
    • View error messages

Best Practices

  • Handle exceptions properly
  • Log important events
  • Optimize for performance
  • Comment complex logic
  • Test edge cases
  • Use meaningful variable names

Next Steps

  • [SQL Database Query] - Database integration
  • [Reports Module] - Automated reporting
  • [Create Dashboards] - Display calculations


  • No labels