zoukankan      html  css  js  c++  java
  • 使用OPCNetAPI连接OPCServer

     OPCServer KepServer;
            OPCGroup KepGroup;
            bool opc_connected;
            string remoteServerName = "KEPware.OPCSampleServer";
            string remoteServerIP = "";
            
            
            public OPCManagerService()
            {
                try
                {
                    KepServer = new OPCServer();
                    KepServer.Connect(remoteServerName, remoteServerIP);
    
                    if (KepServer.ServerState != (int)OPCServerState.OPCRunning)
                    {
                        
                        //这里你可以根据返回的状态来自定义显示信息,请查看自动化接口API文档
                        //tsslServerState.Text = "状态:" + KepServer.ServerState.ToString() + "   ";
                        return;
                    }
                }
                catch (Exception)
                {
                    return;
                }
    
                opc_connected = true;
    
                KepServer.OPCGroups.DefaultGroupIsActive = true;
                KepServer.OPCGroups.DefaultGroupDeadband = 0f;  //the percentage change required before a change is reported, used to filter noise
                KepServer.OPCGroups.DefaultGroupUpdateRate = 10;  //the rate is ms before item is updated
    
                KepGroup = KepServer.OPCGroups.Add("OPCDOTNETGROUP");
                KepGroup.IsSubscribed = false;
                KepGroup.OPCItems.DefaultIsActive = false;
            }

    获取单个OPCItem的方法:

    //OPCDataItemValue是自定义的一个类,用来保存OPCItem的实时数据
    public OPCDataItemValue GetDataItemValue(string ItemID)
            {
                if (!opc_connected) return null;
    
                try
                {
                    OPCItem item = KepGroup.OPCItems.AddItem(ItemID, 1);
    
                    Object value; 
                    Object quality;
                    Object timestamp;
                    item.Read((short)OPCDataSource.OPCDevice, out value, out quality, out timestamp);
    
                    OPCDataItemValue itemValue = new OPCDataItemValue();
                    itemValue.DataValue = value;
                    itemValue.TimeStamp = (DateTime)timestamp;
                    itemValue.Quality = Convert.ToInt32(quality);
    
                    Array removeServerHandle = (Array)(new int[2] { 0, item.ServerHandle });
                    Array removeErrors;
                    KepGroup.OPCItems.Remove(1, ref removeServerHandle, out removeErrors);
    
                    return itemValue;
                }
                catch (Exception)
                {
                   return null;
                }
            }

    获取多个OPCItem的方法:

    public List<OPCDataItemValue> GetDataItems(string ItemID)
            {
                if (!opc_connected) return null;
    
                try
                {
                    List<OPCDataItemValue> dataItems = new List<OPCDataItemValue>();
                    OPCDataItemValue opcDataItem = null;
                    int[] itmHandleServer = new int[2]; //index starts at 1
                    OPCItem KepItem = KepGroup.OPCItems.AddItem(ItemID, 1);
                    itmHandleServer[1] = KepItem.ServerHandle;
                    Array handles = (Array)itmHandleServer;
    
                    Array values; 
                    Array errors;
                    object qualities;
                    object timestamps; //store the timestamp of the read
    
                    //read directly from device
                    KepGroup.SyncRead((short)OPCDataSource.OPCDevice, 1, ref handles, out values, out errors, out qualities, out timestamps);
    
    
                    opcDataItem = new OPCDataItemValue();
                    opcDataItem.ID = KepItem.ItemID;
                    opcDataItem.DataValue = values.GetValue(1);
                    Array t = (Array)timestamps;
                    opcDataItem.TimeStamp = (DateTime)t.GetValue(1);
                    Array q = (Array)qualities;
                    opcDataItem.Quality = Convert.ToInt32(q.GetValue(1));
    
                    dataItems.Add(opcDataItem);
    
                    //删除OPCItem的方法和上面代码中的一样,这里就不写了。
    
                    return dataItems;
                }
                catch (Exception)
                {
                    return null;
                }
            }        

    获取某OPCITem的特定属性:

    public OPCDataItemProperties GetDataItemProperties(string ItemID, List<int> PropertyIDs)
            {
                if (!opc_connected) return null;
    
                try
                {
                    OPCDataItemProperties property = new OPCDataItemProperties();
                    OPCItem item = KepGroup.OPCItems.AddItem(ItemID, 1);
    
                    List<int> idList = new List<int>(PropertyIDs);
                    idList.Insert(0, 0);
                    Array ids = (Array)idList.ToArray();
    
                    int count = PropertyIDs.Count;
                    Array values;
                    Array errors;
                    KepServer.GetItemProperties(ItemID, count, ref ids, out values, out errors);
    
                    property.Count = count;
                    property.IDs = ids;
                    property.Values = values;
                    property.Errors = errors;
    
                    Array removeServerHandle = (Array)(new int[2] { 0, item.ServerHandle });
                    Array removeErrors;
                    KepGroup.OPCItems.Remove(1, ref removeServerHandle, out removeErrors);
    
                    return property;
                }
                catch (Exception)
                {
                    return null;
                }
            }

    获取某OPCItem包含的所有属性:

     public OPCDataItemAvailableProperties GetDataItemAvailableProperties(string ItemID)
            {
                if (!opc_connected) return null;
    
                try
                {
                    OPCDataItemAvailableProperties properties = new OPCDataItemAvailableProperties();
                    OPCItem item = KepGroup.OPCItems.AddItem(ItemID, 1);
    
                    int propertyCount;
                    Array propertyIDs;
                    Array propertyDescriptions;
                    Array propertyDataTypes;
                    KepServer.QueryAvailableProperties(ItemID, out propertyCount, out propertyIDs, out propertyDescriptions, out propertyDataTypes);
    
                    properties.Count = propertyCount;
                    properties.IDs = propertyIDs;
                    properties.Descriptions = propertyDescriptions;
                    properties.DataTypes = propertyDataTypes;
    
                    Array removeServerHandle = (Array)(new int[2] { 0, item.ServerHandle });
                    Array removeErrors;
                    KepGroup.OPCItems.Remove(1, ref removeServerHandle, out removeErrors);
    
                    return properties;
                }
                catch (Exception)
                {
                    return null;
                }
            }
  • 相关阅读:
    【译】在 Chrome 开发者工具中调试 node.js
    2015 年 JavaScript 开发者调查报告
    [译]PostCSS介绍
    [译] Angular 2 VS. React: 血色将至
    使用Fidder将生成环境代码映射到本地(文件夹)
    Sublime Text 3 搭建 React.js 开发环境
    用HTML5+JS开发跨平台的桌面应用
    npm WARN unmet dependency错误解决方法
    【数据结构与算法】线性表操作(C语言)
    【数据结构学习笔记】数组
  • 原文地址:https://www.cnblogs.com/xpvincent/p/3254866.html
Copyright © 2011-2022 走看看