} // 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 { DaAddressSpaceRoot root = Application.Instance.DaAddressSpaceRoot; MyCreator creator = (MyCreator)Application.Instance.Creator; MyDaAddressSpaceElement clockNode = (MyDaAddressSpaceElement)creator.CreateMyDaAddressSpaceElement(); clockNode.Name = "clock"; clockNode.Type = MyDaAddressSpaceElement.TYPE_NODECLOCK; clockNode.IoMode = EnumIoMode.NONE; clockNode.HasChildren = true; root.AddChild(clockNode); MyDaAddressSpaceElement reportNode = (MyDaAddressSpaceElement)creator.CreateMyDaAddressSpaceElement(); reportNode.Name = "report"; reportNode.Type = MyDaAddressSpaceElement.TYPE_NODEREPORT; reportNode.IoMode = EnumIoMode.NONE; reportNode.HasChildren = true; clockNode.AddChild(reportNode); m_reportTagSec = (MyDaAddressSpaceElement)creator.CreateMyDaAddressSpaceElement(); m_reportTagSec.Name = "sec"; m_reportTagSec.Type = MyDaAddressSpaceElement.TYPE_REPORT_SEC; m_reportTagSec.AccessRights = EnumAccessRights.READABLE; m_reportTagSec.Datatype = typeof(Int32); m_reportTagSec.IoMode = EnumIoMode.REPORT_CYCLIC; reportNode.AddChild(m_reportTagSec); m_reportTagMin = (MyDaAddressSpaceElement)creator.CreateMyDaAddressSpaceElement(); m_reportTagMin.Name = "min"; m_reportTagMin.Type = MyDaAddressSpaceElement.TYPE_REPORT_MIN; m_reportTagMin.AccessRights = EnumAccessRights.READABLE; m_reportTagMin.Datatype = typeof(Int32); m_reportTagMin.IoMode = EnumIoMode.REPORT_CYCLIC; reportNode.AddChild(m_reportTagMin); MyDaAddressSpaceElement pollNode = (MyDaAddressSpaceElement)creator.CreateMyDaAddressSpaceElement(); pollNode.Name = "poll"; pollNode.Type = MyDaAddressSpaceElement.TYPE_NODEPOLL; pollNode.IoMode = EnumIoMode.NONE; pollNode.HasChildren = true; clockNode.AddChild(pollNode); MyDaAddressSpaceElement tag = null; tag = (MyDaAddressSpaceElement)creator.CreateMyDaAddressSpaceElement(); tag.Name = "sec"; tag.Type = MyDaAddressSpaceElement.TYPE_POLL_SEC; tag.AccessRights = EnumAccessRights.READABLE; tag.Datatype = typeof(Int32); tag.IoMode = EnumIoMode.POLL; pollNode.AddChild(tag); tag = (MyDaAddressSpaceElement)creator.CreateMyDaAddressSpaceElement(); tag.Name = "min"; tag.Type = MyDaAddressSpaceElement.TYPE_POLL_MIN; tag.AccessRights = EnumAccessRights.READABLE; tag.Datatype = typeof(Int32); tag.IoMode = EnumIoMode.POLL; pollNode.AddChild(tag); } 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); 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())) { // build the namespace 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"); DateTime now = DateTime.Now; ushort second = 0xFF; m_opcServer.ChangeValue(now.Second, EnumDaVariableType.SEC); m_opcServer.ChangeValue(now.Minute, EnumDaVariableType.MIN); while (!Console.End) { now = DateTime.Now; if (second != now.Second) { int currentSecond = now.Second; m_opcServer.ChangeValue(currentSecond, EnumDaVariableType.SEC); if (currentSecond == 0) { m_opcServer.ChangeValue(now.Minute, EnumDaVariableType.MIN); } // end if } // end if Thread.Sleep(100); } // 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