Guest User

Untitled

a guest
Apr 2nd, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. const opcua = require("node-opcua");
  2.  
  3. const userName = "user1"
  4.  
  5. const password = "password1"
  6. //xx const endpointUrl = "opc.tcp://192.168.249.31:4840";
  7. const endpointUrl = "opc.tcp://localhost:26543";
  8. //xx const nodeId = "ns=4;s=opctest1_Copy1";
  9. const nodeId = "ns=411;s=Scalar_Static_UInt16";
  10.  
  11. async function CreateConnection(){
  12.  
  13. console.log('Trying to connect ...');
  14. const client = new opcua.OPCUAClient({endpoint_must_exist: false });
  15.  
  16. await client.connect(endpointUrl);
  17. console.log('Connected!');
  18.  
  19. const session = await client.createSession({userName: userName, password: password});
  20.  
  21. let dataValue = await session.read({nodeId: nodeId })
  22. console.log(dataValue.toString());
  23.  
  24. const nodeToWrite = {
  25. nodeId: nodeId,
  26. attributeId: opcua.AttributeIds.Value,
  27. value: /* DataValue */{
  28. statusCode: opcua.StatusCodes.Good,
  29. value: /* Variant */{
  30. dataType: opcua.DataType.UInt16,
  31. value: 1098
  32. }
  33. },
  34. }
  35. let risultato = await session.write(nodeToWrite);
  36. console.log(risultato.toString());
  37.  
  38. dataValue = await session.read({nodeId: nodeId })
  39. console.log(dataValue.toString());
  40.  
  41. await client.closeSession(session,true);
  42. await client.disconnect();
  43. };
  44.  
  45. CreateConnection();
Add Comment
Please, Sign In to add comment