使用OPCNetAPI连接OPCServer
2015-04-29 12:05
274 查看
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; } }
相关文章推荐
- 使用OPCNetAPI连接OPCServer
- EventStore .NET API Client在使用线程池线程同步写入Event导致EventStore连接中断的问题研究
- C#使用OpcNetApi.dll和OpcNetApi.Com.dll操作OPC
- 使用 OpenSSL API 建立安全连接 - 双向认证
- ASP.NET MVC 5 - 创建连接字符串(Connection String)并使用SQL Server LocalDB
- 在vs里面安装ODP.NET连接oracle数据库,及代码如何使用
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)
- .net WebApi中使用swagger
- Asp.net Core WebApi 使用Swagger做帮助文档,并且自定义Swagger的UI
- 校园网用户使用Vmware虚拟机的Net连接方式上网教程
- 在ASP.NET Core Web API上使用Swagger提供API文档
- 在ASP.NET Core Web API上使用Swagger提供API文档
- Asp.Net Core使用MySql.Data连接MySql示例
- 使用ASP.NET Web API Help Pages 创建在线接口文档
- QTcpSocket 对连接服务器中断的不同情况进行判定(六种情况,其中一种使用IsNetworkAlive API方法)
- 在ASP.NET Core Web API上使用Swagger提供API文档
- centos6.5下部署用于生产的hadoop,并使用C语言API连接hadoop
- C#编程使用Managed Wifi API连接wifi整理(一)
- Asp.net Mvc 使用EF6 code first 方式连接MySQL总结
- 解决使用MySQL C-API开发应用时的连接超时问题