使用Java的Milo框架访问OPCUA服务的过程包括以下步骤:
- 引入依赖
在Maven项目中,需要在pom.xml文件中引入以下依赖:
<dependencies>
<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>milo-client-sdk</artifactId>
<version>0.3.9</version>
</dependency>
<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>milo-sdk-server</artifactId>
<version>0.3.9</version>
</dependency>
</dependencies>
这里我们使用的是Milo的客户端和服务端sdk。
- 创建OPCUA客户端
// 创建 OPCUA client
OpcUaClientConfig config = OpcUaClientConfig.builder()
.setApplicationName(LocalizedText.english("OPCUA-Client"))
.setApplicationUri("urn:localhost:OPCUA:Client")
.setEndpoint(endpoint)
.setRequestTimeout(uint(5000))
.build();
OpcUaClient client = OpcUaClient.create(config);
其中,endpoint
是OPCUA服务的地址。在该步骤中,我们创建了一个OPCUA客户端的配置,然后使用配置创建了一个OPCUA客户端。
- 连接OPCUA服务
// 连接OPCUA服务
client.connect().get();
在此步骤中,我们通过client.connect().get()
方法连接OPCUA服务。
- 读取节点值
// 读取节点值
List<NodeId> nodeIds = new ArrayList<>();
nodeIds.add(new NodeId(2, "/Static/AllProfiles/DeviceSetPoint"));
nodeIds.add(new NodeId(2, "/Dynamic/CoolingWaterInletTemperature"));
Map<NodeId, DataValue> values = client.readValues(0, TimestampsToReturn.Both, nodeIds).get();
for(Map.Entry<NodeId, DataValue> entry : values.entrySet()) {
NodeId nodeId = entry.getKey();
DataValue dataValue = entry.getValue();
System.out.println(nodeId + " value is " + dataValue.getValue().getValue());
}
以上代码展示了如何读取两个节点的值。在readValues
方法中,第一个参数是最大的数据变化历史记录数;第二个参数是时间戳返回模式;第三个参数是Node ID列表。在读取完成后,我们可以迭代values
的条目并输出每个节点的值。
- 写入节点值
// 写入节点值
NodeId nodeId = new NodeId(2, "/Dynamic/CoolingWaterInletTemperature");
DataValue setValue = new DataValue(new Variant(Double.valueOf("50.5")));
StatusCode statusCode = client.writeValue(nodeId, setValue).get();
以上代码展示了如何写入节点的值。在写入之前,我们需要指定要写入的节点并构造要写入的DataValue。在写入完成后,我们可以通过getValue()
方法获取写入的值,并检查StatusCode以确保写入成功。
示例1:
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.eclipse.milo.opcua.sdk.client.api.session.ClientSession;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription;
import org.eclipse.milo.opcua.sdk.client.subscriptions.OpcUaSubscriptionManager;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint;
public class OPCUAExample {
public static void main(String[] args) throws Exception {
// OPCUA服务地址
String endpointUrl = "opc.tcp://localhost:53530/OPCUA/SimulationServer";
// 创建OPCUA客户端
EndpointDescription endpoint = EndpointDescription.parse(endpointUrl);
OpcUaClientConfig config = OpcUaClientConfig.builder()
.setApplicationName(LocalizedText.english("OPCUA-Client"))
.setApplicationUri("urn:localhost:OPCUA:Client")
.setEndpoint(endpoint)
.setRequestTimeout(uint(5000))
.build();
OpcUaClient client = OpcUaClient.create(config);
try {
// 连接OPCUA服务
client.connect().get();
// 读取节点值
readValues(client);
// 监听节点数据变化
monitorValues(client);
// 写入节点值
writeValue(client);
} finally {
client.disconnect().get();
}
}
private static void readValues(OpcUaClient client) throws ExecutionException, InterruptedException {
// 读取节点值
List<NodeId> nodeIds = new ArrayList<>();
nodeIds.add(new NodeId(2, "/Static/AllProfiles/DeviceSetPoint"));
nodeIds.add(new NodeId(2, "/Dynamic/CoolingWaterInletTemperature"));
Map<NodeId, DataValue> values = client.readValues(0, TimestampsToReturn.Both, nodeIds).get();
for(Map.Entry<NodeId, DataValue> entry : values.entrySet()) {
NodeId nodeId = entry.getKey();
DataValue dataValue = entry.getValue();
System.out.println(nodeId + " value is " + dataValue.getValue().getValue());
}
}
private static void monitorValues(OpcUaClient client) throws ExecutionException, InterruptedException {
// 订阅节点值变化
UaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
NodeId nodeId = new NodeId(2, "/Static/AllProfiles/DeviceSetPoint");
UaMonitoredItem monitoredItem = subscription.createMonitoredItem(
nodeId,
OpcUaSubscriptionManager.DEFAULT_ITEM_SETTINGS,
(item, value) -> System.out.println(item.getReadValueId().getNodeId() + " value has changed: " + value.getValue())
).get();
// 等待值变化
Thread.sleep(10000);
monitoredItem.delete();
subscription.delete();
}
private static void writeValue(OpcUaClient client) throws ExecutionException, InterruptedException {
// 写入节点值
NodeId nodeId = new NodeId(2, "/Dynamic/CoolingWaterInletTemperature");
DataValue setValue = new DataValue(new Variant(Double.valueOf("50.5")));
StatusCode statusCode = client.writeValue(nodeId, setValue).get();
// 检查写入是否成功
if (statusCode.isGood()) {
System.out.println("Value has been written successfully");
} else {
System.out.println("Writing failed");
}
}
}
在此示例中,我们连接到了OPCUA服务并展示了如何读取、监视和写入节点的值。
示例2:
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint;
public class OPCUAExample {
public static void main(String[] args) throws Exception {
// OPCUA服务地址
String endpointUrl = "opc.tcp://localhost:53530/OPCUA/SimulationServer";
// 创建OPCUA客户端
EndpointDescription endpoint = EndpointDescription.parse(endpointUrl);
OpcUaClientConfig config = OpcUaClientConfig.builder()
.setApplicationName(LocalizedText.english("OPCUA-Client"))
.setApplicationUri("urn:localhost:OPCUA:Client")
.setEndpoint(endpoint)
.setRequestTimeout(uint(5000))
.build();
OpcUaClient client = OpcUaClient.create(config);
try {
// 连接OPCUA服务
client.connect().get();
// 读取节点值
readValues(client);
// 写入节点值
writeValue(client);
} finally {
client.disconnect().get();
}
}
private static void readValues(OpcUaClient client) throws ExecutionException, InterruptedException {
// 读取节点值
List<NodeId> nodeIds = new ArrayList<>();
nodeIds.add(new NodeId(2, "/Static/AllProfiles/DeviceSetPoint"));
nodeIds.add(new NodeId(2, "/Dynamic/CoolingWaterInletTemperature"));
Map<NodeId, DataValue> values = client.readValues(0, TimestampsToReturn.Both, nodeIds).get();
for(Map.Entry<NodeId, DataValue> entry : values.entrySet()) {
NodeId nodeId = entry.getKey();
DataValue dataValue = entry.getValue();
System.out.println(nodeId + " value is " + dataValue.getValue().getValue());
}
}
private static void writeValue(OpcUaClient client) throws ExecutionException, InterruptedException {
// 写入节点值
NodeId nodeId = new NodeId(2, "/Dynamic/CoolingWaterInletTemperature");
DataValue setValue = new DataValue(new Variant(Double.valueOf("50.5")));
StatusCode statusCode = client.writeValue(nodeId, setValue).get();
// 检查写入是否成功
if (statusCode.isGood()) {
System.out.println("Value has been written successfully");
} else {
System.out.println("Writing failed");
}
}
}
在此示例中,我们连接到了OPCUA服务并展示了如何读取和写入节点的值。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用java的milo框架访问OPCUA服务的过程 - Python技术站