2

I am trying to build an opcua client using the node-opcua library and I am testing it by trying to get it to connect to the Prosys OPC UA Simulation Server. I used the basic client example to attempt a connection but it doesn't seem to work and I can't make sense of the error message.

The code:-

import {
    OPCUAClient,
    MessageSecurityMode,
    SecurityPolicy,
    AttributeIds,
    makeBrowsePath,
    ClientSubscription,
    TimestampsToReturn,
    MonitoringParametersOptions,
    ReadValueIdOptions,
    ClientMonitoredItem,
    DataValue,
} from "node-opcua";

const connectionStrategy = {
    initialDelay: 1000,
    maxRetry: 1
};

const client = OPCUAClient.create({
    applicationName: "MyClient",
    connectionStrategy: connectionStrategy,
    securityMode: MessageSecurityMode.None,
    securityPolicy: SecurityPolicy.None,
    endpointMustExist: false
});

const endpointUrl = "opc.tcp://localhost:53530/OPCUA/SimulationServer"

async function main() {
    try {
        console.log("attempting to connect...");

        // step 1 : connect to
        await client.connect(endpointUrl);
        console.log("connected !");

        // // step 2 : createSession
        // const session = await client.createSession();
        // console.log("session created !");

        // step 3 : browse

        // step 4 : read a variable with readVariableValue

        // step 4' : read a variable with read

        // step 5: install a subscription and install a monitored item for 10 seconds

        // step 6: finding the nodeId of a node by Browse name

        // close session

        // disconnecting
    } catch (err) {
        console.log("An error has occurred : ", err);
    }
}
main();

The error message:-

attempting to connect...
An error has occurred :  AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

  (0, assert_1.default)(s.length <= maxLength)

    at parseBitString (C:\Users\binahmad\Documents\SWE\code\1 - Projects\20230717_opcua_Interface_testing_tool\sample-node-opcua-client\node_modules\node-opcua-crypto\source\asn1.ts:91:15)
    at _readBitString (C:\Users\binahmad\Documents\SWE\code\1 - Projects\20230717_opcua_Interface_testing_tool\sample-node-opcua-client\node_modules\node-opcua-crypto\source\asn1.ts:113:16)
    at _readSubjectPublicKeyInfo (C:\Users\binahmad\Documents\SWE\code\1 - Projects\20230717_opcua_Interface_testing_tool\sample-node-opcua-client\node_modules\node-opcua-crypto\source\crypto_explore_certificate.ts:552:44)
    at readTbsCertificate (C:\Users\binahmad\Documents\SWE\code\1 - Projects\20230717_opcua_Interface_testing_tool\sample-node-opcua-client\node_modules\node-opcua-crypto\source\crypto_explore_certificate.ts:665:40)
    at exploreCertificate (C:\Users\binahmad\Documents\SWE\code\1 - Projects\20230717_opcua_Interface_testing_tool\sample-node-opcua-client\node_modules\node-opcua-crypto\source\crypto_explore_certificate.ts:714:29)
    at publicKeyAndPrivateKeyMatches (C:\Users\binahmad\Documents\SWE\code\1 - Projects\20230717_opcua_Interface_testing_tool\sample-node-opcua-client\node_modules\node-opcua-crypto\source\public_private_match.ts:30:33)
    at C:\Users\binahmad\Documents\SWE\code\1 - Projects\20230717_opcua_Interface_testing_tool\sample-node-opcua-client\node_modules\node-opcua-client\source\verify.ts:131:39
    at Generator.next (<anonymous>)
    at C:\Users\binahmad\Documents\SWE\code\1 - Projects\20230717_opcua_Interface_testing_tool\sample-node-opcua-client\node_modules\node-opcua-client\dist\verify.js:8:71 
    at new Promise (<anonymous>) {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '=='
}

I feel like it should be working but something might be wrong with my system or my node instead of the code.

I tried connecting to the server with UaExpert and that worked, I also tried connecting with a python script and it worked. Its just node that seems affected.

Node version: v16.15.0

node-opcua version: ^2.108.0

If anyone could help me figure out how to simply connect, that would be very helpful

Aman
  • 86
  • 1
  • 6

2 Answers2

0

I am seeing the same error from the node-opcua package when trying to create a simulated server. This fails when running in a container using node version 14.16.1 but works if I run it on my system that has node version 16.15.1. You might try a newer version of node or an older version of node-opcua.

I have reverted back to using node-opcua version 2.50.0 and both initializing the server and connecting to it are working.

Nick
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 17 '23 at 04:40
  • Seems like it was a bug in the node-opcua library itself, I just posted a solution to the question. It might even work for you if you ever decide to update :) – Aman Aug 18 '23 at 09:09
0

After a week of investigating and posting on some other forums, one of the developers of node-opcua answered saying they've been refactoring and caught some bugs. It seems I've just been unlucky and was caught in the middle of this bug fixing. Basically in the end it was a bug in the node-opcua library itself. This was fixed in node-opcua version 2.110.0 as seen here.

In summary, I needed to do a few things to properly "clean" my system to get a fresh start:

  1. Updated Node from v16.15.0 to v18.17.1
  2. deleted folder C:\Users\username\AppData\Roaming\node-opcua-default-nodejs
  3. Changed node-opcua and node-opcua-client to both use version 2.110.0
  4. Then, of course, ran npm install to get the new packages

I could then connect to the Prosys Server and read a value with no issues with the following (simplified) script:

const { OPCUAClient, AttributeIds } = require("node-opcua");
(async ()=>{
    const client  = OPCUAClient.create({ endpointMustExist: false });
    client.on("backoff",(retry,delay)=>{
        console.log("backoff",retry,delay);
    });
    const endpointUrl = "opc.tcp://<system name>:53530/OPCUA/SimulationServer";
    await client.withSessionAsync(endpointUrl, async (session)=>{
        const dataValue = await session.read({ nodeId: "ns=3;i=1003", attributeId: AttributeIds.Value});
        console.log("Local server time is ", dataValue.value.toString());
    });
})();
Aman
  • 86
  • 1
  • 6