JS菜鸟整理。
下载:
GitHub - node-opcua/node-opcua-sample: a simple OPCUA sample client to demonstrate how to use node-opcua SDKhttps://github.com/node-opcua/node-opcua-sample正常:
npm install
修改simple_client.js:
var opcua = require('node-opcua'); var nodeId = require("node-opcua-nodeid"); var async = require("async"); var DataType = opcua.DataType; var AttributeIds = opcua.AttributeIds; var connectionOption = {}; connectionOption.securityPolicy = opcua.SecurityPolicy.None; connectionOption.securityMode = opcua.MessageSecurityMode.None; connectionOption.keepSessionAlive = true; client = opcua.OPCUAClient.create(connectionOption); async function connect_opcua_client() { try{ await client.connect("opc.tcp://192.168.3.1:4840"); } catch (err) { console.log(err); return; } if (!client) { console.log("Client not yet created & connected, cannot getEndpoints!"); return; } // verbose_log("Create session ..."); try { if (!client) { return; } var userIdentity = {}; if (0) { userIdentity.userName = "XXXX"; userIdentity.password = "XXX."; userIdentity.type = opcua.UserTokenType.UserName; // New TypeScript API parameter } else { userIdentity.type = opcua.UserTokenType.Anonymous; } session = await client.createSession(userIdentity); if (!session) { console.log("Create session failed!"); return; } console.log("Create session sucees!"); } catch (err) { console.log(err); return; } //read value: var nodeIdS = "ns=1;s=XXXX"; try { const dataValue = await session.read({ nodeId:nodeIdS, attributeId: AttributeIds.Value }); if (dataValue.statusCode !== opcua.StatusCodes.Good) { console.log("Could not read ", nodeIdS); } else { console.log(" read value success = ", dataValue.toString()); } } catch(err) { console.log(err); return; } // var _toDataType = function (type) { // if (type === 'Boolean') { // return opcua.DataType.Boolean; // } else if (type === 'SByte') { // return opcua.DataType.SByte; // } else if (type === 'Byte') { // return opcua.DataType.Byte; // } else if (type === 'Int16') { // return opcua.DataType.Int16; // } else if (type === 'UInt16') { // return opcua.DataType.UInt16; // } else if (type === 'Int32') { // return opcua.DataType.Int32; // } else if (type === 'UInt32') { // return opcua.DataType.UInt32; // } else if (type === 'Int64') { // return opcua.DataType.Int64; // } else if (type === 'UInt64') { // return opcua.DataType.UInt64; // } else if (type === 'Float') { // return opcua.DataType.Float; // } else if (type === 'Double') { // return opcua.DataType.Double; // } else if (type === 'String') { // return opcua.DataType.String; // } else if (type === 'DateTime') { // return opcua.DataType.DateTime; // } else if (type === 'Guid') { // return opcua.DataType.Guid; // } else if (type === 'ByteString') { // return opcua.DataType.ByteString; // } // } //write value: try { var nodesToWrite = [ { nodeId: nodeIdS, attributeId: opcua.AttributeIds.Value, value: /*new DataValue(*/{ value: {/* Variant */ dataType: opcua.DataType.Double, value: 2.2 } } }]; session.write(nodesToWrite, function (err, statusCodes) { if (err) { console.log(' setValue error: ' + err); } }); } catch(err) { console.log(err); return; } console.log(' setValue susccess: !'); } connect_opcua_client();