Java 使用 OPC UA
创始人
2024-11-05 06:35:33

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

步骤一:引入Eclipse Milo库

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

Maven依赖:

     org.eclipse.milo     sdk-client     0.6.7  

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 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 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 writeFuture = client.writeValue(nodeId, newValue);         writeFuture.get();         System.out.println("值已写入");     } } 

总结

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

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...