这个例子展示了如何使用OPC工具箱™来浏览OPC历史数据访问服务器的网络,并使用OPC工具箱函数来查询服务器项目及其属性的服务器名称空间。
先决条件:
您使用opchdaserverinfo
功能查询网络上可用的OPC历史数据访问服务器的主机。本例使用本地主机。
hostInfo = opchdaserverinfo (“localhost”)
hostInfo = OPC HDA服务器信息对象:Host: localhost ServerID: Matrikon.OPC.Simulation.1描述:模拟和测试MatrikonOPC服务器HDA1
找到以Matrikon开头的服务器信息条目。
hIndex = findDescription (hostInfo,Matrikon的) hostInfo (hIndex)
hIndex = 1 ans = OPC HDA服务器信息对象:Host: localhost ServerID: Matrikon.OPC.Simulation.1描述:模拟和测试MatrikonOPC服务器HDA1
使用ServerInfo
对象,用于构造客户端对象。
hdaObj = opchda (hostInfo (hIndex));
您还可以直接指定主机名和服务器ID。
hdaObj = opchda (“localhost”,“Matrikon.OPC.Simulation.1”)
hdaObj = OPC HDA Client localhost/Matrikon.OPC.Simulation. hdaObj = OPC1: Host: localhost ServerID: Matrikon.OPC.Simulation.1超时:10秒状态:断开连接聚合:——(客户端断开连接)ItemAttributes:——(客户端断开连接)
将客户端连接到服务器。
连接(hdaObj);
检索服务器的名称空间。
ns = getNameSpace (hdaObj)
ns = 4×1 struct array with fields: Name fulllyqualifieddid NodeType节点类型
该结构的每个元素都是服务器名称空间中的一个节点。
ns (1)
ans = Name: '模拟项目' fulllyqualifieddid: '模拟项目¥' NodeType: 'branch'节点:[8×1 struct]
使用serveritems
函数查找包含字符串的名称空间中的所有项真正的
.
realItems = serveritems (ns,“真正* *”)
realItems = '桶旅。ArrayOfReal8‘斗链式。Real4‘斗链式。Real8“随机的。ArrayOfReal8“随机的。Real4“随机的。Real8”'Read Error.ArrayOfReal8' 'Read Error.Real4' 'Read Error.Real8' 'Saw-toothed Waves.Real4' 'Saw-toothed Waves.Real8' 'Square Waves.Real4' 'Square Waves.Real8' 'Triangle Waves.Real4' 'Triangle Waves.Real8' 'Write Error.ArrayOfReal8' 'Write Error.Real4' 'Write Error.Real8' 'Write Only.ArrayOfReal8' 'Write Only.Real4' 'Write Only.Real8'
检查发现的第十项的当前正常最大值。
maxVal = readItemAttributes (hdaObj realItems {10}, hdaObj.ItemAttributes.NORMAL_MAXIMUM,现在,现在)
警告:锯齿状的波。真正的4: No history available for attribute. maxVal = ItemID: 'Saw-toothed Waves.Real4' AttributeID: 11 Timestamp: 7.3643e+05 Value: 100
警告表示项目尚未存储在历史数据库中,但将返回预先配置的项目属性。
断开客户端与服务器的连接,并在不再需要OPC工具箱对象时从内存中删除它们。删除客户端对象也会删除组和项对象。
断开(hdaObj)删除(hdaObj)