/// <summary>
        /// Executes a  read request for the current set if items.
        /// </summary>
        public void DoRead()
        {
            try
            {
                // read from server.
                TsCDaItem[] items = ItemsCTRL.GetItems();

                TsCDaItemValueResult[] results = null;

                if (m_subscription != null)
                {
                    if (m_synchronous)
                    {
                        results = m_subscription.Read(items);
                    }
                    else
                    {
                        results = new AsyncRequestDlg().ShowDialog(m_subscription, items);

                        if (results == null)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    // add dummy client handles to test that they get returned properly.
                    foreach (TsCDaItem item in items)
                    {
                        item.ClientHandle = item.ItemName;
                    }

                    results = m_server.Read(items);
                }

                // save results.
                m_values = results;

                BackBTN.Enabled     = true;
                NextBTN.Enabled     = false;
                CancelBTN.Visible   = false;
                DoneBTN.Visible     = true;
                OptionsBTN.Visible  = false;
                ItemsCTRL.Visible   = false;
                ResultsCTRL.Visible = true;

                // display results.
                ResultsCTRL.Initialize(m_server, (m_subscription != null)?m_subscription.Locale:m_server.Locale, results);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes a  read request for the current set if items.
        /// </summary>
        public void DoRead()
        {
            try
            {
                // read from server.
                TsCDaItem[] items = itemsCtrl_.GetItems();

                TsCDaItemValueResult[] results = null;

                if (mSubscription_ != null)
                {
                    if (mSynchronous_)
                    {
                        results = mSubscription_.Read(items);
                    }
                    else
                    {
                        results = new AsyncRequestDlg().ShowDialog(mSubscription_, items);

                        if (results == null)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    // add dummy client handles to test that they get returned properly.
                    foreach (TsCDaItem item in items)
                    {
                        item.ClientHandle = item.ItemName;
                    }

                    results = mServer_.Read(items);
                }

                // save results.
                mValues_ = results;

                backBtn_.Enabled     = true;
                nextBtn_.Enabled     = false;
                cancelBtn_.Visible   = false;
                doneBtn_.Visible     = true;
                optionsBtn_.Visible  = false;
                itemsCtrl_.Visible   = false;
                resultsCtrl_.Visible = true;

                // display results.
                resultsCtrl_.Initialize(mServer_, (mSubscription_ != null)?mSubscription_.Locale:mServer_.Locale, results);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
Ejemplo n.º 3
0
        public void Run()
        {
            try
            {
                const string serverUrl = "opcda://localhost/Technosoftware.DaSample";

                Console.WriteLine();
                Console.WriteLine("Simple OPC DA Client based on the OPC DA/AE/HDA Client SDK .NET Standard");
                Console.WriteLine("------------------------------------------------------------------------");
                Console.Write("   Press <Enter> to connect to "); Console.WriteLine(serverUrl);
                Console.ReadLine();
                Console.WriteLine("   Please wait...");

                TsCDaServer myDaServer = new TsCDaServer();

                // Connect to the server
                myDaServer.Connect(serverUrl);

                // Get the status from the server
                OpcServerStatus status = myDaServer.GetServerStatus();
                Console.WriteLine($"   Status of Server is {status.ServerState}");

                Console.WriteLine("   Connected, press <Enter> to create an active group object and add several items.");
                Console.ReadLine();

                // Add a group with default values Active = true and UpdateRate = 500ms
                TsCDaSubscriptionState groupState = new TsCDaSubscriptionState {
                    Name = "MyGroup"                                                              /* Group Name*/
                };
                var group = (TsCDaSubscription)myDaServer.CreateSubscription(groupState);

                // Add Items
                TsCDaItem[] items = new TsCDaItem[4];
                items[0] = new TsCDaItem
                {
                    ItemName        = "SimulatedData.Ramp",
                    ClientHandle    = 100,
                    MaxAgeSpecified = true,
                    MaxAge          = 0,
                    Active          = true,
                    ActiveSpecified = true
                };
                // Item Name
                // Client Handle
                // Read from Cache

                items[1] = new TsCDaItem
                {
                    ItemName        = "CTT.SimpleTypes.InOut.Integer",
                    ClientHandle    = 150,
                    Active          = true,
                    ActiveSpecified = true
                };
                // Item Name
                // Client Handle

                items[2] = new TsCDaItem
                {
                    ItemName        = "CTT.SimpleTypes.InOut.Short",
                    ClientHandle    = 200,
                    Active          = false,
                    ActiveSpecified = true
                };
                // Item Name
                // Client Handle

                items[3] = new TsCDaItem
                {
                    ItemName = "CTT.Arrays.InOut.Word[]", ClientHandle = 250, Active = false, ActiveSpecified = true
                };
                // Item Name
                // Client Handle

                // Synchronous Read with server read function (DA 3.0) without a group
                TsCDaItemValueResult[] itemValues = myDaServer.Read(items);

                for (int i = 0; i < itemValues.GetLength(0); i++)
                {
                    if (itemValues[i].Result.IsError())
                    {
                        Console.WriteLine($"   Item {itemValues[i].ItemName} could not be read");
                    }
                }

                var itemResults = group.AddItems(items);

                for (int i = 0; i < itemResults.GetLength(0); i++)
                {
                    if (itemResults[i].Result.IsError())
                    {
                        Console.WriteLine($"   Item {itemResults[i].ItemName} could not be added to the group");
                    }
                }

                Console.WriteLine("");
                Console.WriteLine("   Group and items added, press <Enter> to create a data change subscription");
                Console.WriteLine("   and press <Enter> again to deactivate the data change subscription.");
                Console.WriteLine("   This stops the reception of data change notifications.");
                Console.ReadLine();

                group.DataChangedEvent += OnDataChangeEvent;

                Console.ReadLine();

                // Set group inactive
                groupState.Active = false;
                group.ModifyState((int)TsCDaStateMask.Active, groupState);

                Console.WriteLine("   Data change subscription deactivated, press <Enter> to remove all");
                Console.WriteLine("   and disconnect from the server.");
                group.Dispose();
                myDaServer.Disconnect();
                myDaServer.Dispose();
                Console.ReadLine();
                Console.WriteLine("   Disconnected from the server.");
                Console.WriteLine();
            }
            catch (OpcResultException e)
            {
                Console.WriteLine("   " + e.Message);

                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("   " + e.Message);
                Console.ReadLine();
            }
        }