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. 【Udacity并行计算课程笔记】- Lesson 2 The GPU Hardware and Parallel Communication Patterns

    本小节笔记大纲: 1.Communication patterns gather,scatter,stencil,transpose 2.GPU hardware & Programming ...

  2. 启动apache 提示Starting httpd: AH00558

    Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain nam ...

  3. Informatic学习总结_day02_增量抽取

    SELECT EMP.EMPNO, EMP.ENAME, EMP.JOB, EMP.MGR, EMP.HIREDATE, EMP.SAL, EMP.COMM, EMP.DEPTNO FROM EMP ...

  4. move_base

    1>准备导航所需要的包. a.ros-indigo-gampping :我们不需要修改包内的东西,所以直接安装可执行文件就好了. sudo apt-get install ros-indigo- ...

  5. dubbo 一个接口多个实现

    <dubbo:service ref="dubboGroupService1" interface="com.yncp.dubbo.service.IDubboGr ...

  6. 最新 macOS Sierra 10.12.3 安装CocoaPods及使用详解

    一.什么是CocoaPods 每种语言发展到一个阶段,就会出现相应的依赖管理工具,例如 Java 语言的 Maven,nodejs 的 npm.随着 iOS 开发者的增多,业界也出现了为 iOS 程序 ...

  7. win10下vscode配置sftp

    sftp配置 1.在vscode中下载sftp插件 在vscode中快捷键 ctrl+shift+P 打开指令窗口,输入extension:install,回车,左侧即打开扩展安装的界面  在搜索框中 ...

  8. Deep Learning系统实训之一:深度学习基础知识

    K-近邻与交叉验证 1 选取超参数的正确方法是:将原始训练集分为训练集和验证集,我们在验证集上尝试不同的超参数,最后保留表现最好的那个. 2 如果训练数据量不够,使用交叉验证法,它能帮助我们在选取最优 ...

  9. hdu5672 尺取

    因为没有初始化ans搞了一晚上 还是尺取,枚举所有l 然后寻找对应满足条件的r,这个串可以被后面所有的串包含,所以每个l 的贡献就是len-r+1 #include<bits/stdc++.h& ...

  10. hdu4638 莫队算法

    莫队算法基础题,题目看懂就能做出来 #include<iostream> #include<cstring> #include<cstdio> #include&l ...