Skip to content

Instantly share code, notes, and snippets.

@davidemess
Created March 23, 2018 13:11
Show Gist options
  • Save davidemess/eeb2d8eee624c448cc4ac76c9bbdb70a to your computer and use it in GitHub Desktop.
Save davidemess/eeb2d8eee624c448cc4ac76c9bbdb70a to your computer and use it in GitHub Desktop.

Revisions

  1. davidemess created this gist Mar 23, 2018.
    45 changes: 45 additions & 0 deletions testwrite.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    const endpointUrl = "opc.tcp://192.168.249.31:4840";
    const opcua = require("node-opcua");
    const StatusCodes = opcua.StatusCodes;
    const AttributeIds = opcua.AttributeIds;
    const DataTypes = opcua.DataType;

    let client;
    let session;

    async function CreateConnection(){

    const OPCUAClient = opcua.OPCUAClient;
    console.log('Trying to connect ...');
    client = new OPCUAClient({});

    await client.connect(endpointUrl);
    console.log('Connected!');

    session = await client.createSession({userName: "Davide", password: "davide"});
    let val = await session.read({nodeId: "ns=4;s=opctest1_Copy1"})
    console.log(val.value.value);

    let nodeToWrite = {
    nodeId: "ns=4;s=opctest1_Copy1",
    attributeIds: opcua.AttributeIds.Value,
    value: {
    value: {
    dataType: opcua.DataType.UInt16,
    value: 1098
    }
    },
    statusCode: opcua.StatusCodes.Good // <====
    }
    let risultato = await session.write(nodeToWrite);
    console.log(risultato);

    val = await session.read({nodeId: "ns=4;s=opctest1_Copy1"})
    console.log(val.value.value)

    await client.closeSession(session,true);
    await client.disconnect();
    };

    CreateConnection();