C# Console

This sample uses the Matrikon Simulator Server and creates a TagServer then programmatically adds an IONode, an IOServer, an IOSchedule and several IOTags.  It then gives you the option of polling tags for changes and to use the TagServer form to add or inspect items.  When you exit it calls Release to shutdown.


using System;

using System.Collections;

using IDN.IO;

  

namespace ConsoleExample

{

     class Class1

      {

        [STAThread]

        static void Main(string[] args)

        {

            TagServer tagServer = new TagServer();

         

            IONode node = new IONode();

            node.NodeName = Environment.MachineName;

            // For DCOM use...  node.NodeName = "MyServerPC";

  

            node.Name = "MyNode";

            tagServer.AddChild(node);

            Console.WriteLine("node added.");

           

            IOServer server = new IOServer();

            server.ProgID = "Matrikon.OPC.Simulation";

            // To use a Guid... server.Guid = New Guid("f8582cf2-88fb-11d0-b850-00c0f0104305")

  

            server.Name = "MyServer";

            node.AddChild(server);

            Console.WriteLine("server added.");

  

            IOSchedule schedule = new IOSchedule();

            schedule.Name = "MySchdeule";

            server.AddChild(schedule);

            Console.WriteLine("schedule added.");

  

            IOTag tag = new IOTag();

            tag.AccessName = "Random.ArrayOfReal8";

            schedule.AddChild(tag);

            Console.WriteLine("tag added.");

  

            tag = new IOTag();

            tag.AccessName = "Random.Boolean";

            schedule.AddChild(tag);

            Console.WriteLine("tag added.");

  

            tag = new IOTag();

            tag.AccessName = "Random.Int2";

            schedule.AddChild(tag);

            Console.WriteLine("tag added.");

  

            tag = new IOTag();

            tag.AccessName = "Random.Int4";

            schedule.AddChild(tag);

            Console.WriteLine("tag added.");

  

            Console.WriteLine("connecting...");

            tagServer.Connect();

            Console.WriteLine("connected...");

  

            // Un-comment to show object properties.

            // node.ShowDialog();

            // server.ShowDialog();

            // schedule.ShowDialog();

  

            ((IDNBrowsable) tagServer).ShowDialog();

  

            Console.WriteLine("Press 'ENTER' to check for updates.");

            Console.WriteLine("Enter 'S' to show tag server.");

            Console.WriteLine("Enter 'X' to exit.");

  

            char c;

            int ic;

            while (true)

            {

                ic = Console.Read();

                c = (char) ic;

                if (c == 's' || c == 'S')

                {

                    Console.WriteLine("Starting UI...");

                    tagServer.ShowDialog();

                }

                else if (c == 'x' || c == 'X')

                {

                    Console.WriteLine("Exiting...");

                    tagServer.Release();

                    break;

                }

                else

                {

                    for (int i = 0; i < schedule.Children.Count; i++)

                    {

                        tag = (IOTag) schedule.Children.GetByIndex(i);

                        if (tag.Changed)

                        {

                            tag.Changed = false;

                            Console.WriteLine(tag.Value.ToString());

                        }

                    }

                }

            }

        }

    }

}



Copyright © 2004-2017 Industrial DOT NET, Inc.