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 = ; //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, ); 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[] { , item.ServerHandle });
Array removeErrors;
KepGroup.OPCItems.Remove(, 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[]; //index starts at 1
OPCItem KepItem = KepGroup.OPCItems.AddItem(ItemID, );
itmHandleServer[] = 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, , ref handles, out values, out errors, out qualities, out timestamps); opcDataItem = new OPCDataItemValue();
opcDataItem.ID = KepItem.ItemID;
opcDataItem.DataValue = values.GetValue();
Array t = (Array)timestamps;
opcDataItem.TimeStamp = (DateTime)t.GetValue();
Array q = (Array)qualities;
opcDataItem.Quality = Convert.ToInt32(q.GetValue()); 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, ); List<int> idList = new List<int>(PropertyIDs);
idList.Insert(, );
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[] { , item.ServerHandle });
Array removeErrors;
KepGroup.OPCItems.Remove(, 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, ); 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[] { , item.ServerHandle });
Array removeErrors;
KepGroup.OPCItems.Remove(, ref removeServerHandle, out removeErrors); return properties;
}
catch (Exception)
{
return null;
}
}

使用OPCNetAPI连接OPCServer的更多相关文章

  1. 连接opcserver时报错 connecting to OPC Server "****" CoCreateInstance 服务器运行失败

    在普通windows系统连接OPCServer可能会报这样的错,排查很长时间,OPCServer跟Client都运行正常,点号录入也正常. 最后发现,其实是OPCServer 与OPCClient 权 ...

  2. 【干货】如何通过OPC自定义接口来实现客户端数据的读取?

    上篇博文分享了我的知识库,被好多人关注,受宠若惊.今天我把我在项目中封装的OPC自定义接口的程序分享一下.下面将会简单简单介绍下OPC DA客户端数据访问,以及搭配整个系统的运行环境. OPC(OLE ...

  3. 虚拟机安装_1_wincc_matriton

    1,安装虚拟机: 选择:win7专业版64位ISO. 2,安装office2016 3,激活系统和OFFICE 4,安装winccV7.4 4.1 安装WINDOWS组件 MSMQ 4.1.1 安装I ...

  4. OPCServer Modbus使用和配置

    一,安装KEPware.Enhanced.OPC.DDE.KEPServer.(PLC数据传送给KEPServer,开发的程序用OPCServer读KEPServer) 设置ip地址后面是指的plc站 ...

  5. 使用OPC的方式去连接PLC进行AB SLC-5_04数据的采集

    1.  必备软件 Rslinx classic 2.57 .net framework 2.0 VS2013 OS: win7 enterprise x64 2.  软件安装 2.1.安装RSlinx ...

  6. C#l连接OPC进行数据交互

    步骤 :引用 OPCNETAPI.DLL&&OPCNETAPI.COM.DLL 1.查询服务器      2. 连接服务器  3. 读取数据     4.写入数据 1.查询服务器 :根 ...

  7. OPC测试常用的OPCClient和OPCServer软件推荐

    各位在进行OPC通讯时,常会遇到两种情况: 1)使用一个OPCClient在同一台计算机上连接远程计算机上的多个OPCServer时,发现某个OPCServer是通畅的,但其他的OPCServer却无 ...

  8. C#使用OpcNetApi.dll和OpcNetApi.Com.dll操作OPC

    本人学习了一下.Net,恰好,51自学网,又要用这个.而网上很多VC6,VB6,VB .Net的但,很少C#的.现在研究一下,给出例子: 测试平台,是VS2008,KEPServer,OpcNetAp ...

  9. OPCClient和OPCServer在Windows上运行方式的恩怨

    http://www.diangon.com/wenku/PLC/201504/00021970.html 近段时间,遇到不少人都被OPCClient与OPCServer之间的通讯搞得头大,通过几次远 ...

随机推荐

  1. SpringMvc整合Mybatis并使用声明式事务

    (1).引入相关依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid< ...

  2. npm 无法安装 ionic 解决办法

    一般从 node.js官网下载安装完之后,npm也会同时安装完. 如果通过 $ npm install -g cordova ionic 去安装,往往会失败.这个是由于GFW,很多插件下载不下来,还好 ...

  3. 关于GTID模式下备份时 --set-gtid-purged=OFF 参数的实验【转】

    刚刚听了吴老师是复制章节课程,对于GTID模式下备份数据--set-gtid-purged=OFF 参数有些不理解,于是乎做了实验,加深理解,得出些结论,如有错漏请批评指正! 部分备份: [root@ ...

  4. Python3学习笔记19-继承和多态

    在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承, 新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base class.Sup ...

  5. redis学习笔记(面试题)

    1. 什么是redis Redis是一个数据库,他和我们传统的oracle数据库差别是它是基于内存的数据库:因为是基于内存,所以效率就高,在某些场景下就可以对我们传统的关系型数据库做一个补充 2. r ...

  6. igmpproxy启动时错误:There must be at least 2 Vif's where one is upstream.

    openwrt 下启动igmpproxy时报错 # /etc/init.d/igmpproxy start Not starting instance igmpproxy::instance1, an ...

  7. Ex 5_32 一台服务器当前有n个等待服务的顾客...第八次作业

    设第i个客户需要等待的时间为ti,则n个客户需要总的等待时间为 ,因此,要使T最小,则要使 即可,所以,对所有的ti按升序进行排序和服务将得到最小的等待时间. package org.xiu68.ch ...

  8. SonarQube代码质量管理工具的升级(sonarqube6.2 + sonar-scanner-2.8 + MySQL5.6+)

    SonarQube升级注意事项 0. 前提条件 如果之前是使用sonarqube5.2 + sonar-runner-2.4 +MySQL5.5版本或者类似的组合. 安装方法请参照SonarQube代 ...

  9. tsconfig.json配置

    什么工具看什么官网-一般都会有说明的 https://www.tslang.cn/docs/handbook/tsconfig-json.html 概述 如果一个目录下存在一个tsconfig.jso ...

  10. [主席树 强制在线]ZOJ3888 Twelves Monkeys

    题意:有n年,其中m年可以乘时光机回到过去,q个询问 下面m行,x,y 表示可以在y年穿越回x年, 保证y>x 下面q个询问, 每个询问有个年份k 问的是k年前面 有多少年可以通过一种以上($\ ...