使用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 = ; //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的更多相关文章
- 连接opcserver时报错 connecting to OPC Server "****" CoCreateInstance 服务器运行失败
在普通windows系统连接OPCServer可能会报这样的错,排查很长时间,OPCServer跟Client都运行正常,点号录入也正常. 最后发现,其实是OPCServer 与OPCClient 权 ...
- 【干货】如何通过OPC自定义接口来实现客户端数据的读取?
上篇博文分享了我的知识库,被好多人关注,受宠若惊.今天我把我在项目中封装的OPC自定义接口的程序分享一下.下面将会简单简单介绍下OPC DA客户端数据访问,以及搭配整个系统的运行环境. OPC(OLE ...
- 虚拟机安装_1_wincc_matriton
1,安装虚拟机: 选择:win7专业版64位ISO. 2,安装office2016 3,激活系统和OFFICE 4,安装winccV7.4 4.1 安装WINDOWS组件 MSMQ 4.1.1 安装I ...
- OPCServer Modbus使用和配置
一,安装KEPware.Enhanced.OPC.DDE.KEPServer.(PLC数据传送给KEPServer,开发的程序用OPCServer读KEPServer) 设置ip地址后面是指的plc站 ...
- 使用OPC的方式去连接PLC进行AB SLC-5_04数据的采集
1. 必备软件 Rslinx classic 2.57 .net framework 2.0 VS2013 OS: win7 enterprise x64 2. 软件安装 2.1.安装RSlinx ...
- C#l连接OPC进行数据交互
步骤 :引用 OPCNETAPI.DLL&&OPCNETAPI.COM.DLL 1.查询服务器 2. 连接服务器 3. 读取数据 4.写入数据 1.查询服务器 :根 ...
- OPC测试常用的OPCClient和OPCServer软件推荐
各位在进行OPC通讯时,常会遇到两种情况: 1)使用一个OPCClient在同一台计算机上连接远程计算机上的多个OPCServer时,发现某个OPCServer是通畅的,但其他的OPCServer却无 ...
- C#使用OpcNetApi.dll和OpcNetApi.Com.dll操作OPC
本人学习了一下.Net,恰好,51自学网,又要用这个.而网上很多VC6,VB6,VB .Net的但,很少C#的.现在研究一下,给出例子: 测试平台,是VS2008,KEPServer,OpcNetAp ...
- OPCClient和OPCServer在Windows上运行方式的恩怨
http://www.diangon.com/wenku/PLC/201504/00021970.html 近段时间,遇到不少人都被OPCClient与OPCServer之间的通讯搞得头大,通过几次远 ...
随机推荐
- systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。
1.centos 检查服务是否开机自启 (ntpd是原生的服务,mysql是注册的服务) 参考:1.http://man.linuxde.net/systemctl
- (DT系列五)Linux kernel 是怎么将 devicetree中的内容生成plateform_device【转】
转自:https://blog.csdn.net/lichengtongxiazai/article/details/38942033 Linux kernel 是怎么将 devicetree中的内容 ...
- /etc/fstab文件详解【转】
******************************************************************************* 有很多人经常修改/etc/fstab文件 ...
- System.Runtime.InteropServices.COMException (0x800A03EC): 无法访问文件
使用Microsoft.Office.Interop.Excel 操作 今天在服务器部署,操作程序csv文件转xsl文件的时候,遇到一下问题: System.Runtime.InteropServic ...
- Oracle 正则表达式函数-REGEXP_REPLACE
背景 当初写oracle的一个存储过程,以前不知道sql里也有正则表达式,关于正则表达式教程很多了,这里只是记录下Oracle也有这个功能,下次再有类似需求用这个处理的确方便很多. 想起存储过程,就想 ...
- mysql数据库基于LVM快照的备份
lvm-snapshot: 基于LVM快照的备份 1.事务日志跟数据文件必须在同一个卷上 2.创建快照卷之前,要请求mysql的全局锁,在快照创建完成之后释放锁 3 ...
- svn使用小技巧
在使用svn的时候,往往导入到eclipse中,使用eclipse进行提交修改,但是当删除文件夹的时候,eclipse会发生冲突,不能够提交. 使用一种方式,将svn上的项目跟你eclipse对应项目 ...
- LeetCode(32):最长有效括号
Hard! 题目描述: 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 ...
- Gym100340 线性dp
//看题解写的 https://blog.csdn.net/sdfzyhx/article/details/51804748#include<bits/stdc++.h> using na ...
- 性能测试三十八:Java性能分析神器-JProfiler安装和简单介绍
Jprofiler是一个重量级的工具,需要分别在服务器和windows都装客户端,会损耗性能,用于发现问题后排查问题,而不是常规的监控 JPROFILER工具下载地址:http://www.ej-te ...