我敢肯定,这是一个非常基本的问题。我已经根据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(){});
}) ;
您可以使用 session#write 方法
function (callback) {
var setPointTemperatureId = "ns=4;s=SetPointTemperature";
var nodesToWrite = [
{
nodeId: setPointTemperatureId,
attributeId: AttributeIds.Value,
value: /*new DataValue(*/{
value: {/* Variant */
dataType: DataType.Double,
value: 10.0
}
}
}
];
session.write(nodesToWrite, function (err, statusCodes) {
if (!err) {
}
callback(err);
});
}