예제 #1
0
        private EnumString GetEnumerator(string itemID, BrowseFilters filters, bool branches)
        {
            EnumString str2;
            IOPCBrowseServerAddressSpace server = (IOPCBrowseServerAddressSpace)base.m_server;

            try
            {
                server.ChangeBrowsePosition(OPCBROWSEDIRECTION.OPC_BROWSE_TO, (itemID != null) ? itemID : "");
            }
            catch
            {
                try
                {
                    server.ChangeBrowsePosition(OPCBROWSEDIRECTION.OPC_BROWSE_DOWN, (itemID != null) ? itemID : "");
                }
                catch
                {
                    throw new ResultIDException(ResultID.Da.E_UNKNOWN_ITEM_NAME);
                }
            }
            try
            {
                OpcRcw.Da.IEnumString ppIEnumString = null;
                server.BrowseOPCItemIDs(branches ? OPCBROWSETYPE.OPC_BRANCH : OPCBROWSETYPE.OPC_LEAF, (filters.ElementNameFilter != null) ? filters.ElementNameFilter : "", 0, 0, out ppIEnumString);
                str2 = new EnumString(ppIEnumString);
            }
            catch
            {
                throw new ResultIDException(ResultID.Da.E_UNKNOWN_ITEM_NAME);
            }
            return(str2);
        }
예제 #2
0
        private void GetItemChildren(List <OPCItem> items, string szNameFilter, IOPCBrowseServerAddressSpace pParent, OPCItem parentItem = null)
        {
            opcprox.IEnumString pEnum;
            uint   cnt;
            string strName;
            string szItemID;

            try
            {
                pParent.BrowseOPCItemIDs(tagOPCBROWSETYPE.OPC_LEAF, szNameFilter, (ushort)VarEnum.VT_EMPTY, 0, out pEnum);
                pEnum.RemoteNext(1, out strName, out cnt);
                int nLeavesCount = 0;
                while (cnt != 0)
                {
                    pParent.GetItemID(strName, out szItemID); // получает полный идентификатор тега
                    GetItemProperties(szItemID);
                    items.Add(
                        new OPCItem()
                    {
                        Parent   = parentItem,
                        ItemName = strName,
                        Enabled  = true,
                        ItemType = OPCItemType.LEAF,
                        ItemID   = szItemID
                    }
                        );
                    pEnum.RemoteNext(1, out strName, out cnt);
                    nLeavesCount++;
                }
                pParent.BrowseOPCItemIDs(tagOPCBROWSETYPE.OPC_BRANCH, szNameFilter, (ushort)VarEnum.VT_EMPTY, 0, out pEnum);
                pEnum.RemoteNext(1, out strName, out cnt);
                int nBranchesCount = 0;
                while (cnt != 0)
                {
                    pParent.GetItemID(strName, out szItemID);
                    OPCItem item = new OPCItem()
                    {
                        Parent   = parentItem,
                        ItemName = strName,
                        Enabled  = true,
                        ItemType = OPCItemType.BRANCH,
                        ItemID   = szItemID
                    };
                    items.Add(item);
                    pParent.ChangeBrowsePosition(tagOPCBROWSEDIRECTION.OPC_BROWSE_TO, szItemID);
                    GetItemChildren(items, "", pParent, item);
                    pParent.ChangeBrowsePosition(tagOPCBROWSEDIRECTION.OPC_BROWSE_UP, szItemID);
                    pEnum.RemoteNext(1, out strName, out cnt);
                    nBranchesCount++;
                }
            }
            catch (System.Exception ex)
            {
                Console.Out.WriteLine(ex.Message);
            }
        }
예제 #3
0
        /// <summary>
        /// Changes the browse position.
        /// </summary>
        /// <param name="direction">The direction.</param>
        /// <param name="name">The name.</param>
        /// <returns>True if the operation was successful.</returns>
        private bool ChangeBrowsePosition(OPCBROWSEDIRECTION direction, string name)
        {
            string methodName = "IOPCBrowseServerAddressSpace.ChangeBrowsePosition";

            try
            {
                IOPCBrowseServerAddressSpace server = BeginComCall <IOPCBrowseServerAddressSpace>(methodName, true);
                server.ChangeBrowsePosition(direction, name);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL, ResultIds.E_INVALIDARG))
                {
                    ComCallError(methodName, e);
                }

                return(false);
            }
            finally
            {
                EndComCall(methodName);
            }

            return(true);
        }
