Skip to content

Instantly share code, notes, and snippets.

@simonbowen

simonbowen/node_opcua.js Secret

Created Apr 8, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@erossignon 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