Harshit
Harshit

Reputation: 5157

OPC DataChangeEventHandler not calling

Below is the code that I used to read OPC values.

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++);

            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 = 1; count <= NumItems; count++)
            {
               if (Convert.ToInt32(ClientHandles.GetValue(count)) == 47)
                    temp += ItemValues.GetValue(count).ToString();
            }
            Textbox1.Text = temp.ToString();
            temp = "";
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "Alert");
        }
}

The problem is that DIOPCGroupEvent_DataChangeEventHandler should call the method opcGroup_DataChange whenever any change in the OPC tag occurs, but it only call the method 3-4 times. After that it doesn't calls. Even when the change occurs, the method doesn't gets called.

What is wrong with this code ?

Upvotes: 0

Views: 696

Answers (1)

Blas Mardones
Blas Mardones

Reputation: 1

add this code

opcGroup.IsActive = true;
opcGroup.IsSubscribed = true;

Upvotes: 0

Related Questions