JAVA OPC UA客户端无法连接到Kepware Server

时间:2018-03-01 11:32:09

标签: java opc-ua

我正在尝试将最小的OPC UA客户端连接到kepware服务器。 我已经尝试过OPC Foundations的堆栈和Eclipse Milo的SDK。

似乎可以解析服务器URL,但是如果我尝试从服务器读取任何内容,它会说会话无效。

以下是OPC UA堆栈客户端的代码:

公共类SampleClient {

    public static void main(String[] args) throws Exception {

        String url = "(serverurl)";
        System.out.print("SampleClient: Connecting to " + url + " .. ");

        final Client myClient = Client.createClientApplication(null);


        SessionChannel mySession = myClient.createSessionChannel(url);
        // mySession.activate("username", "123");
        mySession.activate();

        EndpointDescription[] endpoints = myClient.discoverEndpoints(url);
        EndpointDescription endpoint = endpoints[0];

        ServiceChannel myChannel = myClient.createServiceChannel(endpoint);
        // Create Test Request
        ReadValueId[] nodesToRead = {new ReadValueId(Identifiers.RootFolder,                                                                                                                                                        Attributes.BrowseName, null, null)};
        ReadRequest req = new ReadRequest(null, 100.0, TimestampsToReturn.Both, nodesToRead);
        System.out.println("REQUEST: " + req);

        ReadResponse res = myChannel.Read(req);
        System.out.println("RESPONSE: " + res);

        //close
        mySession.close();
        mySession.closeAsync();
    }

}

这是Eclipse Milo SDK的代码

    public class MinimalClient {

    private OpcUaClient createClient() throws Exception {

        EndpointDescription[] endpoints;

        endpoints = UaTcpStackClient
                .getEndpoints("(serverUrl)")
                .get();

        EndpointDescription endpoint = endpoints[0];

        OpcUaClientConfig config = OpcUaClientConfig.builder()
                .setApplicationName(LocalizedText.english("MinimalClient"))
                .setApplicationUri("urn:serverURN")
                .setCertificate(null)
                .setKeyPair(null)
                .setEndpoint(endpoint)
                .setMaxResponseMessageSize(uint(50000))
                .setIdentityProvider(new AnonymousProvider())
                .setRequestTimeout(uint(5000))
                .build();

        return new OpcUaClient(config);
    }

    public void readValue(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {

        // synchronous read request via VariableNode
        VariableNode node = client.getAddressSpace().createVariableNode(Identifiers.Server_ServerStatus_StartTime);
        DataValue value = node.readValue().get();

        System.out.println(value);
    }

    private CompletableFuture<List<DataValue>> readServerStateAndTime(OpcUaClient client) {
        List<NodeId> nodeIds = ImmutableList.of(
                Identifiers.Server_ServerStatus_State,
                Identifiers.Server_ServerStatus_CurrentTime);

        System.out.println(client.readValues(10000.0, TimestampsToReturn.Both, nodeIds));
        return client.readValues(10000.0, TimestampsToReturn.Both, nodeIds);
    }

    public static void main(String[] args) throws Exception {
        CompletableFuture<OpcUaClient> future = new CompletableFuture<>();
        MinimalClient c = new MinimalClient();
        OpcUaClient client = c.createClient();
        client.connect();
        client.getAddressSpace();
        c.readValue(client, future);
    }
}

OPC UA堆栈错误:

Exception in thread "main" ServiceFault: Bad_SessionIdInvalid (0x80250000) "The session id is not valid."

Eclipse Milo错误:

Exception in thread "main" java.util.concurrent.ExecutionException: UaException: status=Bad_SessionClosed, message=The session was closed by the client.

我不确定出了什么问题。我们的Kepserver不使用任何认证等.AncurityPolicy是None。

0 个答案:

没有答案
相关问题