OPC Server示例教程:VC#和VB.NET中的简单API

OPC Server是一套利用微软的COM/DCOM技术实现工业自动化资料获取的架构。OPC Server提供OPC接口,它将与之相连的物理设备(PLC)的信息值通过接口返回到客户端应用程序。也就是说,客户端通过这些接口,可以获得与OPC Server连接的物理设备的信息。对于集成应用程序,只要支持OPC接口,就能轻易访问物理设备,而无需相关的技术信息。 程序设计者可以使用相同的程序代码,操作不同的硬件装置,充分达成软件复用的目的。

OPC Server 最新版下载

此API可以轻松地从VC#和VB.NET opc客户端一次读取和写入数据。

简单的API列表

在C#的简单API的DLL(DxpSimpleAPI.dll)中准备了以下函数。

namespace DxpSimpleAPI
{
  public class DxpSimpleClass
  {
    public DxpSimpleClass();
    public bool Connect(string sNodeName, string sServerName);
    public bool Disconnect();
    public bool EnumServerList(string sNodeName, out string[] sServerNameArray);
    public bool Read(string[] sItemIDArray, out object[] oValueArray, out short[] wQualityArray,
                                            out FILETIME[] fTimeArray, out int[] nErrorArray);
    public bool Write(string[] sItemIDArray, object[] oValArray, out int[] nErrorArray);
  }
}

它主要使用以下四个功能。

连接(连接到OPC服务器)

  • arg1:节点名称(in)
  • arg2:OPC服务器名称(in)
  • 返回:true:成功,false:失败

断开连接(断开与OPC服务器的连接)

  • arg:none 
  • return:true:success,false:failure 

读取(一次性读取)

  • arg1:ItemID(in)

数组arg2:读取值数组(out)

  • arg3:质量数组(out)
  • arg4:时间戳数组(out)
  • arg5:错误数组(out)
  • 返回:true:成功,false:异常错误

写入(一次写入)

扫描二维码关注公众号,回复: 5947344 查看本文章
  • arg1:ItemID(in)
  • 数组arg2:写入值数组(in)
  • arg3:错误数组(out)
  • 返回:true:成功,false:异常错误

OPC服务器的枚举可通过以下函数实现。
EnumServerList(OPC服务器的枚举)

  • arg1:节点名称(in)
  • arg2:安装在指定节点(out)中的OPC服务器名称数组
  • 返回:true:成功,false:失败

DLL的用法

使用Visual Studio创建VC#项目,并添加参考配置。 请从Visual Studio的参考配置的附加菜单中添加对此DLL和OPCRcw.Da.DLL的引用。

请使用using语句定义OpcRcw.Da,如下所示。

using OpcRcw.Da;	

功能的用法

简单的API可以通过以下方式使用。

Connection

// Create OPC access instance
DxpSimpleAPI.DxpSimpleClass opc = new DxpSimpleAPI.DxpSimpleClass();

// Connect:  node name("localhost") and Server Prog.ID("Takebishi.Dxp")
bool ret = opc.Connect("localhost", "Takebishi.Dxp");

Read

// Read 3 points
string[] sItemNameArray = new string[3];
sItemNameArray[0] = "Device1.D0";
sItemNameArray[1] = "Device1.D1";
sItemNameArray[2] = "Device1.D2";
object[] oValueArray;
short[] wQualityArray;
OpcRcw.Da.FILETIME[] fTimeArray;
int[] nErrorArray;

bool ret = opc.Read(sItemNameArray, out oValueArray, out wQualityArray, out fTimeArray, out nErrorArray);

Write

// Write 3 points
object[] oValArray = new object[3];
oValArray[0] = "1234";
oValArray[1] = "5678";
oValArray[2] = "9012";
int[] nErrorArray;

bool ret = opc.Write(sItemNameArray, oValArray, out nErrorArray);

Disconnect

// Disconnect
opc.Disconnect();

实施Demo

VC#客户端(VS2008)的示例程序比以下链接的可下载程序更易于下载。

下载

OPC server

Notes

使用此DLL时,请从Visual Studio 2008开始使用。并且,它需要.NET Framework 3.5作为运行时引擎。

连接目标OPC服务器应对应OPC DA3.0,因为该DLL使用OPC DA3.0的IOPCItemIO接口。

猜你喜欢

转载自blog.csdn.net/xiaochuachua/article/details/89381096
opc