我在opc组中创建了5个项目,但我无法在DataChange方法中获取它们

时间:2016-06-29 15:16:57

标签: c# opc

我用OpcAutomation创建一个OpcGroup,然后我添加了5个项目。但是我无法在DataChange方法中获取它们。提示是IndexOutofRange异常。我的问题是如何在这个方法中获取五个变量。谢谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OPCAutomation;
using System.Threading;


namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        #region 私有变量
        /// <summary>
        /// OPCServer Object
        /// </summary>
        OPCServer KepServer;
        /// <summary>
        /// OPCGroups Object
        /// </summary>
        OPCGroups KepGroups;
        /// <summary>
        /// OPCGroup Object
        /// </summary>
        OPCGroup KepGroup;
        /// <summary>
        /// OPCItems Object
        /// </summary>
        OPCItems KepItems;
        /// <summary>
        /// OPCItem Object
        /// </summary>
        OPCItem[] KepItem=new OPCItem[5];
        /// <summary>
        /// 主机IP
        /// </summary>
        string strHostIP = "";
        /// <summary>
        /// 主机名称
        /// </summary>
        string strHostName = "";
        /// <summary>
        /// 连接状态
        /// </summary>
        bool opc_connected = false;
        /// <summary>
        /// 客户端句柄
        /// </summary>
        int itmHandleClient = 0;
        /// <summary>
        /// 服务端句柄
        /// </summary>
        int itmHandleServer = 0;
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            KepServer = new OPCServer();
            try
            {
                KepServer.Connect("KEPware.KEPServerEx.V4", "127.0.0.1");
                KepGroup = KepServer.OPCGroups.Add("Loading");
                SetGroupProperty();
                KepItems = KepGroup.OPCItems;

                itmHandleClient = 1234;
                KepItem[0] = KepItems.AddItem("Channel_0_User_Defined.Sine.Sine1", itmHandleClient);
                itmHandleClient++;
                KepItem[1] = KepItems.AddItem("Channel_0_User_Defined.Sine.Sine2", itmHandleClient);
                itmHandleClient++;
                KepItem[2] = KepItems.AddItem("Channel_0_User_Defined.Sine.Sine3", itmHandleClient);
                itmHandleClient++;
                KepItem[3] = KepItems.AddItem("Channel_0_User_Defined.Sine.Sine4", itmHandleClient);
                itmHandleClient++;
                KepItem[4] = KepItems.AddItem("Channel_0_User_Defined.Sine.Sine5", itmHandleClient);

                Thread.Sleep(1000);
                KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
                //itmHandleServer = KepItem.ServerHandle;
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void SetGroupProperty()
        {
            KepServer.OPCGroups.DefaultGroupIsActive = true;
            KepServer.OPCGroups.DefaultGroupDeadband = 0;
            KepGroup.UpdateRate = 5000;
            KepGroup.IsActive = true;
            KepGroup.IsSubscribed = true;
        }

        private void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
        {
            label1.Text = ItemValues.GetValue(1).ToString();
            label2.Text = ItemValues.GetValue(2).ToString();
            label3.Text = ItemValues.GetValue(3).ToString();
            label4.Text = ItemValues.GetValue(4).ToString();
            label5.Text = ItemValues.GetValue(5).ToString();
        }
    }
}

Error Page

1 个答案:

答案 0 :(得分:0)

我猜这个DataChange事件仅针对已更改的标记触发(此事件在opc规范中以这种方式设计)。所以它可能只包含一个标签,或者两个等。

所以你需要检查NumItems(这是更改的标签的数量),并迭代两者 ClientHandles和Itemvalues。