SpringBoot集成milo读写OPC UA

SpringBoot集成milo读写OPC UA

OPC UA客户端工具UaExpert使用

OPC UA客户端工具Softing OPC Client使用_推荐使用

Java使用milo读写OPC UA源代码示例

maven引入依赖

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

读写的伪代码

{
	"opcua": {
		"appUri": "",
		"appName": "",
		"password": "opcua",
		"username": "opcua",
		"endPointUrl": "opc.tcp://192.168.3.5:49320",
		"namespaceIndex": "2",
		"requestTimeout": "5000"
	}
}

private OpcUaDto dto;

private static OpcUaClient opcUaClient;

private OpcUaClient getOpcUaClient() {
        if (opcUaClient == null) {
            log.info("初始化OPC UA Client......");
            try {
                IdentityProvider identityProvider;
                if (!StringUtil.isNull(dto.getUsername()) && !StringUtil.isNull(dto.getPassword())) {
                    identityProvider = new UsernameProvider(dto.getUsername(), dto.getPassword());
                } else {
                    identityProvider = new AnonymousProvider();
                }
                opcUaClient = OpcUaClient.create(
                        dto.getEndPointUrl(),
                        endpoints ->
                                endpoints.stream()
                                        .findFirst(),
                        configBuilder ->
                                configBuilder
                                        .setIdentityProvider(identityProvider)
                                        .setRequestTimeout(uint(dto.getRequestTimeout()))
                                        .build()
                );
                log.info("初始化OPC UA Client......成功");
            } catch (Exception e) {
                log.error("初始化OPC UA Client失败, {}", e.getMessage());
                return null;
            }
        }
        if (!opcUaClient.getSession().isDone()) {
            try {
                // synchronous connect
                opcUaClient.connect().get();
                log.info("OPC UA Client连接connect成功");
            } catch (Exception e) {
                log.error("OPC UA Client连接connect失败, {}", e.getMessage());
                opcUaClient.disconnect();
                opcUaClient = null;
                return null;
            }
        }
        return opcUaClient;
    }


public void read() {
		String item = "tongdao.tag1.aaa";
		NodeId nodeId = new NodeId(dto.getNamespaceIndex(),
                            Item);
                    DataValue value = opcUaClient.readValue(0.0, TimestampsToReturn.Both, nodeId).get();
                    if (value.getValue() == null) {
                        log.error("OPC UA字段读取为空, code={}", Item);
                    }
		System.out.println(value.getValue().getValue());
}	

public void write() {
		//写入值
        int v = 1;
	String item = "tongdao.tag1.aaa";
        NodeId nodeId = new NodeId(dto.getNamespaceIndex(), item);
        Variant value = new Variant(v);
        DataValue dataValue = new DataValue(value,null,null);

        StatusCode statusCode = opcUaClient.writeValue(nodeId,dataValue).get();

        System.out.println(statusCode.isGood());

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值