使用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之间的通讯搞得头大,通过几次远 ...
随机推荐
- Java读取Excel转换成JSON字符串进而转换成Java对象
Jar包
- k64 datasheet学习笔记1---概述
1.前言 k64 datasheet描述了Freescale MCU的特性.架构和编程模型,主要是面向使用MCU的系统架构和软件应用开发人员. 2.模块划分 datasheet主要按功能对模块进行划分 ...
- 蓝牙Bluetooth技术手册规范下载【转】
蓝牙Bluetooth技术手册规范下载 http://www.crifan.com/summary_bluetooth_specification_download/ [背景] 之前就已经整理和转帖了 ...
- [ VB ] If 运算符 [ C# ] 条件运算符 (?:)
//保留了原文 ()为大概的意思 VB で使用していた IIf 関数の代わりに VB2008 からは If 演算子 を使用可能となった. また. C# では.条件演算子 (?:) で同等の記述が可 ...
- ubuntu数据库迁移
环境:ubuntu16.04 简介:本教程演示如何从旧数据库服务器服转移到另一个新服务器. 场景:假设你有自己的云服务器安装了WordPress站点,你为了更多的内存和处理能力想升级到新的服务器. 操 ...
- 前端web服务器数据同步方案
概述: 网站采用了web和mysql数据库分离的架构,前端有web1.web2.web3需要对他们进行上传文件同步 方案: 在web2的windows服务器上安装GoodSync软件,利用其双向同步特 ...
- Python-互斥锁 进程间通讯
3.守护进程(**) 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常: AssertionError: daemonic proces ...
- java多线程快速入门(十)
synchonizd解决安全性问题 package com.cppdy; class MyThread6 implements Runnable{ private Integer ticketCoun ...
- LeetCode(57):插入区间
Hard! 题目描述: 给出一个无重叠的 ,按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例 1: 输入: i ...
- poj1436水平可见线
还是线段树区间更新,这次不需要对线段离散化,但是要把线段纵坐标*2,可以举例模拟 #include<iostream> #include<cstring> #include&l ...