Exemple #1
0
        // connect to OPC server via ProgID
        public bool DoConnect(string progid)
        {
            try
            {
                theSrv.Connect(progid);
                Thread.Sleep(100);
                theSrv.SetClientName("DirectOPC " + thisprocess.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);
                txtServerInfo.Text = sb.ToString();

                // set status bar text to show server state
                sbpTimeStart.Text = DateTime.FromFileTime(sts.ftStartTime).ToString();
                sbpStatus.Text    = sts.eServerState.ToString();
            }
            catch (COMException)
            {
                MessageBox.Show(this, "connect error!", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }
            return(true);
        }
        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);
            }
        }
Exemple #3
0
        public void TestSetClientName01()
        {
            //testing --
            OpcServer opcServer         = new OpcServer();
            Accessor  opcServerAccessor = ReflectionAccessor.Wrap(opcServer);

            opcServerAccessor.SetField("ifServer", new OPCServerComClass());
            opcServerAccessor.SetField("ifCommon", ((IOPCCommon)opcServerAccessor.GetField("ifServer")));
            //Test Procedure Call
            opcServer.SetClientName("Str1");
            //Post Condition Check
        }
        public void Connect(string p_ClientName)
        {
            _server = new OpcServer();

            _server.RemoteConnect(_serverId, _machineName);

            try
            {
                _server.SetClientName(p_ClientName);

                _server.ShutdownRequested += new ShutdownRequestEventHandler(Server_ShutdownRequest);
            }
            catch (Exception l_Ex)
            {
                Disconnect();

                throw l_Ex;
            }
        }
Exemple #5
0
        /// <summary>
        /// initialize OPC server and group
        /// </summary>
        /// <param name="serverName">OPC Server Name</param>
        /// <param name="groupName">OPC Group name under the OPC server.
        /// Here groupName is trendViewer Process ID in string</param>
        ///
        public void InitializeServer(string serverName, string groupName)
        {
            string Function_Name = "InitializeServer";

            LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Entered");
            lock (m_opcDataDicObj)
            {
                try
                {
                    m_OPCSrvName   = serverName;
                    m_OPCGroupName = groupName;

                    //Fix - Remove opcGroup and Disconnect server to avoid GC Fianlizer thread call these methods
                    try
                    {
                        if (m_OPCGroup != null)
                        {
                            m_OPCGroup.Remove(false);
                            m_OPCGroup = null;
                        }

                        if (m_OPCServer != null)
                        {
                            m_OPCServer.Disconnect();
                            m_OPCServer = null;
                        }
                    }
                    catch (Exception localException)
                    {
                        LogHelper.Error(CLASS_NAME, Function_Name, localException);
                    }

                    m_OPCServer = new OpcServer();

                    LogHelper.Trace(CLASS_NAME, Function_Name, "Before Connect");
                    m_OPCServer.Connect(serverName);
                    LogHelper.Trace(CLASS_NAME, Function_Name, "After Connect");

                    Thread.Sleep(200);
                    m_OPCServer.SetClientName("DirectOPC " + groupName + serverName);   // set my client name (exe+process no)

                    SERVERSTATUS sts;
                    m_OPCServer.GetStatus(out sts);
                    LogHelper.Info(CLASS_NAME, Function_Name, serverName + ": " + DateTime.FromFileTime(sts.ftStartTime).ToString());
                    LogHelper.Info(CLASS_NAME, Function_Name, serverName + ": " + sts.eServerState.ToString());

                    // add event handler for server shutdown
                    m_OPCServer.ShutdownRequested += new ShutdownRequestEventHandler(this.OPCSrv_ServerShutDown);

                    //Setup opc group
                    m_OPCGroup              = m_OPCServer.AddGroup("MFTOPC-Group-" + serverName, true, UPDATEGROUPRATE);
                    m_OPCGroup.DataChanged += new DataChangeEventHandler(this.OPCGrp_DataChange);
                    m_opcSrvConnectFlag     = true;
                }
                catch (COMException localException)
                {
                    LogHelper.Error(CLASS_NAME, Function_Name, localException);
                    LogHelper.Info(CLASS_NAME, Function_Name, "Initialize of the OPC Server is not successful");
                }
                catch (Exception localException)
                {
                    LogHelper.Error(CLASS_NAME, Function_Name, localException);
                }
            }
            LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Exited");
        }