gpt4 book ai didi

javascript - node-opcua如何写入变量

转载 作者:行者123 更新时间:2023-11-30 21:10:15 26 4
gpt4 key购买 nike

我确定这是一个非常基本的问题。我已经根据 http://node-opcua.github.io/ 上的示例创建了客户端和服务器.

该示例演示了读取变量但未演示如何写入。

感谢帮助

最佳答案

我为那些有相同问题的人解决了这个问题,请参见下面的代码。写函数在第4步

/*global require,console,setTimeout */
var opcua = require("node-opcua");
var async = require("async");

var client = new opcua.OPCUAClient();
var endpointUrl = "opc.tcp://" + require("os").hostname() + ":4334/UA/MyLittleServer";


var the_session, the_subscription;
async.series([

// step 1 : connect to
function(callback) {
client.connect(endpointUrl,function (err) {
if(err) {
console.log(" cannot connect to endpoint :" , endpointUrl );
} else {
console.log("Step 1 connected !");
}
callback(err);
});
},

// step 2 : createSession
function(callback) {
client.createSession( function(err,session) {
if(!err) {
the_session = session;
}
callback(err);
});
},

// step 3 : browse
function(callback) {
the_session.browse("RootFolder", function(err,browse_result){
if(!err) {
browse_result[0].references.forEach(function(reference) {
console.log("Step 3" + reference.browseName.toString());
});
}
callback(err);
});
},

// step 4 : read a variable with readVariableValue
function(callback) {
the_session.readVariableValue("ns=1;s=myvariable1", function(err,dataValue) {
if (!err) {
console.log("Step4 free mem % = " , dataValue.toString());
}
callback(err);

});


},

// step 4' : read a variable with read
function(callback) {
var max_age = 0;
var nodes_to_read = [
{ nodeId: "ns=1;s=myvariable2", attributeId: opcua.AttributeIds.Value }
];
the_session.read(nodes_to_read, max_age, function(err,nodes_to_read,dataValues) {
if (!err) {
console.log("Step 4 again free mem % = " , dataValues[0].value.value);
}
//callback(err);

});

var nodesToWrite = [{
nodeId: "ns=1;s=myvariable2",
attributeId: opcua.AttributeIds.Value,
indexRange: null,
value: {
value: {
dataType: opcua.DataType.Double,
value: 34
}
}
}];
the_session.write(nodesToWrite, function(err,statusCode,diagnosticInfo) {
if (!err) {
console.log(" write ok" );
console.log(diagnosticInfo);
console.log(statusCode);
}
callback(err);
});


},

// step 5: install a subscription and install a monitored item for 10 seconds
function(callback) {

the_subscription=new opcua.ClientSubscription(the_session,{
requestedPublishingInterval: 100,
requestedLifetimeCount: 10,
requestedMaxKeepAliveCount: 2,
maxNotificationsPerPublish: 10,
publishingEnabled: true,
priority: 10
});

the_subscription.on("started",function(){
console.log("subscription started for 2 seconds - subscriptionId=",the_subscription.subscriptionId);
}).on("keepalive",function(){
console.log("keepalive");
}).on("terminated",function(){
callback();
});
/*
setTimeout(function(){
the_subscription.terminate();
},10000);
*/
// install monitored item
var monitoredItem = the_subscription.monitor({
nodeId: opcua.resolveNodeId("ns=1;s=myvariable1"),
attributeId: opcua.AttributeIds.Value
},
{
samplingInterval: 100,
discardOldest: true,
queueSize: 10
},
opcua.read_service.TimestampsToReturn.Both
);
console.log("-------------------------------------");

monitoredItem.on("changed",function(dataValue){
console.log("variable 1 = ",dataValue.value.value);
});
},

// close session
function(callback) {
the_session.close(function(err){
if(err) {
console.log("session closed failed ?");
}
callback();
});
}

],
function(err) {
if (err) {
console.log(" failure ",err);
} else {
console.log("done!");
}
client.disconnect(function(){});
}) ;

关于javascript - node-opcua如何写入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46223471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com