private OpcDataVariableNode <T> CreateNode <T>( OpcFolderNode nodes, T defaultValue, Func <T, int, T> randomizer = null) { var name = typeof(T).Name; name = char.ToUpper(name[0]) + name.Substring(1); if (typeof(T).IsArray) { name = typeof(T).GetElementType().Name + "Array"; } var node = new OpcDataVariableNode <T>(nodes, name, defaultValue); node.ReadVariableValueCallback = this.HandleReadVariableValue; node.WriteVariableValueCallback = this.HandleWriteVariableValue; if (randomizer != null) { this.tasks.Add(() => { node.Value = randomizer(node.Value, this.valueRandom.Next(0, 183)); node.ApplyChanges(this.SystemContext); }); } return(node); }
private void TemperatureMaker() { // _temperatureNode = new OpcDataVariableNode<double>("Temperature", 100.0); while (true) { if (_temperatureNode.Value == 110) { _temperatureNode.Value = 100; } else { _temperatureNode.Value++; } _temperatureNode.ApplyChanges(_server.SystemContext); Thread.Sleep(1000); } }
private static void ProduceDataChanges(object state) { var server = (OpcServer)state; while (!producerControl.IsCancellationRequested) { lock (state) { dataAvailableNode.Value = false; dataAvailableNode.ApplyChanges(server.SystemContext); unchecked { foreach (var node in dataNodes) { node.Value++; } } dataProcessedNode.Value = false; dataAvailableNode.Value = true; timestampNode.Value = DateTime.UtcNow; dataNode.ApplyChanges(server.SystemContext, recursive: true); } try { do { Console.WriteLine("{0} HANDSHAKE: New Data available - awaiting data being processed.", DateTime.Now); } while (!dataProcessed.Wait(Interval, producerControl.Token)); Console.WriteLine("{0} HANDSHAKE: Completed - processing for {1} ms.", DateTime.Now, Interval); // Wait for "Stop" or perform next handshake changes after interval elapsed if (producerControl.Token.WaitHandle.WaitOne(Interval)) { break; } } catch (OperationCanceledException) { break; } } }
public static void Main() { var temperatureNode = new OpcDataVariableNode <double>("Temperature", 100.0); using (var server = new OpcServer("opc.tcp://localhost:4840/", temperatureNode)) { server.Start(); while (true) { if (temperatureNode.Value == 110) { temperatureNode.Value = 100; } else { temperatureNode.Value++; } temperatureNode.ApplyChanges(server.SystemContext); Thread.Sleep(1000); } } }
private static void RunMachine(object state) { var context = (OpcContext)state; var eventNode = new OpcEventNode("State"); var eventSourceNode = default(OpcNode); while (!machineControl.IsCancellationRequested) { var time = DateTime.Now.TimeOfDay; temperatureNode.Value = time.Seconds + time.Milliseconds * 0.1; temperatureNode.ApplyChanges(context); speedNode.Value = time.Seconds * 1000; speedNode.ApplyChanges(context); if (time.Seconds % 11 == 0) { eventSourceNode = speedNode; eventNode.Severity = OpcEventSeverity.Medium; } else { eventSourceNode = temperatureNode; if (temperatureNode.Value <= 10) { eventNode.Severity = OpcEventSeverity.Low; } else if (temperatureNode.Value <= 20) { eventNode.Severity = OpcEventSeverity.MediumLow; } else if (temperatureNode.Value <= 30) { eventNode.Severity = OpcEventSeverity.Medium; } else if (temperatureNode.Value <= 40) { eventNode.Severity = OpcEventSeverity.MediumHigh; } else if (temperatureNode.Value <= 50) { eventNode.Severity = OpcEventSeverity.High; } else { eventNode.Severity = OpcEventSeverity.Max; } } eventNode.SourceName = eventSourceNode.SymbolicName; eventNode.SourceNodeId = eventSourceNode.Id; var eventData = eventNode.CreateEvent(context); eventSourceNode.ReportEvent(context, eventData); // Wait for "StopMachine" or perform next steps after interval elapsed if (machineControl.Token.WaitHandle.WaitOne(2500)) { break; } } }
public static void Main(string[] args) { if (args == null || args.Length == 0) { Console.WriteLine("Enter a valid ip address for the server (or localhost)"); return; } string serverIP = args[0]; #region 1st Way: Use the OpcServer class. { //// The OpcServer class interacts with one or more OPC UA clients using one of //// the registered base addresses of the server. While this class provides the //// different OPC UA services defined by OPC UA, it does not implement a main loop. //string serverURL = "https://localhost:4840/SampleServer"; //string serverURL = "opc.tcp://localhost:4840/SampleServer"; string serverURL = $"opc.tcp://{serverIP}:4840/SampleServer"; var temperatureNode = new OpcDataVariableNode <double>("Temperature", 100.0); //using var server = new OpcServer(serverURL, temperatureNode); using var server = new OpcServer(serverURL, temperatureNode); server.Start(); Console.WriteLine("Started the server at {0}", serverURL); while (true) { if (temperatureNode.Value == 110) { temperatureNode.Value = 100; } else { temperatureNode.Value++; } temperatureNode.ApplyChanges(server.SystemContext); Thread.Sleep(1000); } } #endregion #region 2nd Way: Use the OpcServerApplication class. { // The OpcServerApplication class uses a single OpcServer instance which is // wrapped within a main loop. // // Remarks // - The app instance does start a main loop when the server has been started. // - Custom startup code have to be implemented within the event handler of the // Started event of the app instance. // new OpcServerApplication("opc.tcp://localhost:4840/SampleServer", new SampleNodeManager()).Run(); } #endregion #region 3rd Way: Use the OpcServerServiceApplication class. { //// The OpcServerServiceApplication class uses a single OpcServer instance which is //// wrapped within a main loop when it is started with an interactive user or in //// debug mode. Otherwise it will start the process as a windows service which //// allows the application can be registered as a service process. //// //// Remarks //// - The app instance does start a main loop when the server has been started. //// - Custom startup code have to be implemented within the event handler of the //// Started event of the app instance. //new OpcServerServiceApplication("opc.tcp://localhost:4840/SampleServer", new SampleNodeManager()).Run(); } #endregion }