opc ua client c# open source code example

Example: opc ua client c#

//client
using System;
using System.Threading;
 
using Opc.UaFx.Client;
 
public class Program {
  public static void Main()
  {
    using (var client = new OpcClient("opc.tcp://localhost:4840"))
    {
      client.Connect();

      while (true) {
        var temperature = client.ReadNode("ns=2;s=Temperature");
        Console.WriteLine("Current Temperature is {0} °C", temperature);
 
        Thread.Sleep(1000);
      }
    }
  }
}

//server
using System.Threading;

using Opc.UaFx;
using Opc.UaFx.Server;

class Program
{
  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);
      }
    }
  }
}
// for siemens opc ua read also =>
// https://cache.industry.siemens.com/dl/files/088/42014088/att_954892/v1/42014088_OPC_UAClient_DOKU_V12_de.pdf