} // end Terminate public int Prepare(MyCreator aMyCreator) { int result = (int)EnumResultCode.S_OK; // TODO - binary license activation // Fill in your binary license activation keys here // // NOTE: you can activate one or all of the features at the same time // firstly activate the COM-DA Server feature // result = Application.Instance.Activate(EnumFeature.DA_SERVER, "XXXX-XXXX-XXXX-XXXX-XXXX"); if (!ResultCode.SUCCEEDED(result)) { return result; } // activate the XML-DA Server Feature // result = Application.Instance.Activate(EnumFeature.XMLDA_SERVER, "XXXX-XXXX-XXXX-XXXX-XXXX"); if (!ResultCode.SUCCEEDED(result)) { return result; } // END TODO - binary license activation result = Application.Instance.Initialize(aMyCreator); if (ResultCode.SUCCEEDED(result)) { Application.Instance.EnableTracing( EnumTraceGroup.ALL, EnumTraceGroup.ALL, EnumTraceGroup.SERVER, EnumTraceGroup.SERVER, "Trace.txt", 1000000, 0); } // end if return result; } // end Prepare
} // end ProcessCommandLine public int BuildAddressSpace() { try { MyCreator creator = (MyCreator)Application.Instance.Creator; // DA DaAddressSpaceRoot daRoot = Application.Instance.DaAddressSpaceRoot; m_daSimulationElement = (MyDaAddressSpaceElement)creator.CreateMyDaAddressSpaceElement(); m_daSimulationElement.Name = "simulation"; m_daSimulationElement.AccessRights = EnumAccessRights.READWRITEABLE; m_daSimulationElement.Datatype = typeof(System.Int32); m_daSimulationElement.IoMode = EnumIoMode.POLL; daRoot.AddChild(m_daSimulationElement); m_daSimulationElement.ValueChanged( new ValueQT( m_random.Next(5000), EnumQuality.GOOD, DateTime.Now)); //AE AeAddressSpaceRoot aeRoot = Application.Instance.AeAddressSpaceRoot; AeAddressSpaceElement aeElement = creator.CreateAeAddressSpaceElement(); aeElement.Name = "SimulationElement"; aeElement.HasChildren = false; aeRoot.AddChild(aeElement); } catch(Exception exc) { Trace( EnumTraceLevel.ERR, EnumTraceGroup.USER1, "OpcServer:BuildAddressSpace", exc.ToString()); return (int)EnumResultCode.E_FAIL; } // end try...catch return (int)EnumResultCode.S_OK; } // end BuildAddressSpace
} // end StartSimulationThread //-- #endregion #region Public Methods //-------------------- public int Start() { try { int result = (int)EnumResultCode.S_OK; int registrationResult = (int)EnumResultCode.S_OK; // create and initialize the OpcServer instance m_opcServer = new OpcServer(); m_opcServer.Initialize(); MyCreator creator = new MyCreator(); if (!ResultCode.SUCCEEDED(m_opcServer.Prepare(creator))) { m_opcServer.Terminate(); m_opcServer = null; return((int)EnumResultCode.E_FAIL); } // end if // handle the command line arguments (register/unregister, etc) string commandline = Environment.CommandLine; registrationResult = m_opcServer.ProcessCommandLine(commandline); if (registrationResult != (int)EnumResultCode.S_OK) { if (registrationResult == (int)EnumResultCode.S_FALSE) { //registration operation succesful m_opcServer.Trace( EnumTraceLevel.INF, EnumTraceGroup.USER1, "Dll::Main", "Registration succeeded"); } else { m_opcServer.Trace( EnumTraceLevel.INF, EnumTraceGroup.USER1, "Console::Main", "Registration failed"); } // end if...else // no matter what close the application if //processCommandLine returned something different of S_OK m_opcServer.Terminate(); m_opcServer = null; return(registrationResult); } // end if // start the OPC server's I/O internal mechanism if (ResultCode.SUCCEEDED(m_opcServer.Start())) { // build the namespace m_opcServer.BuildAddressSpace(); // Build the event categories m_opcServer.BuildEventCategories(); // declare the namespaces built and the server ready for clients to connect m_opcServer.Ready(); } // end if // start the simulation thread if (ResultCode.SUCCEEDED(result)) { StartSimulationThread(); } // end if return(registrationResult); } catch (Exception exc) { Console.WriteLine(exc.ToString()); return((int)EnumResultCode.E_FAIL); } // end try...catch } // end Start