}               //	end OpcServer
        //--
        #endregion

        #region Public Methods
        //--------------------------
        public void CreateOpcServer()
        {
            if (m_opcServer == null)
            {
                m_opcServer = new OpcServer();
            }           //	em=nd if
        }               //	end CreateOpcClient
Example #2
0
        // The main entry point for the process
        static void Main()
        {
            int    result      = (int)EnumResultCode.S_OK;
            string commandline = Environment.CommandLine;

            OpcServer.AddressSpaceChanged = new System.Threading.AutoResetEvent(true);

            System.ServiceProcess.ServiceBase[] ServicesToRun;
            OpcService opcService = new OpcService();

            ServicesToRun = new System.ServiceProcess.ServiceBase[] { opcService };

            //	create and initialize the OpcServer instance
            opcService.CreateOpcServer();
            m_opcServer.Initialize();
            m_opcServer.ServiceName        = serviceName;
            m_opcServer.ServiceDescription = serviceDescription;

            m_opcServer.ReconfigHandles = new MyDaAddressSpaceElement[3];
            MyCreator creator = new MyCreator();

            if (!ResultCode.SUCCEEDED(m_opcServer.Prepare(creator)))
            {
                m_opcServer.Terminate();
                m_opcServer = null;
                return;
            }               //	end if

            //	handle the command line arguments (register/unregister, etc)
            result = m_opcServer.ProcessCommandLine(commandline);
            if (result != (int)EnumResultCode.S_OK)
            {
                if (result == (int)EnumResultCode.S_FALSE)
                {
                    //registration operation succesful
                    m_opcServer.Trace(
                        EnumTraceLevel.INF,
                        EnumTraceGroup.USER1,
                        "Service::Main",
                        "Registration succeeded");
                }
                else
                {
                    m_opcServer.Trace(
                        EnumTraceLevel.INF,
                        EnumTraceGroup.USER1,
                        "Service::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;
            }               //	end if
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }
Example #3
0
        }           //	end Start

        /// <summary>
        /// Stop this service.
        /// </summary>
        protected override void OnStop()
        {
            try
            {
                //	terminate the simulation
                SimulationEnd = true;
                Mutex.WaitOne();
                Mutex.ReleaseMutex();
                m_opcServer.Stop();
                m_opcServer.Terminate();
                m_opcServer = null;
            }
            catch (Exception exc)
            {
                FileStream   fs           = new FileStream("C:\\Service_TraceData.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter streamWriter = new StreamWriter(fs);
                streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                streamWriter.WriteLine();
                streamWriter.WriteLine(exc.Message + exc.Source + exc.StackTrace + exc.TargetSite);
                streamWriter.Flush();
                streamWriter.Close();
            }       //	end try...catch
        }           //	end Stop
        }               //	end StartSimulationThread

        //--
        #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
                server.ReconfigHandles        = new MyDaAddressSpaceElement[3];
                OpcServer.AddressSpaceChanged = new System.Threading.AutoResetEvent(true);

                //	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

                server.ShowObjectTree();

                //	start the simulation thread
                if (ResultCode.SUCCEEDED(result))
                {
                    console.StartSimulationThread();
                }                       //	end if

                System.Console.WriteLine("Press Ctrl-C to exit\n");

                while (!Console.End)
                {
                    //	TODO: place your cyclic code here
                    Thread.Sleep(1000);
                }                       //	end while

                //	terminate the simulation
                console.SimulationEnd = true;
                console.Mutex.WaitOne();
                console.Mutex.ReleaseMutex();

                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