本人在程序中通过OpcAutomation来访问Opc服务器的变量,本人创建了多个OpcItem项添加到集合中,但是在DataChange事件中只能访问到一项。
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(); } } }
解决方案
80
哎不对..本人的代码好像跟你的不一样….
本人是这样的..
本人是这样的..
subscription.DataChanged += new DataChangedEventHandler(subscription_DataChanged); void subscription_DataChanged(object subscriptionHandle, object requestHandle, ItemValueResult[] values) { }
那么在以上代码中..假如某个”变量”发生了变化.”values”标识变化的值,不发生变化的不在这里体现..
记录的时候 是这样的. .
object msg = DateTime.Now + "发生了DataChanged事件,本次变化的变量长度=" + values.Length + "\n"; for (int i = 0; i < values.Length; i++) { msg += " " + values[i].ItemName + "=" + values[i].Value + "\n"; }
不过可能我们用的组件不太一样吧.不过最终的目的本人觉得都是一样的..
读取变化的”变量”而已..
本人有一个帖子是写了这个的用法..不如你本人找下