[英]How to read opc-ua variable in java with hurence
I'm try to read a variable from a OPC-UA server.我正在尝试从 OPC-UA 服务器读取变量。 I'm using com.hurence.opc library.
我正在使用 com.hurence.opc 库。 My code is
我的代码是
OpcUaConnectionProfile connectionProfile = new OpcUaConnectionProfile()
.withConnectionUri(URI.create("opc.tcp://localhost:62541/Quickstarts/ReferenceServer"))
.withClientIdUri("imp:ladisa").withClientName("GestionaleLadisa")
.withSocketTimeout(Duration.ofSeconds(15));
// Create an instance of a ua operations
OpcUaOperations opcUaOperations = new OpcUaTemplate();
// connect using our profile
opcUaOperations.connect(connectionProfile).doOnError(throwable -> System.out.println(throwable.getMessage()))
.ignoreElement().blockingAwait();
OpcUaSessionProfile sessionProfile = new OpcUaSessionProfile()
//the publication window
.withPublicationInterval(Duration.ofMillis(100));
try (OpcSession session = opcUaOperations.createSession(sessionProfile).blockingGet()) {
List<OpcData> result = session.read("Objects.CTT.Scalar.Scalar_Instructions").blockingGet();
for(var dt:result) {
System.out.println( dt.getValue());
}
}
I'm testing with opc server reference implementation.我正在使用 opc 服务器参考实现进行测试。 But I'm not able to reference the variable and I get an Exception
但是我无法引用该变量并且出现异常
java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:141)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
What is the correct way to point to a node in the OPC-UA tree?指向 OPC-UA 树中节点的正确方法是什么?
The way I found is:我找到的方法是:
List<OpcData> result = session.read("ns=2;s=Scalar_Instructions").blockingGet();
using nodeId.使用节点 ID。 I hope that this can help somebody.
我希望这可以帮助某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.