OPC UA java 代码示例
时间: 2024-05-23 15:08:46 浏览: 16
OPC UA(Open Platform Communications Unified Architecture)是一种用于工业自动化领域的通信协议。在Java语言中,可以使用Eclipse Milo库来实现OPC UA通信。下面是一个简单的Java示例代码,演示如何使用Eclipse Milo库读取OPC UA服务器中的节点值。
```java
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.OpcUaSubscription;
import org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import java.util.concurrent.ExecutionException;
public class OpcUaClientExample {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// 创建 OPC UA 客户端
OpcUaClient client = OpcUaClient.create("opc.tcp://localhost:12686/example");
// 连接服务器
client.connect().get();
// 创建订阅
OpcUaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
// 读取变量节点的值
UaVariableNode node = client.getAddressSpace().getVariableNode("ns=2;s=MyVariable").get();
DataValue value = node.readValue().get();
System.out.println("Value = " + value.getValue());
// 断开连接
client.disconnect().get();
}
}
```
此代码示例连接到本地主机上的OPC UA服务器,并读取名为"MyVariable"的变量节点的值。如果您需要更详细的代码示例,请查看Eclipse Milo库的官方文档或GitHub存储库。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)