【问题标题】:How to read values from OPC如何从 OPC 读取值
【发布时间】:2015-09-22 03:21:25
【问题描述】:

我正在尝试使用 Interop.OPCAutomation.dll 从 OPC 服务器读取值

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OPCAutomation;
namespace OPC
{
    public partial class Form1 : Form
    {
        OPCServer ObjOPCServer;
        OPCGroups ObjOPCGroups;
        OPCGroup ObjOPCGroup;
        string OPCServerName;
        public Form1()
        {
            getData();
        }
        private void getData()
        {
            try
            {
                 int count = 1;
            opcServer.Connect("OPCTechs.SiemensNet30DA", "");

            opcGroup = opcServer.OPCGroups.Add("MP");
            opcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(opcGroup_DataChange);

            //Get First String
            for (int i = 40; i <= 47; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Second String
            for (int i = 80; i <= 91; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Third String
            for (int i = 69; i >= 60; i--)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Fourth String
            for (int i = 200; i <= 224; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Fifth String
            for (int i = 300; i <= 849; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);

            //Get Sixth String
            for (int i = 40; i >= 47; i++)
                opcGroup.OPCItems.AddItem("D104.B" + i, count++);


            opcGroup.OPCItems.DefaultIsActive = true;
            opcGroup.UpdateRate = 1000;
            opcGroup.IsSubscribed = opcGroup.IsActive;
            }
            catch (Exception exc)
            {
                 MessageBox.Show(exc.Message, "Alert");
            }
       }
       private void opcGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
       {
            try
            {
                 string temp = "";
            int count = 1;
            for (; count <= 8; count++)
            {
                int ff = Convert.ToInt32(ClientHandles.GetValue(count));
                //if (Convert.ToInt32(ClientHandles.GetValue(count)) == count)
                temp += ItemValues.GetValue(count).ToString();
            }
            Textbox4.Text = temp.ToString();
            temp = "";

            for (; count <= 12; count++)
            {
                if (Convert.ToInt32(ClientHandles.GetValue(count)) == count)
                    temp += ItemValues.GetValue(count).ToString();
            }
            TextBox3.Text = temp.ToString();
            temp = "";

            for (; count <= 12; count++)
            {
                if (Convert.ToInt32(ClientHandles.GetValue(count)) == count)
                    temp += ItemValues.GetValue(count).ToString();
            }
            TextBox2.Text = temp.ToString();
            temp = "";


        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "Alert");
        }
        }
    }
}

此代码未从 OPC 服务器返回值。这条线报错

temp += ItemValues.GetValue(count).ToString();

错误

Object reference not set to an instance of an object

【问题讨论】:

    标签: c# plc opc opc-da


    【解决方案1】:

    首先,检查 ItemValues 是否不为空。可能不是,问题可能出在 ItemValues.GetValue(count) 的空值上,但无论如何都值得检查。你永远不知道服务器会返回什么......

    现在,实际答案是:您应该首先检查 Qualities 中的相应元素,即方法中的 Qualities.GetValue(count)。质量很可能很差,因此该值无效(因此可能是空引用)。您需要根据 OPC 规范根据其含义解码质量位字段,但在简化(并且稍微不正确,但通常有效)的意义上,低于 64 的质量不好,并且没有与之关联的数据值。

    【讨论】:

    • 是的,我收到了Qualities.GetValue(count) = 0。可能这就是它给出错误的原因。正如您在代码中看到的,我正在尝试从 OPC 服务器获取 100 多个项目。但是ClientHandles 数组每次加载时都会随机播放。如何从数组中获取特定项目
    • (这是一个新问题,应该这样发布)。
    • 您将客户端句柄传递给 AddItem(s) 方法。它可以是任何你喜欢的东西——你可以使用的某种物品的标识。然后,您需要解释您在 opcGroup_DataChange 中接收到的客户端句柄,并根据客户端句柄执行所需的任何操作。
    • 是的,我知道。这就是为什么我接受了您的回答并发布了另一个问题。请看stackoverflow.com/questions/32710350/get-multiple-opc-values
    • 正如我所说,我随机获取 ClientHandles,因此无法检测到它们。更多细节,请寻找其他问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    相关资源
    最近更新 更多