英文:
How to read opc-ua variable in java with hurence
问题
我试图从一个OPC-UA服务器读取一个变量。我正在使用com.hurence.opc库。我的代码如下:
OpcUaConnectionProfile connectionProfile = new OpcUaConnectionProfile()
.withConnectionUri(URI.create("opc.tcp://localhost:62541/Quickstarts/ReferenceServer"))
.withClientIdUri("imp:ladisa").withClientName("GestionaleLadisa")
.withSocketTimeout(Duration.ofSeconds(15));
// 创建一个ua操作的实例
OpcUaOperations opcUaOperations = new OpcUaTemplate();
// 使用我们的配置连接
opcUaOperations.connect(connectionProfile).doOnError(throwable -> System.out.println(throwable.getMessage()))
.ignoreElement().blockingAwait();
OpcUaSessionProfile sessionProfile = new OpcUaSessionProfile()
.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());
}
}
我正在使用OPC服务器的参考实现进行测试。但是我无法引用变量并且遇到了异常:
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)
在OPC-UA树中指向节点的正确方式是什么?
英文:
I'm try to read a variable from a OPC-UA server. I’m using com.hurence.opc library. 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. 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?
答案1
得分: 0
我找到的方法是:
List<OpcData> result = session.read("ns=2;s=Scalar_Instructions").blockingGet();
使用节点ID。
希望这能帮助到某人。
英文:
The way I found is:
List<OpcData> result = session.read("ns=2;s=Scalar_Instructions").blockingGet();
using nodeId.
I hope that this can help somebody.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论