예제 #4
0
        void ImportOPCChannels(IOPCBrowseServerAddressSpace srv, TreeNodeCollection root)
        {
            OPCNAMESPACETYPE nsType;

            srv.QueryOrganization(out nsType);
            OpcRcw.Da.IEnumString es;
            if (nsType == OPCNAMESPACETYPE.OPC_NS_HIERARCHIAL)
            {
                try{ srv.BrowseOPCItemIDs(OPCBROWSETYPE.OPC_BRANCH, "", 0, 0, out es); }
                catch (COMException) { return; }

                int fetched;
                do
                {
                    string[] tmp = new string[100];
                    es.RemoteNext(tmp.Length, tmp, out fetched);
                    for (int i = 0; i < fetched; i++)
                    {
                        try{ srv.ChangeBrowsePosition(OPCBROWSEDIRECTION.OPC_BROWSE_DOWN, tmp[i]); }
                        catch (Exception e)
                        {
                            Env.Current.Logger.LogWarning(string.Format("OPC server failed to handle OPC_BROWSE_DOWN request for item '{0}' ({1})", tmp[i], e.Message));
                            continue;
                        };
                        TreeNode node = root.Add(tmp[i]);
                        ImportOPCChannels(srv, node.Nodes);
                        try { srv.ChangeBrowsePosition(OPCBROWSEDIRECTION.OPC_BROWSE_UP, ""); }
                        catch (Exception e)
                        {
                            Env.Current.Logger.LogWarning(string.Format("OPC server failed to handle OPC_BROWSE_UP request for item '{0}' ({1})", tmp[i], e.Message));
                            continue;
                        };
                    }
                } while(fetched > 0);

                try{ srv.BrowseOPCItemIDs(OPCBROWSETYPE.OPC_LEAF, "", 0, 0, out es); }
                catch (COMException) { return; }
                IterateOPCItems(srv, root, es);
            }
            else if (nsType == OPCNAMESPACETYPE.OPC_NS_FLAT)
            {
                try { srv.BrowseOPCItemIDs(OPCBROWSETYPE.OPC_FLAT, "", 0, 0, out es); }
                catch (COMException) { return; }
                IterateOPCItems(srv, root, es);
            }
            es = null;
        }
예제 #5
0
        private void OnConnect(object sender, EventArgs e)
        {
            string serverName = serversComboBox.Text;

            Type t;

            if (localServerButton.Checked)
            {
                t = Type.GetTypeFromProgID(serverName);
            }
            else
            {
                t = Type.GetTypeFromProgID(serverName, serverTextBox.Text);
            }

            object obj = Activator.CreateInstance(t);
            IOPCBrowseServerAddressSpace srv = (IOPCBrowseServerAddressSpace)obj;

            //IntPtr statusPtr;
            //server.GetStatus(out statusPtr);
            //OPCSERVERSTATUS status = (OPCSERVERSTATUS)Marshal.PtrToStructure(statusPtr, typeof(OPCSERVERSTATUS));
            //statusPtr = IntPtr.Zero;

            if (srv != null)
            {
                try
                {
                    for (; ;)
                    {
                        srv.ChangeBrowsePosition(OPCBROWSEDIRECTION.OPC_BROWSE_UP, "");
                    }
                }
                catch (COMException) { };
                channelsTree.Nodes.Clear();
                ImportOPCChannels(srv, channelsTree.Nodes);
            }
            channelsTree.AfterCheck += new TreeViewEventHandler(channelsTree_AfterCheck);
            groupBox1.Enabled        = false;
            connectButton.Enabled    = false;
        }
