Java 使用 OPC UA

使用Java与OPC UA (OPC Unified Architecture) 进行通信,可以使用开源的Eclipse Milo库。以下是一个简单的指南,帮助你开始使用Eclipse Milo库与OPC UA服务器进行交互。

步骤一:引入Eclipse Milo库

你需要在项目中引入Eclipse Milo库。可以使用Maven或Gradle来管理依赖。

Maven依赖:

<dependency>
    <groupId>org.eclipse.milo</groupId>
    <artifactId>sdk-client</artifactId>
    <version>0.6.7</version>
</dependency>

Gradle依赖:

implementation 'org.eclipse.milo:sdk-client:0.6.7'

步骤二:创建并配置OPC UA客户端

下面是一个简单的示例代码,展示如何创建并配置一个OPC UA客户端,以及如何连接到OPC UA服务器。

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescriptionArray;
import org.eclipse.milo.opcua.stack.core.util.EndpointUtil;

import java.util.concurrent.CompletableFuture;

public class OpcUaClientExample {
    public static void main(String[] args) throws Exception {
        String endpointUrl = "opc.tcp://localhost:4840";  // 替换为你的OPC UA服务器地址
        
        // 找到服务器的端点
        EndpointDescription[] endpoints = OpcUaClient.getEndpoints(endpointUrl).get();

        // 选择合适的端点
        EndpointDescription endpoint = EndpointUtil.select(endpoints, "None", "None").orElseThrow(() ->
                new Exception("没有找到合适的端点"));

        // 创建客户端
        OpcUaClient client = OpcUaClient.create(endpoint);

        // 连接到服务器
        CompletableFuture<Void> future = client.connect();

        future.whenComplete((result, ex) -> {
            if (ex != null) {
                ex.printStackTrace();
            } else {
                System.out.println("连接成功!");
                // 在此添加你的业务逻辑
            }
        });

        future.get(); // 等待连接完成
    }
}

步骤三:读取和写入变量

下面是如何读取和写入OPC UA服务器上的变量的示例。

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;

import java.util.concurrent.CompletableFuture;

public class ReadWriteExample {
    public static void main(String[] args) throws Exception {
        OpcUaClient client = OpcUaClient.create("opc.tcp://localhost:4840").get();
        client.connect().get();

        NodeId nodeId = new NodeId(2, "MyVariable");

        // 读取变量值
        CompletableFuture<DataValue> readFuture = client.readValue(0, null, nodeId);
        DataValue value = readFuture.get();
        System.out.println("读取到的值: " + value.getValue().getValue());

        // 写入变量值
        Variant variant = new Variant(42); // 替换为你要写入的值
        DataValue newValue = new DataValue(variant);
        CompletableFuture<Void> writeFuture = client.writeValue(nodeId, newValue);
        writeFuture.get();
        System.out.println("值已写入");
    }
}

总结

使用Eclipse Milo库可以方便地与OPC UA服务器进行通信。通过上述步骤,你可以快速开始使用Java与OPC UA进行交互。可以根据需求扩展和定制客户端的功能,如订阅数据更改、浏览节点等。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值