这个例子展示了如何使用OPC工具箱™浏览OPC历史数据访问服务器的网络,并使用OPC工具箱函数来查询服务器项及其属性的服务器名称空间。
先决条件:
您使用opchdaserverinfo
查询网络中主机是否有可用的OPC历史数据访问服务器。本例以本地主机为例。
hostInfo = opchdaserverinfo (“localhost”)
hostInfo = OPC HDA服务器信息对象:Host: localhost ServerID: Matrikon.OPC.Simulation.1描述:MatrikonOPC服务器模拟和测试hdspec: HDA1
找到以Matrikon开头的描述的服务器信息条目。
Hindex = FindDescription(hostinfo,Matrikon的) hostInfo (hIndex)
hIndex = 1 ans = OPC HDA服务器信息对象:Host: localhost ServerID: Matrikon.OPC.Simulation.1描述:MatrikonOPC服务器模拟和测试hdspec: HDA1
使用ServerInfo
对象,用于构造客户端对象。
hdaObj = opchda (hostInfo (hIndex));
您还可以直接指定主机名和服务器ID。
hdaObj = opchda (“localhost”,“Matrikon.OPC.Simulation.1”)
hdaObj = OPC HDA客户端localhost/ matrikon .OPC.仿真。1: Host: localhost ServerID: Matrikon.OPC.Simulation.1Timeout: 10秒Status: disconnected aggregate:——(客户端断开连接)ItemAttributes:——(客户端断开连接)
将客户端连接到服务器。
连接(hdaObj);
检索服务器的名称空间。
ns = getNameSpace (hdaObj)
ns = 4×1 struct array with fields: Name fullqualifiedid NodeType节点
该结构的每个元素都是服务器名称空间中的一个节点。
ns (1)
ans = Name: '仿真项目' fullqualifiedid: '仿真项目¥' NodeType: 'branch'节点:[8×1 struct]
使用serveritems
函数查找包含字符串的名称空间中的所有项真正的
.
realItems = serveritems (ns,“真正* *”)
realItems = 'Bucket Brigade。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'
检查找到的第10项的当前正常最大值。
maxVal = readItemAttributes (hdaObj realItems {10}, hdaObj.ItemAttributes.NORMAL_MAXIMUM,现在,现在)
警告:Saw-Tooked Waves.real4:没有历史可用于属性。maxval = itemid:'锯齿波.Real4'attributeId:11时间戳:7.3643E + 05值:100
该警告表示该项目尚未存储在历史数据库中,但将返回预配置的项目属性。
断开客户端从服务器的连接,并在不再需要时从内存中删除OPC工具箱对象。删除客户端对象还删除组和项目对象。
断开(hdaObj)删除(hdaObj)