自己本地用java写了一个客户端,然后创建opcua客户端的方法如下
public static OpcUaClient createClientNewEndpoint(String endPointUrl, String username, String password) {
System.out.println(endPointUrl);
IdentityProvider identityProvider = new AnonymousProvider();
if (!StringUtils.isEmpty(username) || !StringUtils.isEmpty(password)) {
identityProvider = new UsernameProvider(username, password);
}
try {
Function<List<EndpointDescription>, Optional<EndpointDescription>> selectEndpoint = endpoints -> {
final Optional<EndpointDescription> endpoint = endpoints
.stream()
.filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri()))
.findFirst();
EndpointDescription newEndpoint = new EndpointDescription(endPointUrl, endpoint.get().getServer(), endpoint.get().getServerCertificate(),
endpoint.get().getSecurityMode(), endpoint.get().getSecurityPolicyUri(), endpoint.get().getUserIdentityTokens(),
endpoint.get().getTransportProfileUri(), endpoint.get().getSecurityLevel());
return Optional.of(newEndpoint);
};
IdentityProvider finalIdentityProvider = identityProvider;
OpcUaClient opcClient = OpcUaClient.create(endPointUrl,
selectEndpoint,
configBuilder -> configBuilder
.setApplicationName(LocalizedText.english("plc"))
.setApplicationUri("urn:eclipse:milo:examples:client")
//访问方式
.setIdentityProvider(finalIdentityProvider)
.setRequestTimeout(UInteger.valueOf(5000))
.build()
);
opcClient.connect().get();
System.out.println("连接成功:success");
return opcClient;
} catch (Exception e) {
e.printStackTrace();
System.out.println("======== opc connection fail ========");
}
return null;
}
其中服务端是在另一个电脑上,kepserver的配置如下

我传入的endPointUrl是opc.tcp://192.168.53.124:49320。两个电脑在一个局域网下,测试过可以ping通。然后kepserver这边应该也是正常的(刚入门不是太会使用)

但是在我启动主程序试图连接后,报了如下的错误:
UaException: status=Bad_Timeout, message=io.netty.channel.ConnectTimeoutException: connection timed out: /192.168.53.124:49320
我搜索其它文章,有博主说是因为原来是使用url去服务器端点列表查找符合条件的第一个端点,返回的地址使用的是内网地址,因此不能使用。需要重新构造EndpointDescription对象即可,以上代码是我根据该文章修改过后的:
但依旧会报上述的错误?请问我该怎么解决。我在kepserver中opcua的配置里的受信任的客户端似乎也没找到有新的需要我去信任。
以及,我之前使用的服务端是一个叫做Prosys OPC Simulation Server的软件,和我的程序在同一台电脑上,只修改了一个端点url就可以正常获取点位的值,不知道Kepserver为什么不行。