主要内容

Acquire Data from an OPC Historical Data Access Server

This example shows you how to acquire data from an OPC Historical Data Access (HDA) server.

先决条件:

在服务器上启动历史数据记录

笔记:You do not normally need to execute this step on a production server.

本示例使用模拟服务器,该服务器仅记录使用OPC数据访问客户端订阅的项目的历史数据。从垫子文件加载客户端对象并重新连接客户端。

daObjs = load('opcdemohdaconfigure.mat');connect(daObjs.opcdemoHDAConfigure);

Wait a while for the server to log some data.

暂停(10);

Create an OPC HDA Client Object

Create an OPC HDA Client associated with the OPC HDA Server.

hdaObj = opchda('localhost','matrikon.OPC.Simulation')
hdaObj = OPC HDA Client localhost/matrikon.OPC.Simulation: Host: localhost ServerID: matrikon.OPC.Simulation Timeout: 10 seconds Status: disconnected Aggregates: -- (client is disconnected) ItemAttributes: -- (client is disconnected)

The client object manages the connection with the server, allows you to retrieve information about the server, browse the server name space, and to read data stored on the server.

At this point, the client is not yet connected to the server. Connect the client to the server.

connect(hdaObj);

To confirm that the client is connected, display the clientStatusproperty.

hdaobj.status
ans =连接

Define Items of Interest

This example uses theReal8items fromSaw-toothed Waves和theReal8UInt2items from随机的. Make a cell array of item names for ease-of-use.

itmIDs = {'锯齿波。Real8',...'Random.Real8',...'Random.UInt2'};

从服务器读取原始数据

在过去的一天中读取历史服务器的原始数据值。

data = readraw(hdaobj,itmids,now-1,现在)
data = 1-by-3 OPC HDA Data object: ItemID Value Start TimeStamp End TimeStamp Quality ----------------------- --------------- ----------------------- ----------------------- ---------------------- Saw-toothed Waves.Real8 9 double values 2016-04-12 16:30:17.662 2016-04-12 16:30:25.776 1 unique quality [Raw] Random.Real8 9 double values 2016-04-12 16:30:17.662 2016-04-12 16:30:25.776 1 unique quality [Raw] Random.UInt2 9 uint16 values 2016-04-12 16:30:17.662 2016-04-12 16:30:25.776 1 unique quality [Raw] Use the showValues method to display all values.

Note:The Matrikon server retains only the last 200 simulated values for each item.

Display the values of the first data element.

showValues(data(1))
OPC HDA Data object for item Saw-toothed Waves.Real8: TIMESTAMP VALUE QUALITY ======================= ============= ========== 2016-04-12 16:30:17.662 3.141593 Raw (Good) 2016-04-12 16:30:18.677 6.283185 Raw (Good) 2016-04-12 16:30:19.692 9.424778 Raw (Good) 2016-04-12 16:30:20.707 12.566371 Raw (Good) 2016-04-12 16:30:21.717 15.707963 Raw (Good) 2016-04-12 16:30:22.732 18.849556 Raw (Good) 2016-04-12 16:30:23.747 21.991149 Raw (Good) 2016-04-12 16:30:24.761 25.132741 Raw (Good) 2016-04-12 16:30:25.776 28.274334 Raw (Good)

从服务器读取处理的数据

查询Aggregatesproperty of the HDA Client object to find out what aggregate types the server supports.

hdaObj.Aggregates
ANS = OPC HDA聚合类型:名称ID描述------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------插值1检索插值值。及时4在重新样本间隔中检索时间加权平均数据。最小值时间7在重新样本间隔和最小值的时间戳中检索最小值。至少8在重新样本间隔中检索最小值。最大程度的时间9在重新样本间隔和最大值的时间戳中检索最大值。最多10在重新样本间隔中检索最大值。

The Matrikon server supports the time weighted average value, so we will use that aggregate type on 10 seconds of data for the last 1 minute. Note below how theAggregates属性可用于指定聚合类型。

pdata = ReadProcessed(HDAOBJ,ITMIDS,HDAOBJ.AGGREGATES.timeavering,10,现在1/24/60,现在)'
pData = 1-by-3 OPC HDA Data object: ItemID Value Start TimeStamp End TimeStamp Quality ----------------------- --------------- --------------------------------------------------------------------------- ----------------------------- Saw-toothed Waves.Real8 6 double values 2016-04-12 16:29:26.840 2016-04-12 16:30:16.840 1独特质量[计算] Random.Real8 6 Double值2016-04-12 16:29:29:26.840 2016-04-12 16:30:16.840 1独特质量[计算]随机质量.UINT2 6 UINT16值2016-04-12 16:29:29:26.840 2016-04-12 16:30:16.840 1唯一质量[计算]使用ShowValues方法显示所有值。

显示随机的.Real8item.

itmind = getIndexfromid(pdata,'Random.Real8');Showvalues(pdata(itmind))
opc hda数据对象for intocy random.real8:时间戳值质量==================================================================== 2016-04-12 16:29:29:26.840 5073.986117计算(不确定)2016-04-12 16:29:29:36.840 5073.986074(不确定))2016-04-12 16:29:46.840 5073.986105计算(不确定)2016-04-12 16:29:29:56.840 5073.986137计算(不确定)2016-04-12 16:30:30:30:06.840 5073.9862227 calling(nose)12 16:30:16.840 7322.794889计算(不确定)

The last value has a quality of'Uncertain'because the time interval is not a complete 10 seconds.

Clean Up

When you have finished with the OPC Toolbox™ objects, delete them from the OPC Toolbox engine. Although deleting an HDA Client object automatically disconnects the object from the server, this example explicitly shows it.

disconnect(hdaObj) delete(hdaObj) disconnect(daObjs.opcdemoHDAConfigure); delete(daObjs.opcdemoHDAConfigure);

The client object is now invalid.

isvalid (hdaObj)
ans = 0