Skip to content

Instantly share code, notes, and snippets.

@simonbowen
Created April 8, 2021 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonbowen/033e600832a27664cdb8d6225ef5ef5b to your computer and use it in GitHub Desktop.
Save simonbowen/033e600832a27664cdb8d6225ef5ef5b to your computer and use it in GitHub Desktop.
import { OPCUAClient, AttributeIds } from "node-opcua";
const endpoint = "opc.tcp://192.168.100.64:4840";
const PITCH_NODE = "ns=6;s=::CuttingLab:DataExchange.Cfg.Cutting.Pitch";
(async () => {
try {
const client = OPCUAClient.create({
endpoint_must_exist: false,
connectionStrategy: {
maxRetry: 2,
initialDelay: 2000,
maxDelay: 10 * 1000
}
});
await client.connect(endpoint);
const session = await client.createSession({ userName: 'XXXXXX', password: 'XXXXXX' });
const pitch = await session.read({
nodeId: PITCH_NODE,
attributeId: AttributeIds.Value,
});
console.log(pitch);
} catch (err) {
console.log(err);
}
})();
@erossignon
Copy link

erossignon commented Apr 8, 2021

import { OPCUAClient, AttributeIds } from "node-opcua";

const endpoint = "opc.tcp://192.168.100.64:4840";
const PITCH_NODE = "ns=6;s=::CuttingLab:DataExchange.Cfg.Cutting.Pitch";

(async () => {

    try {
        const client = OPCUAClient.create({
            endpointMustExists: false, // Now in camelCase
            connectionStrategy: {
                maxRetry: 2,
                initialDelay: 2000,
                maxDelay: 10 * 1000
            }
        });
    
        await client.connect(endpoint);
    
        const session = await client.createSession({ userName: 'XXXXXX', password: 'XXXXXX' });

        const pitchDataValue = await session.read({
            nodeId: PITCH_NODE,
            attributeId: AttributeIds.Value,
        });

        console.log(pitchDataValue .toString());   // use toString() to get friendly output
      
        // do not forget to close the session and disconnect (to free up ressources)
        await session.close();
        await client.disconnect();

    } catch (err) {
        console.log(err);
    }
})();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment