qr
fistfulofbytes

OPC Sample Code

10 October 2013 by Sevki

This article is briefly describes how to setup a sample application that works with OPC clients.

OPC

My personal opinion is that OPC is a horrible platform and people resposible for it should be hunted down and killed alongside one direction.

Setup

// Declare a Server first.
String OPCServerName = "{Here comes your OPC s name}";
ObjOPCServer = new OPCServer();
ObjOPCServer.Connect(OPCServerName, "";);
// Then a Object group because to add an item changed event.
ObjOPCGroups = ObjOPCServer.OPCGroups;
ObjOPCGroup = ObjOPCGroups.Add("OPCGroup1");
// The item changed event.
ObjOPCGroup.DataChange += new
DIOPCGroupEvent_DataChangeEventHandler(ObjOPCGroup_DataChange);
// Now the Iteem
ObjOPCGroup.OPCItems.AddItem("{tag name or address (like {plc name on server}!%mw0)}", 1);
// Update rate and other miscellaneous
ObjOPCGroup.UpdateRate = 10;
ObjOPCGroup.IsActive = true;
ObjOPCGroup.IsSubscribed = true;

Datachange Event Handler

private void ObjOPCGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
    for (int i = 1; i <= NumItems; i++){
        if ((Convert.ToInt32(ClientHandles.GetValue(i)) == 1)) {
            textBox1.Text = ItemValues.GetValue(i).ToString();
        }
    }
}

I get a lot of "where do I download this magic dll" type questions, just do a quick Interop.OPCAutomation.dll search on ddg.

Full sample app

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OPCAutomation;
namespace OPC
{
    public partial class Form1 : Form
    {
        OPCServer ObjOPCServer;
        OPCGroups ObjOPCGroups;
        OPCGroup ObjOPCGroup;
        string OPCServerName;
        public Form1()
        {
            try
            {
                InitializeComponent();
                OPCServerName = "{Here comes your OPC server’s name}";
                ObjOPCServer = new OPCServer();
                ObjOPCServer.Connect(OPCServerName, "";);
                ObjOPCGroups = ObjOPCServer.OPCGroups;
                ObjOPCGroup = ObjOPCGroups.Add("OPCGroup1");
                ObjOPCGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(ObjOPCGroup_DataChange);
                ObjOPCGroup.OPCItems.AddItem("{tag name or address (like {plc name on server}!%mw0)}", 1);
                ObjOPCGroup.UpdateRate = 10;
                ObjOPCGroup.IsActive = true;
                ObjOPCGroup.IsSubscribed = true;
            }
            catch (Exception e){
                MessageBox.Show(e.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void ObjOPCGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
        {
            for (int i = 1; i <= NumItems; i++)
            {
                if ((Convert.ToInt32(ClientHandles.GetValue(i)) == 1))
                {
                    textBox1.Text =  ItemValues.GetValue(i).ToString();
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            OPCServerClass GlobalOPCServer = new OPCServerClass();
            Array ServerList = (Array)GlobalOPCServer.GetOPCServers("");
            for (int i = 1; i <= ServerList.Length; i++)
            {
                comboBox1.Items.Add(ServerList.GetValue(i).ToString());
            }
        }
    }
}