How to read values from OPC

Harshit26

Member
Join Date
Sep 2015
Location
India
Posts
1
I am trying to read values from OPC server using Interop.OPCAutomation.dll

Code:
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()
        {
            getData();
        }
        private void getData()
        {
            try
            {
                 int count = 1;
            opcServer.Connect("OPCTechs.SiemensNet30DA", "");

            opcGroup = opcServer.OPCGroups.Add("MP");
            opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup_DataChange);

            //Get First String
            for (int i = 40; i <= 47; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Second String
            for (int i = 80; i <= 91; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Third String
            for (int i = 69; i >= 60; i--)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Fourth String
            for (int i = 200; i <= 224; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Fifth String
            for (int i = 300; i <= 849; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Sixth String
            for (int i = 40; i >= 47; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);


            opcGroup.OPCItems.DefaultIsActive = true;
            opcGroup.UpdateRate = 1000;
            opcGroup.IsSubscribed = opcGroup.IsActive;
            }
            catch (Exception exc)
            {
                 MessageBox.Show(exc.Message, "Alert");
            }
       }
       private void opcGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
       {
            try
            {
                 string temp = "";
            int count = 1;
            for (; count <= 8; count++)
            {
                int ff = Convert.ToInt32(ClientHandles.GetValue(count));
                //if (Convert.ToInt32(ClientHandles.GetValue(count)) == count)
                temp += ItemValues.GetValue(count).ToString();
            }
            Textbox4.Text = temp.ToString();
            temp = "";

            for (; count <= 12; count++)
            {
                if (Convert.ToInt32(ClientHandles.GetValue(count)) == count)
                    temp += ItemValues.GetValue(count).ToString();
            }
            TextBox3.Text = temp.ToString();
            temp = "";

            for (; count <= 12; count++)
            {
                if (Convert.ToInt32(ClientHandles.GetValue(count)) == count)
                    temp += ItemValues.GetValue(count).ToString();
            }
            TextBox2.Text = temp.ToString();
            temp = "";


        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "Alert");
        }
        }
    }
}




 


I am trying to read values from OPC server using Interop.OPCAutomation.dll
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()
        {
            getData();
        }
        private void getData()
        {
            try
            {
                 int count = 1;
            opcServer.Connect("OPCTechs.SiemensNet30DA", "");

            opcGroup = opcServer.OPCGroups.Add("MP");
            opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup_DataChange);

            //Get First String
            for (int i = 40; i <= 47; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Second String
            for (int i = 80; i <= 91; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Third String
            for (int i = 69; i >= 60; i--)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Fourth String
            for (int i = 200; i <= 224; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Fifth String
            for (int i = 300; i <= 849; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Sixth String
            for (int i = 40; i >= 47; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);


            opcGroup.OPCItems.DefaultIsActive = true;
            opcGroup.UpdateRate = 1000;
            opcGroup.IsSubscribed = opcGroup.IsActive;
            }
            catch (Exception exc)
            {
                 MessageBox.Show(exc.Message, "Alert");
            }
       }
       private void opcGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
       {
            try
            {
                 string temp = "";
            int count = 1;
            for (; count <= 8; count++)
            {
                int ff = Convert.ToInt32(ClientHandles.GetValue(count));
                //if (Convert.ToInt32(ClientHandles.GetValue(count)) == count)
                temp += ItemValues.GetValue(count).ToString();
            }
            Textbox4.Text = temp.ToString();
            temp = "";

            for (; count <= 12; count++)
            {
                if (Convert.ToInt32(ClientHandles.GetValue(count)) == count)
                    temp += ItemValues.GetValue(count).ToString();
            }
            TextBox3.Text = temp.ToString();
            temp = "";

            for (; count <= 12; count++)
            {
                if (Convert.ToInt32(ClientHandles.GetValue(count)) == count)
                    temp += ItemValues.GetValue(count).ToString();
            }
            TextBox2.Text = temp.ToString();
            temp = "";


        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "Alert");
        }
        }
    }
}

This code is not returning the values from OPC server. It is giving error on this line
Code:
temp += ItemValues.GetValue(count).ToString();

Error
Code:
Object reference not set to an instance of an object

What wrong I am doing ?
 

Similar Topics

Hello expert; I want to read the tag values from control logix plc over the WiFi connection. I did it by using Rs link OPC and successfully read...
Replies
1
Views
1,166
Good Evening Gentlemen, we have a FT View SE client running in windows 7 system, now I want to update that system with windows 10. I did the...
Replies
2
Views
2,718
Hello i have two systems workering together with a Micrologix 1000, where their are communication between them with a serial cable. Now i have...
Replies
5
Views
2,415
Good morning to all... I am a new bee in C# to GE Fanuc Versamax Pro PLC communication..... So I want to read/write registers values using c#...
Replies
3
Views
3,582
Hai Everybody, I have a doubt about How to read Voltage and Current values from a multifunction meter to Allen Bradley SLC 500 PLC through RS...
Replies
11
Views
3,007
Back
Top Bottom