编写一个OpcUaClient客户端的java示例
时间: 2024-02-25 10:54:10 浏览: 147
以下是一个简单的Java OPC UA客户端示例代码:
```java
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
public class OPCUAClient {
public static void main(String[] args) throws Exception {
String endpointUrl = "opc.tcp://localhost:12686/example";
OpcUaClient client = new OpcUaClient(endpointUrl);
client.setIdentityProvider(new UsernameProvider("user", "password"));
CompletableFuture<Void> future = client.connect().thenCompose(v -> {
NodeId nodeId = new NodeId(2, "/Static/AllProfiles/Scalar/Int32");
return client.readValue(0, TimestampsToReturn.Both, nodeId);
}).thenCompose(dataValue -> {
System.out.println("Value: " + dataValue.getValue().getValue());
return client.disconnect();
}).exceptionally(ex -> {
ex.printStackTrace();
return null;
});
future.get();
}
}
```
请注意,此示例仅用于演示目的。在实际应用中,您需要根据您的需要进行修改。
阅读全文