这个示例向您展示了如何使用OPC Toolbox™来浏览网络中的OPC服务器,并查询服务器项及其属性的服务器名称空间。
先决条件:
您使用opcserverinfo
功能查询网络上可用的OPC数据访问服务器的主机。本例使用本地主机。
hostInfo = opcserverinfo (“localhost”)
hostInfo = Host: 'localhost' ServerID: {'Matrikon.OPC.Simulation. 'Matrikon.OPC.Simulation. 'Matrikon.OPC.Simulation. '1'} ServerDescription: {'MatrikonOPC Server for Simulation and Testing'} OPCSpecification: {'DA2'} ObjectConstructor: {'opcda('localhost', 'Matrikon.OPC.Simulation.1')'}
返回的结构提供了关于每个服务器的信息:
hostInfo。ServerDescription”
ans = '模拟和测试的MatrikonOPC服务器'
以及用于创建客户端对象的Server ID。
allID = hostInfo。ServerID”
allID = ' Matrikon.OPC.Simulation.1 '
使用上一步中找到的主机名和服务器ID来构造客户机对象。
da = opcda (“localhost”,“Matrikon.OPC.Simulation.1”)
da = OPC数据访问客户端对象概要:localhost/Matrikon.OPC.Simulation.1服务器参数主机:localhost ServerID: Matrikon.OPC.Simulation.1状态:disconnected超时:10秒对象参数组:0-by-1 dagroup对象事件日志:1000个事件中的0个
将客户端连接到服务器。
连接(da);
检索服务器的名称空间。
ns = getnamespace (da)
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'
检查规范数据类型(PropID = 1
)及查阅资料的权利(PropID = 5
)。
accessRights = serveritemprops(da,realItems{2},1)
PropID: 1 PropDescription: 'Item Canonical DataType' PropValue: 'single' PropItemID: " accessRights = PropID: 5 PropDescription: 'Item Access Rights' PropValue: 'read/write' PropItemID: "
断开客户端与服务器的连接,并在不再需要OPC工具箱对象时从内存中删除它们。删除客户端对象也会删除组和项对象。
断开(da)删除(da)