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. SpringAOP+注解实现简单的日志管理

    今天在再次深入学习SpringAOP之后想着基于注解的AOP实现日志功能,在面试过程中我们也经常会被问到:假如项目已经上线,如何增加一套日志功能?我们会说使用AOP,AOP也符合开闭原则:对代码的修改 ...

  2. Java导出List集合到txt文件中——(四)

    有时候,需要将数据以一定格式导出到txt文件中.利用Java的IO可以轻松的导出数据到txt中. package Action.txt; import java.io.BufferedWriter; ...

  3. Linux时间日期类指令

    ⒈date [Options] 显示/设置当前日期 基本语法: date 显示当前时间 date +"%Y" 显示当前年份 date +"%m" 显示当前月份 ...

  4. 020_秘钥管理服务器vault

    一. https://github.com/hashicorp/vault     #待研究

  5. KVM -> 虚拟机管理&console登录_02

    1.KVM虚拟机管理操作 virsh命令常用参数总结 1.开机关机: virsh list (只可以查看运行的虚拟机) virsh list  --all (全部都可以查看) 开机与关机: virsh ...

  6. java多线程快速入门(十六)

    ThreadLocal关键字实现每个线程有自己的变量 package com.cppdy; class Number { private int num; public static ThreadLo ...

  7. tomcat 输入学习

    Tomcat学习—Tomcat7 修改/webapps/ROOT发布路径(Linux和windows环境) https://blog.csdn.net/u010648555/article/detai ...

  8. poj1436水平可见线

    还是线段树区间更新,这次不需要对线段离散化,但是要把线段纵坐标*2,可以举例模拟 #include<iostream> #include<cstring> #include&l ...

  9. 安装httpd过程,将网站部署到httpd过程

    1,配置DNSvi /etc/resolv.conf 加入以下代码 nameserver 192.168.0.1 nameserver 8.8.8.8 nameserver 8.8.4.4 2.输入y ...

  10. 【动态规划】Part1

    1. 硬币找零 题目描述:假设有几种硬币,如1.3.5,并且数量无限.请找出能够组成某个数目的找零所使用最少的硬币数. 分析:   dp [0] = 0           dp [1] = 1 + ...