private void ObjOPCGroup_AsyncReadComplete(int TransactionID, int NumItems, ref System.Array ClientHandles, ref System.Array ItemValues, ref System.Array Qualities, ref System.Array TimeStamps, ref System.Array Errors) { try { int index = 0; for (index = 1; index <= NumItems; index++) { List_Values[(int)ClientHandles.GetValue(index) - 1] = ItemValues.GetValue(index); // send to Tag Tags[List_TagName[(int)ClientHandles.GetValue(index) - 1]].TagValue = ItemValues.GetValue(index); //Array_Values(ClientHandles(index)) = index } if (DataArrivalEvent != null) { Evargs = new OPCDataUpdatedEventArgs(); for (int i = 0; i < List_Values.Count; i++) { if (!Tag.ValueEqual(Saved_List_Values[i], List_Values[i])) { // value update Saved_List_Values[i] = List_Values[i]; Evargs.AddUpdateTag(List_TagName[i], List_Values[i]); } } DataArrivalEvent(this, Evargs); } //if (ContinueReading) //{ // AsyncReadAllData(); // LOG.Info(string.Format("{0} read again.", SourceName)); //} } catch (Exception ex) { LOG.Error(string.Format("OPCDataSource异步读取出错:{0},", ex.Message)); //WarnLog("clsDeviceOPC.ObjOPCGroup_AsyncReadComplete() " + ex.Message); } }
private bool SyncReadAllData() { int liCounter = 0; Array gsErrors = null; try { //if (!Connected) // return false; if (ObjOPCGroup == null) { return(false); } //int[] tempArray_ServerHanlers = new int[NoOfItems]; //Array_ServerHandlers.CopyTo(tempArray_ServerHanlers, 1); //'ObjOPCGroup.IsSubscribed = True object tempQualities = new object(); object timestamps = new object(); ObjOPCGroup.SyncRead((short)OPCAutomation.OPCOPCDevice, NoOfItems, ref Array_ServerHandlers, out Array tempArray_Values, out gsErrors, out tempQualities, out timestamps); bool HasError = false; for (liCounter = 1; liCounter <= NoOfItems; liCounter++) { if ((int)gsErrors.GetValue(liCounter) == 0) { //Array_ServerHandlers.SetValue((int)tempArray_Values.GetValue(liCounter), liCounter); List_Values[liCounter - 1] = tempArray_Values.GetValue(liCounter); // send to Tag Tags[List_TagName[liCounter - 1]].TagValue = List_Values[liCounter - 1]; } else { //WarnLog("clsDeviceOPC.StartOPCClient(): Tag \'" + Array_Items[liCounter] + "\' has problem"); HasError = true; } } //if (!HasError) //{ // //MessageLog("clsDeviceOPC.StartOPCClient(): Connected to OPC device \'" + DeviceName + "\'"); // LOG.Error(" Connected to OPC group \'" + SourceName + "\'"); //} // 数据变化通知事件 if (DataArrivalEvent != null) { Evargs = new OPCDataUpdatedEventArgs(); for (int i = 0; i < List_Values.Count; i++) { if (List_Values[i] == null || Saved_List_Values[i] == null) {//如果空值则不比较 David 20170705 continue; } if (!Tag.ValueEqual(Saved_List_Values[i], List_Values[i])) { // value update Saved_List_Values[i] = List_Values[i]; Evargs.AddUpdateTag(List_TagName[i], List_Values[i]); } } DataArrivalEvent(this, Evargs); } return(!HasError); } catch (Exception ex) { //WarnLog("clsDeviceOPC.SyncReadAllData() " + ex.Message) LOG.Error(string.Format("OPCDataSource同步读取出错:{0},", ex.Message)); return(false); } }