Esempio n. 1
0
        private OpcServerConnectResult DoConnect(string progid)
        {
            OpcServerConnectResult result = new OpcServerConnectResult {
                IsSuccess = false
            };

            try
            {
                _theSrv.Connect(progid);
                Thread.Sleep(100);
                _theSrv.SetClientName("DirectOPC " + Process.GetCurrentProcess().Id);   // set my client name (exe+process no)

                SERVERSTATUS sts;
                _theSrv.GetStatus(out sts);

                // get infos about OPC server
                StringBuilder sb = new StringBuilder(sts.szVendorInfo, 200);
                sb.AppendFormat(" ver:{0}.{1}.{2}", sts.wMajorVersion, sts.wMinorVersion, sts.wBuildNumber);
                result.ServerInfo = sb.ToString();

                // set status to show server state
                result.SbpTimeStart = DateTime.FromFileTime(sts.ftStartTime);
                result.SbpStatus    = sts.eServerState.ToString();
                result.IsSuccess    = true;
                return(result);
            }
            catch (COMException ex)
            {
                result.ErrorText = ex.Message;
                return(result);
            }
        }
Esempio n. 2
0
        public bool DoInit(OpcServerDescription opcServerDescription)
        {
            _theSrv = new OpcServer();

            OpcNamespacesTree = new TreeNode <string>(_rootname);

            // ---------------
            OpcServerConnectResult connectResult = DoConnect(opcServerDescription.ProgId);

            if (!connectResult.IsSuccess)
            {
                return(false);
            }

            OpcConnected = true;

            // add event handler for server shutdown
            _theSrv.ShutdownRequested += (e, args) =>
            {
                CallOnServerShutdown(args.shutdownReason);
            };

            // precreate the only OPC group in this example
            if (!CreateGroup())
            {
                return(false);
            }

            // browse the namespace of the OPC-server
            if (!DoBrowse())
            {
                return(false);
            }

            return(true);
        }