예제 #6
0
		/// <summary>
		/// Получает дерево тегов OPC DA сервера
		/// </summary>
		/// <param name="parentDirectory">При первом вызове передать null - 
		/// будеть создана корневая директория</param>
		/// <param name="server"></param>
		void BuildTreeTags(ref OpcDaDirectory parentDirectory, IOPCBrowseServerAddressSpace server)
		{
			opcproxy.IEnumString ptrEnum;
			uint cnt;
			string strName;
			string szItemID;

			if (parentDirectory == null)
			{
				parentDirectory = new OpcDaDirectory(); // Создаём корневую директорию
			}


			// Получаем все теги
			server.BrowseOPCItemIDs(tagOPCBROWSETYPE.OPC_LEAF, "", (ushort)VarEnum.VT_EMPTY, 0, out ptrEnum);
			ptrEnum.RemoteNext(1, out strName, out cnt);
			while (cnt != 0)
			{
			
				// получаем полный идентификатор тега
				server.GetItemID(strName, out szItemID);
				parentDirectory.Items.Add(new OpcDaTag(szItemID, strName, parentDirectory));
				ptrEnum.RemoteNext(1, out strName, out cnt);
			}

			// Получем директории 
			server.BrowseOPCItemIDs(tagOPCBROWSETYPE.OPC_BRANCH, "", 1, 0, out ptrEnum);
			ptrEnum.RemoteNext(1, out strName, out cnt);
			while (cnt != 0)
			{
				server.ChangeBrowsePosition(tagOPCBROWSEDIRECTION.OPC_BROWSE_DOWN, strName);
				var childDirectory = new OpcDaDirectory(strName, parentDirectory);
				parentDirectory.Items.Add(childDirectory);
				BuildTreeTags(ref childDirectory, server);
				server.ChangeBrowsePosition(tagOPCBROWSEDIRECTION.OPC_BROWSE_UP, strName);
				ptrEnum.RemoteNext(1, out strName, out cnt);
			}
		}
예제 #7
0
 public void ChangeBrowsePosition(OPCBrowseDirection p_Direction, string p_Name)
 {
     m_IfBrowse.ChangeBrowsePosition(p_Direction, p_Name);
 }
예제 #8
0
파일: Server.cs 프로젝트: ZSYMAX/OpcNetApi
        private EnumString GetEnumerator(string itemID, BrowseFilters filters, bool branches, bool flat)
        {
            IOPCBrowseServerAddressSpace iOPCBrowseServerAddressSpace = (IOPCBrowseServerAddressSpace)m_server;

            if (!flat)
            {
                string text = (itemID != null) ? itemID : "";
                try
                {
                    iOPCBrowseServerAddressSpace.ChangeBrowsePosition(OPCBROWSEDIRECTION.OPC_BROWSE_TO, text);
                }
                catch
                {
                    try
                    {
                        iOPCBrowseServerAddressSpace.ChangeBrowsePosition(OPCBROWSEDIRECTION.OPC_BROWSE_DOWN, text);
                    }
                    catch
                    {
                        try
                        {
                            while (true)
                            {
                                iOPCBrowseServerAddressSpace.ChangeBrowsePosition(OPCBROWSEDIRECTION.OPC_BROWSE_UP, string.Empty);
                            }
                        }
                        catch
                        {
                        }

                        string[] array = null;
                        lock (m_separatorsLock)
                        {
                            array = ((m_separators == null) ? text.Split(m_separators) : text.Split(m_separators));
                        }

                        for (int i = 0; i < array.Length; i++)
                        {
                            if (array[i] != null && array[i].Length != 0)
                            {
                                try
                                {
                                    iOPCBrowseServerAddressSpace.ChangeBrowsePosition(OPCBROWSEDIRECTION.OPC_BROWSE_DOWN, array[i]);
                                }
                                catch
                                {
                                    throw new ResultIDException(ResultID.Da.E_UNKNOWN_ITEM_NAME, "Cannot browse because the server is not compliant because it does not support the BROWSE_TO function.");
                                }
                            }
                        }
                    }
                }
            }

            try
            {
                IEnumString   ppIEnumString      = null;
                OPCBROWSETYPE dwBrowseFilterType = branches ? OPCBROWSETYPE.OPC_BRANCH : OPCBROWSETYPE.OPC_LEAF;
                if (flat)
                {
                    dwBrowseFilterType = OPCBROWSETYPE.OPC_FLAT;
                }

                iOPCBrowseServerAddressSpace.BrowseOPCItemIDs(dwBrowseFilterType, (filters.ElementNameFilter != null) ? filters.ElementNameFilter : "", 0, 0, out ppIEnumString);
                return(new EnumString(ppIEnumString));
            }
            catch
            {
                throw new ResultIDException(ResultID.Da.E_UNKNOWN_ITEM_NAME);
            }
        }