} // 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 // 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 MyDaAddressSpaceRoot daRoot = (MyDaAddressSpaceRoot)Application.Instance.DaAddressSpaceRoot; m_daSimulationElement1 = (MyDaAddressSpaceElement)creator.CreateMyDaAddressSpaceElement(); m_daSimulationElement1.Name = "client specific"; m_daSimulationElement1.AccessRights = EnumAccessRights.READABLE; m_daSimulationElement1.Datatype = typeof(System.Int32); m_daSimulationElement1.IoMode = EnumIoMode.POLL_OWNCACHE; daRoot.AddChild(m_daSimulationElement1); m_daSimulationElement2 = (MyDaAddressSpaceElement)creator.CreateMyDaAddressSpaceElement(); m_daSimulationElement2.Name = "secured write"; m_daSimulationElement2.AccessRights = EnumAccessRights.READWRITEABLE; m_daSimulationElement2.Datatype = typeof(System.Int32); m_daSimulationElement2.IoMode = EnumIoMode.POLL; daRoot.AddChild(m_daSimulationElement2); m_daSimulationElement2.ValueChanged(new ValueQT(2, EnumQuality.GOOD, DateTime.Now)); } 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 CreateOpcClient //-- #endregion public static void Main(String[] args) { try { int result = (int)EnumResultCode.S_OK; EndEvent = new AutoResetEvent(false); m_clientChanged = new AutoResetEvent(true); Console console = new Console(); MyWin32.HandlerRoutine handlerRoutine = new MyWin32.HandlerRoutine(MyWin32.Handler); MyWin32.SetConsoleCtrlHandler( handlerRoutine, true); // create and initialize the OpcServer instance console.CreateOpcServer(); OpcServer server = console.OpcServer; server.Initialize(); MyCreator creator = new MyCreator(); if (!ResultCode.SUCCEEDED(server.Prepare(creator))) { server.Terminate(); MyWin32.Handler(MyWin32.CtrlTypes.CTRL_CLOSE_EVENT); server = null; return; } // end if // handle the command line arguments (register/unregister, etc) string commandline = Environment.CommandLine; result = server.ProcessCommandLine(commandline); if (result != (uint)EnumResultCode.S_OK) { if (result == (uint)EnumResultCode.S_FALSE) { //registration operation succesful server.Trace( EnumTraceLevel.INF, EnumTraceGroup.USER1, "Console::Main", "Registration succeeded"); } else { server.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 server.Terminate(); server = null; return; } // end if // start the OPC server's I/O internal mechanism if (ResultCode.SUCCEEDED(server.Start())) { server.BuildAddressSpace(); // declare the namespaces built and the server ready for clients to connect server.Ready(); } // end if System.Console.WriteLine("Press Ctrl-C to exit\n"); Int32 iRet = WaitHandle.WaitTimeout; while (!Console.End) { // wait for keyboard entry 'Ctrl + C' or 'Ctrl + Break' iRet = System.Threading.WaitHandle.WaitAny(new AutoResetEvent[] { EndEvent, m_clientChanged }, -1, true); if (iRet == 0) { End = true; } else { ShowObjectTree(); } } // end while server.Stop(); server.Terminate(); server = null; MyWin32.Handler(MyWin32.CtrlTypes.CTRL_CLOSE_EVENT); } catch (Exception exc) { System.Console.WriteLine(exc.ToString()); } // end try...catch } // end Main