Пример #1
0
        /// <summary>
        /// Synchronously eads the values from the server.
        /// </summary>
        private TsCHdaItemValueCollection[] SyncRead(TsCHdaItem[] items)
        {
            switch (mType_)
            {
            // synchronous read raw or read processed.
            case RequestType.Read:
            {
                return(mTrend_.Read(items));
            }

            // synchronous read raw.
            case RequestType.ReadRaw:
            {
                return(mTrend_.ReadRaw(items));
            }

            // synchronous read processed.
            case RequestType.ReadProcessed:
            {
                return(mTrend_.ReadProcessed(items));
            }

            // synchronous read modified.
            case RequestType.ReadModified:
            {
                return(mTrend_.ReadModified(items));
            }

            // synchronous read at time.
            case RequestType.ReadAtTime:
            {
                return(mTrend_.ReadAtTime(items));
            }
            }

            return(null);
        }
        public void Run()
        {
            try
            {
                const string serverUrl = "opchda://localhost/OPCSample.OpcHdaServer";

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

                TsCHdaServer myHdaServer = new TsCHdaServer();

                myHdaServer.Connect(serverUrl);

                Console.WriteLine("   Connected, press <Enter> to add a trend.");
                Console.ReadLine();

                // Add a trend and set the properties for reading
                TsCHdaTrend trend = new TsCHdaTrend(myHdaServer)
                {
                    StartTime = new TsCHdaTime(new DateTime(2004, 01, 01, 00, 00, 00)), EndTime = new TsCHdaTime(new DateTime(2004, 01, 01, 06, 00, 00)), IncludeBounds = true, MaxValues = 1000
                };

                OpcItem itemId = new OpcItem("Static Data/Ramp [15 min]");

                trend.Timestamps.Add(new DateTime(2016, 01, 01, 00, 00, 00));

                trend.ReadRaw(new OpcItem[] { trend.AddItem(itemId) }, null, OnReadComplete, out _);

                // read the historic data of the specified item
                TsCHdaItemValueCollection[] items = trend.ReadRaw(new[] { trend.AddItem(itemId) });
                foreach (TsCHdaItemValueCollection item in items)
                {
                    Console.WriteLine($"{item.ItemName}");

                    foreach (TsCHdaItemValue val in item)
                    {
                        if ((val.Quality.GetCode() & (int)TsDaQualityMasks.QualityMask) != (int)TsDaQualityBits.Good)
                        {
                            Console.WriteLine($"      {val.Timestamp}, {val.Quality}");
                        }
                        else
                        {
                            Console.WriteLine($"      {val.Timestamp}, {val.Value}");
                        }
                    }
                }

                trend.Timestamps.Add(new DateTime(2016, 01, 01, 00, 00, 00));
                items = trend.ReadAtTime(new[] { trend.AddItem(itemId) });
                foreach (TsCHdaItemValueCollection item in items)
                {
                    Console.WriteLine($"{item.ItemName}");


                    foreach (TsCHdaItemValue val in item)
                    {
                        if ((val.Quality.GetCode() & (int)TsDaQualityMasks.QualityMask) != (int)TsDaQualityBits.Good)
                        {
                            Console.WriteLine($"      {val.Timestamp}, {val.Quality}");
                        }
                        else
                        {
                            Console.WriteLine($"      {val.Timestamp}, {val.Value}");
                        }
                    }
                }
                Console.WriteLine("   Historical Data Trend read, press <Enter> to disconnect from the server.");
                myHdaServer.Disconnect();
                myHdaServer.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();
            }
        }
        ///////////////////////////////////////////////////////////////////////
        #region OPC Sample Functionality

        void DoOPCCalls()
        {
            try
            {
                const string serverUrl = "opchda://localhost/OPCSample.OpcHdaServer";

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

                //OpcBase.ValidateLicense("License Key");

                TsCHdaServer myHdaServer = new TsCHdaServer();

                myHdaServer.Connect(serverUrl);

                Console.WriteLine("   Connected, press <Enter> to add a trend.");
                Console.ReadLine();

                // Add a trend and set the properties for reading
                TsCHdaTrend trend = new TsCHdaTrend(myHdaServer)
                {
                    StartTime = new TsCHdaTime(new DateTime(2004, 01, 01, 00, 00, 00)), EndTime = new TsCHdaTime(new DateTime(2004, 01, 01, 06, 00, 00)), IncludeBounds = true, MaxValues = 1000
                };

                OpcItem itemID = new OpcItem("Static Data/Ramp [15 min]");

                trend.Timestamps.Add(new DateTime(2016, 01, 01, 00, 00, 00));
                OpcItemResult[] results = null;
                IOpcRequest     request = null;

                results = trend.ReadRaw(new TsCHdaItem[] { trend.AddItem(itemID) }, null, new TsCHdaReadValuesCompleteEventHandler(OnReadComplete), out request);

                // read the historic data of the specified item
                TsCHdaItemValueCollection[] items = trend.ReadRaw(new TsCHdaItem[] { trend.AddItem(itemID) });
                foreach (TsCHdaItemValueCollection item in items)
                {
                    Console.WriteLine(String.Format("{0}", item.ItemName));

                    foreach (TsCHdaItemValue val in item)
                    {
                        if (((int)val.Quality.GetCode() & (int)TsDaQualityMasks.QualityMask) != (int)TsDaQualityBits.Good)
                        {
                            Console.WriteLine(string.Format("      {0}, {1}", val.Timestamp, val.Quality));
                        }
                        else
                        {
                            Console.WriteLine(string.Format("      {0}, {1}", val.Timestamp, val.Value.ToString()));
                        }
                    }
                }

                trend.Timestamps.Add(new DateTime(2016, 01, 01, 00, 00, 00));
                items = trend.ReadAtTime(new TsCHdaItem[] { trend.AddItem(itemID) });
                foreach (TsCHdaItemValueCollection item in items)
                {
                    Console.WriteLine(String.Format("{0}", item.ItemName));


                    foreach (TsCHdaItemValue val in item)
                    {
                        if (((int)val.Quality.GetCode() & (int)TsDaQualityMasks.QualityMask) != (int)TsDaQualityBits.Good)
                        {
                            Console.WriteLine(string.Format("      {0}, {1}", val.Timestamp, val.Quality));
                        }
                        else
                        {
                            Console.WriteLine(string.Format("      {0}, {1}", val.Timestamp, val.Value.ToString()));
                        }
                    }
                }
                Console.WriteLine("   Historical Data Trend read, press <Enter> to disconnect from the server.");
                myHdaServer.Disconnect();
                myHdaServer.Dispose();
                Console.ReadLine();
                Console.WriteLine("   Disconnected from the server.");
                Console.WriteLine();
            }
            catch (OpcResultException e)
            {
                Console.WriteLine("   " + e.Message);
                Console.ReadLine();
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine("   " + e.Message);
                Console.ReadLine();
                return;
            }
        }