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.
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment