Nell’esempio riportato sotto viene mostrato come creare una applicazione che mediante i componenti iTools® pubblica i valori di un certo numero di variabili, in questo caso due, mediante il protocollo OPC UA.
La pubblicazione delle variabili iTools® avviene mediante la creazione di due componenti IOServer, dove uno funge da sorgente (“Simulation”) e uno da destinazione (OpcUAServer), il collegamento tra il Group (“group1”) e l’IOServer (“ioserver2”) permette di pubblicare le variabili iTools® mediante il protocollo OPC UA.
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 |
using System; using System.Collections.Generic; namespace OpcUAServerSample { using IToolS.Components.Communication; using IToolS.Components.IOServers; static class Program { static void Main() { IToolS.Components.ComponentBase.RaiseEventsOnMainThread = false; Group group = new Group() { GroupName = "group1" }; Variable variable1 = new Variable() { VariableName = "variable1", Area = "HR", Address = "0" }; Variable variable2 = new Variable() { VariableName = "variable2", Area = "HR", Address = "1" }; variable1.Changed += variableChanged; variable2.Changed += variableChanged; IOServer ioserver1 = new IOServer() { Name = "Simulation" }; IOServer ioserver2 = new IOServer() { Name = "OpcUAServer" }; Client client = new Client() { ClientName = "client1", Group = group, IOServer = ioserver1 }; group.Add(variable1); group.Add(variable2); group.IOServer = ioserver2; client.Start(); group.StartIOServer(); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); client.Stop(); client.StopIOServer(); group.StopIOServer(); } private static void variableChanged(object sender, IToolS.Data.ChangedEventArgs e) { Variable variable = (Variable)sender; Console.WriteLine("{0} value: {1}", variable.VariableName, e.NewValue); } } } |