public void GetData(ref List <OPCItems> list) { if (TotalItemNumber != list.Count) { opcgrp_arrayQualities = new int[0]; opcgrp_arrayHandles = new Int32[0]; opcgrp_arraySHandles = new int[0]; opcgrp_arrayErrors = new int[0]; opcgrp_arrayValues = new int[0]; opcgrp_arrayPaths = new string[0]; opcgrp_itemVar = new OPCItems[0]; TotalItemNumber = 0; foreach (OPCItems item in list) { AddItem(item.Tag, item.Address, item.Description, item.PLCName); this.Create(); } } this.GetData(); for (int i = 0; i < list.Count; i++) { OPCItems oPCItems = this.GetTagItem(list[i].Tag); if (oPCItems.Quality < 192) { Quality = "Bad"; } if (list[i].Value != oPCItems.Value) { list[i].Value = oPCItems.Value; TransationSum += 1; } list[i].Quality = oPCItems.Quality; } }
public Boolean AddItem(string newTag, string newAddress, string newDescprition, string newPLCName = null) { TotalItemNumber++; opcgrp_arrayPaths = ResizeArray(opcgrp_arrayPaths, TotalItemNumber + 1); opcgrp_arrayHandles = ResizeArray(opcgrp_arrayHandles, TotalItemNumber + 1); opcgrp_itemVar = ResizeArray(opcgrp_itemVar, TotalItemNumber + 1); opcgrp_arrayValues = ResizeArray(opcgrp_arrayValues, TotalItemNumber + 1); opcgrp_arrayErrors = ResizeArray(opcgrp_arrayErrors, TotalItemNumber + 1); opcgrp_arraySHandles = ResizeArray(opcgrp_arraySHandles, TotalItemNumber + 1); opcgrp_arrayQualities = ResizeArray(opcgrp_arrayQualities, TotalItemNumber + 1); OPCItems itemV; itemV = new OPCItems { Tag = newTag.Trim(), Address = newAddress.Trim(), Description = newDescprition.Trim(), PLCName = newPLCName == null?PLCName.Trim() : newPLCName.Trim() }; opcgrp_arrayHandles.SetValue(TotalItemNumber, TotalItemNumber); opcgrp_itemVar.SetValue(itemV, TotalItemNumber); opcgrp_arrayPaths.SetValue(newPLCName.Trim() + newAddress.Trim(), TotalItemNumber); return(true); }
public Boolean GetData() { try { Transacting = true; TransactionFlag = true; //for (; IntData.readingOPC; ) Thread.Sleep(10); if (IntData.IsOPCConnected) { object timestamps = new object(); //store the timestamp of the read object qualities = new object(); IntData.IsOPCConnected = true; opcgrp_server = new OPCServer(); opcgrp_server.Connect(ServerName); opcgrp_group = opcgrp_server.OPCGroups.Add(GroupName); TotalItemNumber = opcgrp_arrayPaths.Length - 1; opcgrp_group.OPCItems.DefaultIsActive = true; opcgrp_group.OPCItems.AddItems(TotalItemNumber, opcgrp_arrayPaths, opcgrp_arrayHandles, out opcgrp_arraySHandles, out opcgrp_arrayErrors); opcgrp_group.SyncRead((short)OPCAutomation.OPCDataSource.OPCDevice, TotalItemNumber, ref opcgrp_arraySHandles, out opcgrp_arrayValues, out opcgrp_arrayErrors, out qualities, out timestamps); var qualitiesList = new List <int>(); var errorsList = new List <int>(); foreach (var quality in (Array)qualities) { qualitiesList.Add(Convert.ToInt32(quality)); } foreach (var error in opcgrp_arrayErrors) { errorsList.Add(Convert.ToInt32(error)); } for (int i = 1; i <= TotalItemNumber; i++) { OPCItems opcitem = new OPCItems(); opcitem = (OPCItems)opcgrp_itemVar.GetValue(i); opcitem.Quality = qualitiesList[i - 1]; //opcitem.Error = errorsList[i - 1]; if (opcgrp_arrayValues.GetValue(i) is System.Int32) { opcitem.Value = (int)opcgrp_arrayValues.GetValue(i); } else if (opcgrp_arrayValues.GetValue(i) is System.Double) { opcitem.Value = (double)opcgrp_arrayValues.GetValue(i); } else { opcitem.Value = opcgrp_arrayValues.GetValue(i).ToString(); } if (opcitem.Quality == 0) { IntData.IsOPCConnected = false; Quality = "Bad"; } opcgrp_itemVar.SetValue(opcitem, i); } if (!IntData.IsOPCConnected) { throw new Exception("The PLC connection is broken!"); } } opcgrp_server.Disconnect(); Transacting = false; //log.Success(); return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } }