• 使用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;
                }
            }
  • 相关阅读:
    关于EventEmitter的用法
    nodejs中异常错误的处理方式
    nodejs中使用RabbitMq消息中心系统的方式
    webpack中alias别名配置
    webpack中字体配置,可以引入bootstrap
    安装node-sass提示没有vendor目录的解决办法
    Object.assign方法复制或合并对象
    ZeroClipboard跨浏览器复制粘贴
    用extract-text-webpack-plugin提取出来的css文件中背景图片url的不正确的问题
    python抽取指定url页面的title方法
  • 原文地址:https://www.cnblogs.com/xpvincent/p/3254866.html
Copyright © 2020-2023  润新知