在Java中使用Milo可以实现实时通信功能,以下是一个简单的示例:
<dependency> <groupId>org.eclipse.milo</groupId> <artifactId>milo-client-opcua-stack</artifactId> <version>0.6.6</version> </dependency>
String endpointUrl = "opc.tcp://localhost:4840"; OpcUaClient client = new OpcUaClient(endpointUrl); client.connect().get();
NodeId nodeId = new NodeId(2, "HelloWorld/ScalarTypes/Int32"); DataValue value = client.readValue(0, TimestampsToReturn.Both, nodeId).get(); System.out.println("Value: " + value.getValue().getValue().toString());
MonitoredItemCreateRequest request = new MonitoredItemCreateRequest( new ReadValueId(nodeId, AttributeId.Value.uid(), null, QualifiedName.NULL_VALUE), MonitoringMode.Reporting, new MonitoringParameters( UInt32.valueOf(1), Double.valueOf(0.0), null, UInt32.valueOf(10), true ) ); client.getSubscriptionManager().createSubscription(1000.0).get(); client.getSubscriptionManager().createMonitoredItems( TimestampsToReturn.Both, Collections.singletonList(request), (item, id) -> System.out.println("Value changed: " + item.getValue().getValue().getValue()) ).get();
通过以上步骤,您可以实现基于Java Milo的实时通信功能。更多关于Java Milo的文档可以在官方网站上找到:https://github.com/eclipse/milo