Skip to content

Instantly share code, notes, and snippets.

@erossignon
Forked from davidemess/testwrite.js
Last active March 24, 2018 18:22
Show Gist options
  • Save erossignon/48ebf30d501b8d51472cac573e5160b2 to your computer and use it in GitHub Desktop.
Save erossignon/48ebf30d501b8d51472cac573e5160b2 to your computer and use it in GitHub Desktop.
const opcua = require("node-opcua");
const userName = "user1"
const password = "password1"
//xx const endpointUrl = "opc.tcp://192.168.249.31:4840";
const endpointUrl = "opc.tcp://localhost:26543";
//xx const nodeId = "ns=4;s=opctest1_Copy1";
const nodeId = "ns=411;s=Scalar_Static_UInt16";
async function CreateConnection(){
console.log('Trying to connect ...');
const client = new opcua.OPCUAClient({endpoint_must_exist: false });
await client.connect(endpointUrl);
console.log('Connected!');
const session = await client.createSession({userName: userName, password: password});
let dataValue = await session.read({nodeId: nodeId })
console.log(dataValue.toString());
const nodeToWrite = {
nodeId: nodeId,
attributeId: opcua.AttributeIds.Value,
value: /* DataValue */{
statusCode: opcua.StatusCodes.Good,
value: /* Variant */{
dataType: opcua.DataType.UInt16,
value: 1098
}
},
}
let risultato = await session.write(nodeToWrite);
console.log(risultato.toString());
dataValue = await session.read({nodeId: nodeId })
console.log(dataValue.toString());
await client.closeSession(session,true);
await client.disconnect();
};
CreateConnection();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment