public bool AddItem(Tag bi) { try { #region *********集合已包含此Tag,则退出************************* foreach (Tag item in this.Tags) { if (item.TagName == bi.TagName) { MessageBox.Show("Tag重复:在在重复的Tag点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } } #endregion OPCItems items = this.OPCGroup.OPCItems;//根据指定的组类别,取出组下的标签集合 Func <Tag, int> f = p => { if (p != null) { return(p.ExtraAs <DaExtra>().ItmHandleClient + 1); } return(0); }; int itmHandleBegin = f(Tags.LastOrDefault());//itmHandleBegin的逻辑为最后一个标签itmHandle值+1 //关键,此步骤将标签添加入OPCclient的监听列表中 OPCItem ki = items.AddItem(bi.OpcTagName, itmHandleBegin); bi.ExtraAs <DaExtra>().ItmHandleClient = itmHandleBegin; //bi.DataTypeName = DaOpcTypes.GetEnumTypeRemarkByValue(ki.CanonicalDataType); bi.ExtraAs <DaExtra>().ItmHandleServer = ki.ServerHandle; Tags.Add((Tag)(Object)bi); AddItemsComplete?.Invoke(this); return(true); } catch (Exception) { throw; } }
public IGroup AddItems(List <Tag> biList) { try { #region 入的标签集合为空时,直接返回 if (biList.Count == 0) { return(this); } #endregion #region 当前标签集合已包含此Tag,则退出 if (Tags.Count(p => biList.Select(b => b.TagName).Contains(p.TagName)) != 0) { MessageBox.Show("Tag重复:在在重复的Tag点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(this); } #endregion //根据组名,取出组下的子集合 OPCItems items = this.OPCGroup.OPCItems; int itmHandleBegin; int NUMITEMS; Array OPCItemIDs; Array ItemClientHandles; Array ItemServerHandles; Array AddItemServerErrors; Func <Tag, int> f = (p => p != null ? p.ExtraAs <DaExtra>().ItmHandleClient : 0); itmHandleBegin = f(Tags.LastOrDefault()); //itmHandleBegin的逻辑为最后一个标签itmHandle值 NUMITEMS = biList.Count() + 1; //OPCitem集合元素是以1为数组的基数,例:如果新增5个标签的话,就需要创建一个长度为6的标签, OPCItemIDs = Array.CreateInstance(typeof(string), NUMITEMS); ItemClientHandles = Array.CreateInstance(typeof(int), NUMITEMS); ItemServerHandles = Array.CreateInstance(typeof(int), NUMITEMS); AddItemServerErrors = default(System.Array); //标签赋值后加入daTags for (int i = 1; i < NUMITEMS; i++) { OPCItemIDs.SetValue(biList[i - 1].OpcTagName, i); ItemClientHandles.SetValue(itmHandleBegin + i, i); biList[i - 1].ExtraAs <DaExtra>().ItmHandleClient = itmHandleBegin + i; Tags.Add((Tag)(Object)biList[i - 1]); } //关键,此步骤将标签添加入OPCclient的监听列表中 items.AddItems(biList.Count, ref OPCItemIDs, ref ItemClientHandles, out ItemServerHandles, out AddItemServerErrors); #region ***********判断tag点是否正常监听************ for (short i = 1; i < NUMITEMS; i++) { if ((int)AddItemServerErrors.GetValue(i) == 0) //If the item was added successfully then allow it to be used. { Tags[i - 1].Message = "OPC Add Item Successful"; Tags[i - 1].Enabled = true; //设置标签的数据类型及ServerHandle OPCItem ki = items.GetOPCItem((int)ItemServerHandles.GetValue(i)); //biList[i - 1].DataTypeName = DaOpcTypes.GetEnumTypeRemarkByValue(ki.CanonicalDataType); biList[i - 1].DataType = DaOpcTypes.GetEnumTypeByValue(ki.CanonicalDataType); biList[i - 1].ExtraAs <DaExtra>().ItmHandleServer = (int)ItemServerHandles.GetValue(i); } else { Tags[i - 1].Message = "OPC Add Item Fail"; Tags[i - 1].Enabled = false; //ItemServerHandles(i) = 0; // If the handle was bad mark it as empty //OPCItemValue(i).Enabled = false; //OPCItemValue(i).Text = "OPC Add Item Fail"; } } #endregion AddItemsComplete?.Invoke(this); return(this); } catch (Exception e) { throw; } }