Exemple #1
0
        public bool AddItems(List <Tag> biList)
        {
            try
            {
                #region  入的标签集合为空时,直接返回
                if (biList.Count == 0)
                {
                    return(false);
                }
                #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(false);
                }


                #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].DataType = Enum.GetName(typeof(OpcTypes.CanonicalDataTypesForDa), 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
                return(true);
            }
            catch (Exception e)
            {
                throw;
            }
        }
        /// <summary>
        /// Adresleri Ekler...
        /// </summary>
        public void OPCAddItems(string groupname)
        {
            SetConnectedGroup(groupname);

            if ((ConnectedGroup != null))
            {
                List <OpcItems> grupitems = OPCItem.FindAll(p => p == null || p.OpcGroupName == groupname || p.OpcGroupName == "lks" + groupname);

                try
                {
                    int ItemCount = grupitems.Count - 1;

                    //' Array for potential error returns.  This example doesn't
                    //' check them but yours should ultimately.
                    System.Array AddItemServerErrors;
                    if (OPCItemIDs.Count == 0)
                    {
                        OPCItemIDs.Add(null);
                        OPCItemIsArray.Add(0);
                        ClientHandles.Add(0);
                    }
                    //' Load the request OPC Item names and build the ClientHandles list
                    for (int i = 1; i < grupitems.Count; i++)
                    {
                        //' Load the name of then item to be added to this group.  You can add
                        //' as many items as you want to the group in a single call by building these
                        //' arrays as needed.
                        List <OpcItems> item = grupitems.FindAll(p => p != null && p.GrupAdresNo == i);

                        OPCItemIDs.Add(item[0].OPCItemName);

                        //' ASSume all aren't an array. If it is, this holds size and is set in
                        //' Data change event.
                        OPCItemIsArray.Add(0);

                        //' The client handles are given to the OPC Server for each item you intend
                        //' to add to the group.  The OPC Server will uses these client handles
                        //' by returning them to you in the 'DataChange' event.  You can use the
                        //' client handles as a key to linking each valued returned from the Server
                        //' back to some element in your application.  In this example we are simply
                        //' placing the Index number of each control that will be used to display
                        //' data for the item.  In your application the ClientHandle value you use
                        //' can by whatever you need to best fit your program.  You will see how
                        //' these client handles are used in the 'DataChange' event handler.
                        ClientHandles.Add(i);

                        //' Make the Items active start control Active, for the demo I want all items to start active
                        //' Your application may need to start the items as inactive.
                        item[0].OPCItemActiveState = true;
                    }


                    ConnectedGroup.OPCItems.DefaultIsActive = true;
                    System.Array clienthandles = ClientHandles.ToArray <int>();
                    System.Array oPCitemIDs    = OPCItemIDs.ToArray <string>();
                    ConnectedGroup.OPCItems.AddItems(ItemCount, ref oPCitemIDs, ref clienthandles, out ItemServerHandles, out AddItemServerErrors);
                    bool AnItemIsGood;
                    AnItemIsGood = false;

                    for (int i = 1; i < grupitems.Count; i++)
                    {
                        List <OpcItems> item = grupitems.FindAll(p => p != null && p.GrupAdresNo == i);
                        if ((int)AddItemServerErrors.GetValue(i) == 0)                                    //'If the item was added successfully then allow it to be used.
                        {
                            AnItemIsGood = true;
                        }
                        else
                        {
                            ItemServerHandles.SetValue(0, i);                                           // ' If the handle was bad mark it as empty

                            item[0].OPCItemValue = "OPC Add Item Fail";
                        }
                    }
                    for (int i = 1; i < grupitems.Count; i++)
                    {
                        List <OpcItems> item = grupitems.FindAll(p => p != null && p.GrupAdresNo == i);
                        if ((int)AddItemServerErrors.GetValue(i) == 0)                                    //'If the item was added successfully then allow it to be used.
                        {
                            AnItemIsGood = true;
                        }
                        else
                        {
                            ItemServerHandles.SetValue(0, i);                                           // ' If the handle was bad mark it as empty
                            item[0].OPCItemValue = "OPC Add Item Fail";
                        }
                    }
                    //' Disable the Add OPC item button if any item in the list was good
                    Object Response;
                    //if (!AnItemIsGood)
                    //    MessageBox.Show("The OPC Server has not accepted any of the item you have entered, check your item names and try again.", "OPC Add Item", MessageBoxButtons.OK);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("OPC server add items failed with exception: " + ex.Message, "SimpleOPCInterface Exception", MessageBoxButtons.OK);
                }
            }
        }