コード例 #1
0
        /// <summary>
        /// Reads the local opc servers.
        /// </summary>
        /// <param name="servers">The servers.</param>
        /// <param name="error">The error.</param>
        /// <param name="exception">The exception.</param>
        /// <returns><c>true</c> if the operation succeeds, <c>false</c> otherwise</returns>
        public bool ReadLocalOpcServers(out OpcServerItems servers, out uint error, out Exception exception)
        {
            servers   = new OpcServerItems();
            error     = ResultCodes.Success;
            exception = null;
            var result = true;

            try
            {
                var opc        = OpcMonitor.CreateOpcClient();
                var opcServers = opc.BrowseServers(string.Empty);

                // The IP address is not used anymore...
                servers.AddRange(opcServers.Select(server => new OpcServerItem(false)
                {
                    Name = server.Description, ClassId = server.ServerClass, IpAddress = "127.0.0.1"
                }));

                opc.Dispose();
            }
            catch (Exception ex)
            {
                Logger.FatalException(this, "Error browsing local OPC servers.", exception);
                servers.Clear();
                error     = ResultCodes.CannotBrowseOpcServers;
                exception = ex;
                DiagnosticsCollection.Instance.AddMessage("Error browsing local OPC servers.");
                DiagnosticsCollection.Instance.AddMessage(exception);
                result = false;
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Reads the opc address space.
        /// </summary>
        /// <param name="serverName">Name of the server.</param>
        /// <param name="opcItem">The opc item.</param>
        /// <param name="error">The error.</param>
        /// <param name="exception">The exception.</param>
        /// <returns><c>true</c> if the operation succeeds, <c>false</c> otherwise</returns>
        public bool ReadOpcAddressSpace(string serverName, out OpcItem opcItem, out uint error, out Exception exception)
        {
            // The root of our tree. This is only to have a unique root in the tree.
            // OPC servers do not always have a unique root.
            opcItem = new OpcItem(true)
            {
                Name = "OPC Address Space"
            };
            error     = ResultCodes.Success;
            exception = null;
            var result = true;

            if (string.IsNullOrEmpty(serverName) || string.IsNullOrWhiteSpace(serverName))
            {
                error = ResultCodes.MissingArgument;
                return(false);
            }

            try
            {
                var opc = OpcMonitor.CreateOpcClient();
                var serverDescriptor = new ServerDescriptor(serverName);
                this.BrowseRecursive(opc, serverDescriptor, null, opcItem);

                opc.Dispose();
            }
            catch (Exception ex)
            {
                Logger.FatalException(this, "Error browsing OPC servers.", exception);
                error     = ResultCodes.BrowseAddressSpaceError;
                exception = ex;
                DiagnosticsCollection.Instance.AddMessage(string.Format("Error browsing OPC server: {0}", serverName));
                DiagnosticsCollection.Instance.AddMessage(exception);
                result = false;
            }

            return(result);
        }