OpcNetApi慢速从kepware读取

时间:2014-10-09 11:00:54

标签: .net winforms opc

我正在使用OpcNetApi与kepware进行通信。我尝试从kepware读取一些值(超过40个值)但是当我的程序执行opc服务器的读取方法时,它等待大约10秒来读取我想要的值。那太过分了。我不希望它等那么久。我希望它最多执行读取方法1秒我不知道是什么问题。为什么opc读取值非常慢?有没有办法让它更快。

这是我的OPC类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VestelTest
{
    class clsOPC
    {

        private static Opc.Da.Server server = null;
        private static Opc.Da.SubscriptionState groupState;
        private static Opc.Da.Subscription group;

        public clsOPC()
        {

        }

        public static void Connect()
        {
            //OpcCom.ServerEnumerator se = new OpcCom.ServerEnumerator();
            //Opc.Server[] servers = se.GetAvailableServers(Opc.Specification.COM_DA_20);
            //Opc.Da.Server server = null;
            Opc.URL url = new Opc.URL("opcda://localhost/KEPware.KEPServerEX.V4");
            OpcCom.Factory fact = new OpcCom.Factory();
            server = new Opc.Da.Server(fact, url);
            server.Connect();


            groupState = new Opc.Da.SubscriptionState();
            groupState.Name = "Salla";
            groupState.Active = false;
            group = (Opc.Da.Subscription)server.CreateSubscription(groupState);
        }

        public static void Read(Opc.Da.Item[] items, Opc.Da.ReadCompleteEventHandler method)
        {
            //Opc.Da.ItemValueResult[] sonuclar = group.Read(group.Items);
            if (group != null)
            {
                //server.Subscriptions.Clear();
                groupState = new Opc.Da.SubscriptionState();
                groupState.Name = "Salla";
                groupState.Active = false;
                server.CancelSubscription(group);
                group = null;
                group = (Opc.Da.Subscription)server.CreateSubscription(groupState);

                items = group.AddItems(items);

                Opc.IRequest req;
                group.Read(group.Items, 123, method, out req);
            }
            //return sonuclar;      
        }

        public static Opc.Da.ItemValueResult[] Read(Opc.Da.Item[] items)
        {
            Opc.Da.ItemValueResult[] sonuclar = new Opc.Da.ItemValueResult[items.Length];
            if (group != null)
            {
                //server.Subscriptions.Clear();
                groupState = new Opc.Da.SubscriptionState();
                groupState.Name = "Salla";
                groupState.Active = false;
                server.CancelSubscription(group);
                group = null;
                group = (Opc.Da.Subscription)server.CreateSubscription(groupState);

                items = group.AddItems(items);

                //sonuclar = group.Read(group.Items);

                sonuclar = server.Read(items);
            }
            return sonuclar;  
        }

        public static void Write(Opc.Da.Item[] writeItems, Opc.Da.ItemValue[] writeValues, Opc.Da.WriteCompleteEventHandler method, Opc.Da.ReadCompleteEventHandler methodRead)
        {
            if (group != null)
            {
                //server.Subscriptions.Clear();
                groupState = new Opc.Da.SubscriptionState();
                groupState.Name = "Salla";
                groupState.Active = false;
                server.CancelSubscription(group);
                group = null;
                group = (Opc.Da.Subscription)server.CreateSubscription(groupState);

                writeItems = group.AddItems(writeItems);

                for (int i = 0; i < writeItems.Length; i++)
                {
                    if (writeValues[i] != null)
                        writeValues[i].ServerHandle = group.Items[i].ServerHandle;
                }
                Opc.IRequest req;

                group.Write(writeValues, 321, method, out req);
                System.Threading.Thread.Sleep(1000);
                group.Read(group.Items, 123, methodRead, out req);
            }

        }

        public static void Write(Opc.Da.Item[] writeItems, Opc.Da.ItemValue[] writeValues)
        {
            if (group != null)
            {
                //server.Subscriptions.Clear();
                groupState = new Opc.Da.SubscriptionState();
                groupState.Name = "Salla";
                groupState.Active = false;
                server.CancelSubscription(group);
                group = null;
                group = (Opc.Da.Subscription)server.CreateSubscription(groupState);

                writeItems = group.AddItems(writeItems);

                for (int i = 0; i < writeItems.Length; i++)
                {
                    if (writeValues[i] != null)
                        writeValues[i].ServerHandle = group.Items[i].ServerHandle;
                }
                Opc.IRequest req;

                group.Write(writeValues);
            }
        }

        public static void Disconnect()
        {
            server.Disconnect();
        }

        static void ReadCompleteCallback(object clientHandle, Opc.Da.ItemValueResult[] results)
        {
            Console.WriteLine("Read completed");
            foreach (Opc.Da.ItemValueResult readResult in results)
            {
                Console.WriteLine("\t{0}\tval:{1}", readResult.ItemName, readResult.Value);
            }
            Console.WriteLine();
        }

        static void WriteCompleteCallback(object clientHandle, Opc.IdentifiedResult[] results)
        {
            Console.WriteLine("Write completed");
            foreach (Opc.IdentifiedResult writeResult in results)
            {
                Console.WriteLine("\t{0} write result: {1}", writeResult.ItemName, writeResult.ResultID);
            }
            Console.WriteLine();
        }

    }
}

我这样用它;

int deviceID = vChannelDevice[i].device.ID;
                        var vAdres = (from adres in d.tblAddress join groupp in d.tblGroup on adres.GroupID equals groupp.ID join device in d.tblDevice on groupp.DeviceID equals device.ID where device.ID == deviceID select new { adres, groupp, device }).ToList();
                        if (vAdres.Count > 0)
                        {
                            Opc.Da.Item[] valueItems = new Opc.Da.Item[vAdres.Count];
                            for (int j = 0; j < vAdres.Count; j++)
                            {
                                valueItems[j] = new Opc.Da.Item();
                                valueItems[j].ItemName = vChannelDevice[i].channel.Ad + "." + vChannelDevice[i].device.Ad + "." + vAdres[j].groupp.Ad + "." + vAdres[j].adres.Ad;
                            }
                            Opc.Da.ItemValueResult[] valueResults = new Opc.Da.ItemValueResult[vAdres.Count];
                            if (backgroundWorker1.CancellationPending)
                            {
                                e.Cancel = true;
                                return;
                            }
                            valueResults = clsOPC.Read(valueItems);
                            for (int j = 0; j < vAdres.Count; j++)
                            {
                                if (valueResults[j] != null)
                                {
                                    if (valueResults[j].Value != null)
                                    {
                                        Kaydet(vAdres[j].adres.ID, Convert.ToDouble(valueResults[j].Value), donguTarih);
                                    }
                                }
                            }
                        }

valueResults = clsOPC.Read(valueItems);等了太多

1 个答案:

答案 0 :(得分:0)

嗯,我知道这已经晚了.. 您要取消订阅 - 这需要旅行。您正在重新创建订阅时间。然后你决定向它添加项目,然后阅读它。

如果要清除订阅 - 为什么不在返回完整读取后执行此操作。然后,您不必在读取请求期间浪费Opc服务器时间。

您的代码是这样的:

    if (group != null)
    {
        //server.Subscriptions.Clear();
        groupState = new Opc.Da.SubscriptionState();
        groupState.Name = "Salla";
        groupState.Active = false;
        server.CancelSubscription(group);
        group = null;
        group = (Opc.Da.Subscription)server.CreateSubscription(groupState);

        items = group.AddItems(items);

        Opc.IRequest req;
        group.Read(group.Items, 123, method, out req);
    }