1.导入依赖
<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>sdk-client</artifactId>
<version>0.6.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>dictionary-reader</artifactId>
<version>0.6.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.eclipse.milo</groupId>
<artifactId>sdk-server</artifactId>
<version>0.6.0-SNAPSHOT</version>
</dependency>
2.编写代码块
@Test
public void createOpcClient() throws Exception {
//opc ua服务端地址
String endPointUrl = "opc.tcp://127.0.0.1:49320";
Path securityTempDir = Paths.get(System.getProperty("java.io.tmpdir"),
"security");
Files.createDirectories(securityTempDir);
if (!Files.exists(securityTempDir)) {
throw new Exception("unable to create security dir: " + securityTempDir);
}
OpcUaClient opcUaClient = OpcUaClient.create(endPointUrl,
endpoints ->
endpoints.stream()
.filter(e ->
e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri()))
.findFirst(),
configBuilder ->
configBuilder
.setApplicationName(LocalizedText.english("eclipse milo
opc-ua client"))
.setApplicationUri("urn:eclipse:milo:examples:client")
//访问方式
.setIdentityProvider(new AnonymousProvider())
.setRequestTimeout(UInteger.valueOf(500))
.build()
);
opcUaClient.connect().get();
}
3. 测试输出
NodeId nodeId = new NodeId(2, "OPC_Test.设备 1.TAG1");
//读取节点数据
DataValue value = opcUaClient.readValue(0.0, TimestampsToReturn.Neither,
nodeId).get();
// 状态
System.out.println("Status: " + value.getStatusCode());
//标识符
String id = String.valueOf(nodeId.getIdentifier());
System.out.println(id + ": " + value.getValue().getValue());
连接时出现的依赖冲突:
此依赖在很多帖子中没有导入 了解到此依赖是谷歌的一个工具类 疑惑的是跟opcua 的连接又有什么联系 可能是本身导入的依赖出现了版本问题或者是冲突 不管咋说希望大家养成一个文件管理的好习惯
我在上面尝试了很多方式建立连接 始终没有获取到节点的值 网上很多帖子没有这个依赖
<!--谷歌的官方的第三方库-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>