Per implementare di una scalatura personalizzata da applicare al valore di una variabile, e’ necessario implementare l’interfaccia “IToolS.Data.IValueScale”, ad esempio:
1 2 3 4 5 6 7 8 9 10 11 |
class MyScaling : IToolS.Data.IValueScale { public object Scale(IVariableInternal variable, object value) { return Convert.ToInt32(value) * 2; } public object Unscale(IVariableInternal variable, object value) { return Convert.ToInt32(value) / 2; } } |
Quindi impostare la classe di scalatura implementata alla variabile:
1 |
m_variable1.SetValueScale(new MyScaling()); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
using System; using System.Collections.Generic; using System.Text; using IToolS.Components; using IToolS.Components.Communication; using IToolS.Components.IOServers; using IToolS.Data; namespace TestCustomVariableScaling { class Program { static void Main(string[] args) { ComponentBase.RaiseEventsOnMainThread = false; IOServer m_ioServer1 = new IOServer(); Clients m_clients1 = new Clients(); Client m_client1 = new Client(); Group m_group1 = new Group(); Variable m_variable1 = new Variable(); m_ioServer1.Name = "Simulation"; m_clients1.Items.Add(m_client1); m_group1.Items.Add(m_variable1); m_client1.IOServer = m_ioServer1; m_client1.Group = m_group1; m_variable1.Area = "HR"; m_variable1.Address = "0"; m_variable1.SetValueScale(new MyScaling()); m_variable1.Changed += delegate(object sender, ChangedEventArgs e) { Console.WriteLine(e.NewValue); }; m_clients1.Start(); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); } } class MyScaling : IToolS.Data.IValueScale { public object Scale(IVariableInternal variable, object value) { return Convert.ToInt32(value) * 2; } public object Unscale(IVariableInternal variable, object value) { return Convert.ToInt32(value) / 2; } } } |