Opcua学习笔记-java-milo框架1-启动server

milo框架

milo是java的实现的一个opcua协议的框架,提供了server和client,结合上几个章节学习的关于opcua的一些基础理论知识,今天通过代码

来实践一下

启动一个opcua server

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

运行后,控制台如下:

在这里插入图片描述

可以看到,此次会有两个namespace,一个是opcua基金会占用的 namespaceindex=0的,一个是我们自定义的namespaceindex=1的;

NameSpaceTable

在这里插入图片描述
可以看到,初始化的时候,uriTable会插入index=0的地址空间

在这里插入图片描述

ua专家链接

我们使用ua专家链接,看看目前server上都有哪些东西

在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是基于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()方法断开连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脚本大神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值