OPC UA JAVA开发笔记(五):milo nodeparser解析XML文件获取结点集

这篇博客介绍了如何使用milo的nodeparser模块解析OPC UA的XML文件以获取节点集。作者强调了这个功能在开源OPC库中的稀缺性,并提供了详细步骤,包括克隆milo源码,使用XSD生成Java类,修改import路径,创建CncNamespace类以及实现解析NodeSet的方法。通过这个过程,读者可以学习到如何自动生成和处理OPC UA节点对象。
摘要由CSDN通过智能技术生成

市面上各种收费的UA软件都有一个诱人的功能,那就是直接解析XML获取结点集,而开源的OPC库中,据我所知只有open62541有,这可以极大的简化我们的流程,并且OPC UA Foundation已经建立了相应的NodeSet文件。

这里我们采用milo的nodeparser来解析XML文件。
版本要求是milo的-0.4.0-SNAPSHOT或以上

<dependency>
            <groupId>org.eclipse.milo</groupId>
            <artifactId>sdk-server</artifactId>
            <version>0.4.0-SNAPSHOT</version>
        </dependency>

本文发布时暂没有相应的版本发布,大家可以等一下0.4版本发布,或者和我一样直接在Github上已有的源码基础上更改。

我们将这个地址的代码clone到本地nodeparser-milo
克隆好后,我们需要先做一个步骤。将克隆后的一个文件 UANodeSet.xsd 复制到任意位置,比如我是在桌面。然后我们通过xsd来生成对应的Java文件,详情可参考XSD生成java文件

如果不管用就再找找其他教程,核心就是通过xsd生成java类

接下来将nodeparser中的以下文件,复制到milo的module server-example 中。
在这里插入图片描述
这里的generated文件下的类就是我们通过xsd生成的所有类。

复制完后,肯定会飘红,主要是要改一下import的目录,把以前的

  • import org.opcfoundation.ua.generated
  • 统统改成 import nodeset.generated.;

其实就是把generated文件夹下的import修正一下
还有就是这里的NodeUtils是后面创建的,现在先不管

接着我们新建一个CncNamespace的类
在这里插入图片描述
内容如下:

public class CNCNamespace extends ManagedNamespace {
   

    public static final String NAMESPACE_URI = "urn:eclipse:milo:CNC";

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final Random random = new Random();

    private final DataTypeDictionaryManager dictionaryManager;

    private final SubscriptionModel subscriptionModel;

    CNCNamespace(OpcUaServer server) {
   
        super(server, NAMESPACE_URI);

        subscriptionModel = new SubscriptionModel(server, this);

        dictionaryManager = new DataTypeDictionaryManager(getNodeContext(), NAMESPACE_URI);
    }

    @Override
    protected void onStartup() {
   
        super.onStartup();

        dictionaryManager.startup();
        subscriptionModel.startup();

        // Create a "HelloWorld" folder and add it to the node manager
        NodeId folderNodeId = newNodeId("CNC");

        UaFolderNode folderNode = new UaFolderNode(
                getNodeContext(),
                folderNodeId,
                newQualifiedName("CNC"),
                LocalizedText.english("CNC")
        );

        getNodeManager().addNode(folderNode);

        // Make sure our new folder shows up under the server's Objects folder.
        folderNode.addReference(new Reference(
                folderNode.getNodeId(),
                Identifiers.Organizes,
                Identifiers.ObjectsFolder.expanded(),
                false
        ));

        addCNCNodes();

        // Set the EventNotifier bit on Server Node for Events.
        UaNode serverNode = getServer()
                .getAddressSpaceManager()
                .getManagedNode(Identifiers.Server)
                .orElse(null);

        if (serverNode instanceof ServerTypeNode) {
   
            ((ServerTypeNode) serverNode).setEventNotifier(ubyte(1))
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是基于Milo SDK的Java代码示例,连接到OPC UA服务器并读取节点值: ```java import org.eclipse.milo.opcua.sdk.client.OpcUaClient; import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig; import org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider; import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy; import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription; import org.eclipse.milo.opcua.stack.core.types.structured.UserTokenPolicy; import java.util.List; import java.util.concurrent.CompletableFuture; public class OpcUaClientExample { public static void main(String[] args) throws Exception { // 连接参数 String endpointUrl = "opc.tcp://localhost:12685/example"; String username = "user"; String password = "password"; // 创建OpcUaClientConfig OpcUaClientConfig config = OpcUaClientConfig.builder() .setEndpoint(getEndpoint(endpointUrl)) .setIdentityProvider(getIdentityProvider(username, password)) .setSecurityPolicy(SecurityPolicy.Basic256Sha256) .setRequestTimeout(uint(5000)) .build(); // 创建OpcUaClient OpcUaClient client = OpcUaClient.create(config); // 连接服务器 CompletableFuture<Void> future = client.connect(); future.get(); // 读取节点值 String nodeToRead = "ns=2;s=MyVariable"; CompletableFuture<DataValue> readFuture = client.readValue(0, TimestampsToReturn.Both, nodeToRead); DataValue value = readFuture.get(); System.out.println("Value: " + value.getValue().getValue()); // 断开连接 CompletableFuture<Void> closeFuture = client.disconnect(); closeFuture.get(); } /** * 获取EndpointDescription */ private static EndpointDescription getEndpoint(String endpointUrl) throws Exception { List<EndpointDescription> endpoints = DiscoveryClient.getEndpoints(endpointUrl).get(); EndpointDescription endpoint = endpoints.stream() .filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.Basic256Sha256.getUri())) .findFirst().orElseThrow(() -> new Exception("no desired endpoints returned")); return endpoint; } /** * 获取IdentityProvider */ private static UsernameProvider getIdentityProvider(String username, String password) { UserTokenPolicy userTokenPolicy = new UserTokenPolicy(null, null, null, null); return new UsernameProvider(username, password, userTokenPolicy); } /** * 将int转为UnsignedInteger */ private static UnsignedInteger uint(int value) { return Unsigned.uint(value); } } ``` 在这个示例中,我们先创建了一个OpcUaClientConfig对象,该对象包含了连接OPC UA服务器的参数,如服务器地址、用户名、密码、安全策略等。然后创建一个OpcUaClient对象,并使用connect()方法连接服务器。之后,我们使用readValue()方法读取指定节点的值,并使用disconnect()方法断开连接。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值