Monday, February 22, 2021

Sample OPC client using OPC .Net API

 OPC Classic is a common platform/specification implemented in industrial applications. This includes SCADA (supervisory control and data acquisition) and DCS (distributed control systems), building management packages, discrete manufacturing solutions, etc. DCS systems including DeltaV from Emerson, Experion LX from Honeywell, Centum VP from Yokogawa are just few of the big names that implements OPC.

OPC Foundation provides redistributables that can be used to implement OPC clients for free. Access to specifications, sample servers and clients need paid membership to download from the foundation. 

 Create an account in OPC Foundation to download OPC Core Components Redistributables,  As of this writing (2/23/20201) the version available is 3.00.108. Once downloaded, extract the zip file and run OPC Core Components Redistributable (x64) 3.00.108.msi. Note that you may have to remove alternate streams from the downloaded zip file, see this link.Among other things, the installer should have installed nuget packages in C:\Program Files (x86)\OPC Foundation\NuGetPackages, mine looks like below.

Figure 1. Nuget packages

 Assuming you have Visual Studio 2019, if not you can download a trial or community edition from here, create a new C# .Net Framework console application like below. 

Figure 2. Project type

 
Figure 3. Project name and .Net Framework target

Add OPC Foundation Nuget packages via Tools | Options... menu. It should look like below.

Figure 4. OPC Nuget package in Visual Studio

With Nuget package local store added, add them to the project, like:

Figure 5. Bring up Manage Nuget Packages...

Figure 6. Install all 4 packages


Figure 7. Solution explorer shows the 4 packages added to the project

Now add the following code to your main project.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace testopcclient

{

    class Program

    {

        static void Main(string[] args)

        {

            string hostname = "M6"; //machine name or IP

            Opc.URL url = new Opc.URL($"opcda://{hostname}/OPC.DeltaV.1");

            var opc_item = new Opc.Da.Item(new Opc.ItemIdentifier("TESTMOD/SGGN1/OUT.CV"));

            Opc.Da.Server server = new Opc.Da.Server(new OpcCom.Factory(), url);

            server.Connect(new Opc.ConnectData(null, null));


            Console.WriteLine("Esc key to exit");

            do

            {

                while (!Console.KeyAvailable)

                {

                    var result = server.Read(new Opc.Da.Item[] { opc_item });

                    Console.WriteLine($"{result[0].ItemName}:{result[0].Value}");

                    System.Threading.Thread.Sleep(1000);

                }

            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);

            server.Disconnect();

            Console.WriteLine("This should close connection to the server, key to continue");

            Console.ReadKey(true);

        }

    }

}

This assumes that you are using DeltaV OPC Server on machine M6 and that it has a control module with SGGN block generating data.

Have fun!

No comments:

Post a Comment

Sample OPC client using OPC .Net API

  OPC Classic is a common platform/specification implemented in industrial applications. This includes SCADA (supervisory control and data ...