当第一个周期中有“非良好质量”值时,我用于订阅多个 OPC DA 项目的 C# 代码无法按预期工作

问题描述 投票:0回答:1

我正在使用 OPCLabs dll 订阅远程 OPC DA 服务器的多个项目。从服务器获取第一个(初始)值时,通常会出现某些值质量“差”或“不确定”的情况。 (特别是当我订阅大量标签时)。因此,在我的代码中,我正在检查所有值的质量是否为“良好”,然后我接受这些值并用于进一步处理。

但问题是:当所有初始值(订阅后的第一个值)都具有“良好”质量(情况并非总是如此)时,就没有问题。但是,当我假设总共有 70 个标签,并且在第一个周期中仅读取了 45 个标签且质量为“良好”时,那么在第二个读取周期中,在完全读取了 70-45=25 个标签后停止读取,所有标签的质量都良好这次“好”。 (我的意思是 70 减去上一个周期的“良好”质量标签的数量)。

此后我没有收到任何错误,也没有任何异常。该代码只是等待总订阅时间(在我的例子中为 10 秒)完成然后退出。可能是什么问题呢?为什么我的代码停止循环读取更多值?

    public string item_no = ""; // amount of items
    public string[] item_array= new string[100];
    public Int16 item_value_read_good = 0;
    public Int16 item_value_read_total = 0;

    static void Main(string[] args)
    {
          //some code for filling the item_array coming from the arguments;
          item_no = args[2]; // amount of items coming from argument
          subscribe_items(10.92.XXX.XXX)
    }


    public void subscribe_items(string ip)
    {
        item_value_array=new string[100];            
        dAItemGroupArguments.Clear();
        item_value_read_good = 0;
        item_value_read_total = 0;
        all_items_read = false;

        for (int u=0; u<100; u++)
        {
            item_value_array[u] = "";                
        }

        for (int i = 0; i < Convert.ToInt16(item_no); i++)
        {
            dAItemGroupArguments.Add(new DAItemGroupArguments(ip, opcServer , item_array[i], 1000, null));

            //Console.WriteLine("item-" + i+ "= " + item_array[i]) 
        }          

        using (var client = new EasyDAClient())
        {
            client.ItemChanged += client_Main1_ItemChanged;              
            client.SubscribeMultipleItems(dAItemGroupArguments.ToArray());
            //Console.WriteLine("Processing item changed events for 1 minute...");
            Thread.Sleep(10 * 1000);
            client.UnsubscribeAllItems();                
            Environment.Exit(0);
        }
    }

    public void client_Main1_ItemChanged(object sender, EasyDAItemChangedEventArgs e)
    {
        //Console.WriteLine("item changed: "+ e.Arguments.ItemDescriptor.ItemId);
        try
        {
            if (e.Succeeded)
            {
                item_value = e.Vtq.ToString();
                for (int j = 0; j < Convert.ToInt16(item_no); j++)
                {
                    if (e.Arguments.ItemDescriptor.ItemId == item_array[j])
                    {
                        if (item_value.Contains("Good"))
                        {
                            item_value_array[j] = e.Vtq.Value.ToString();                              
                            item_value_read_good += 1;
                        }
                        item_value_read_total += 1;
                        using (StreamWriter sw = File.AppendText(@"D:\test\opcda_testlog.txt"))
                        {
                            sw.WriteLine("item value read total: " + item_value_read_total + " " + Convert.ToString(DateTime.Now));
                            sw.WriteLine("item value read good: " + item_value_read_good + " item no: " + item_no + " " + Convert.ToString(DateTime.Now));
                        }
                    }
                }
                if (item_value_read_good != Convert.ToInt16(item_no) && item_value_read_total == Convert.ToInt16(item_no))
                {
                    item_value_read_good = 0;
                    item_value_read_total = 0;
                    array_item_to_web = "";
                    item_value_array_to_web = "";
                    item_value_array = new string[100];
                    //Console.WriteLine "not all items good read next cycle";
                }
                if (item_value_read_good == Convert.ToInt16(item_no) && item_value_read_total == Convert.ToInt16(item_no))
                {
                    // all tags read with quality good, process the tags....
                }
            }
            else
            {
               // Error handling
            }
        }
        catch(Exception ex)
        {
              // Error Handling
        }
    }
c# publish-subscribe opc-da
1个回答
0
投票

正如@ZbynekZ 回答的那样,我注意到我订阅的标签值都没有改变。所以代码没有问题。抱歉耽误大家时间了。

© www.soinside.com 2019 - 2024. All rights reserved.