如何在Java中使用Hurence读取opc-ua变量

huangapple go评论105阅读模式
英文:

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(&quot;opc.tcp://localhost:62541/Quickstarts/ReferenceServer&quot;))
			.withClientIdUri(&quot;imp:ladisa&quot;).withClientName(&quot;GestionaleLadisa&quot;)
			.withSocketTimeout(Duration.ofSeconds(15));

	// Create an instance of a ua operations
	OpcUaOperations opcUaOperations = new OpcUaTemplate();
	// connect using our profile
	opcUaOperations.connect(connectionProfile).doOnError(throwable -&gt; 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&lt;OpcData&gt; result = session.read(&quot;Objects.CTT.Scalar.Scalar_Instructions&quot;).blockingGet();
	    	for(var dt:result) {
	    		System.out.println( dt.getValue());
	    	}
	    	
	    	
	    	
	    }		  

如何在Java中使用Hurence读取opc-ua变量

如何在Java中使用Hurence读取opc-ua变量

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&lt;OpcData&gt; result = session.read(&quot;ns=2;s=Scalar_Instructions&quot;).blockingGet();

using nodeId.
I hope that this can help somebody.

huangapple
  • 本文由 发表于 2020年10月21日 04:11:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64452663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定