我在github中找到了一个更好的项目node-opcua,使用这个项目的简单示例可以创建一个opc服务器和客户端,并且在本地可以访问,我现在需要访问一个远程opc服务器,提供的一些参数是:IP网络节点(例如10.195.156.150),opc服务(例如ArchestrA。FSGateway),opc组(例如ArchestrA),opc TagName(例如HXi1_P201_00。Ia),是否可以使用节点opcua访问它?
谢谢!
sample_server代码:
var opcua = require("node-opcua");
var os = require("os");
// Let's create an instance of OPCUAServer
var server = new opcua.OPCUAServer({port: 4334});
var siteName = 'site01';
var var1 = {nodeId: 'ns=4;s=hook_weight', browseName: 'hook_weight', dataType: 'Double'};
var var2 = {nodeId: 'ns=4;s=rpm', browseName: 'rpm', dataType: 'Double'};
var var3 = {nodeId: 'ns=1;s=free_memory', browseName: 'free_memory', dataType: 'Double'};
function post_initialize(){
console.log("server initialized");
function construct_my_address_space(server) {
server.engine.createFolder("RootFolder",{ browseName: siteName});
// emulate variable1 changing every 500 ms
var variable1 = 1;
setInterval(function(){ variable1 += 1; }, 1000);
var1.value = {
get: function(){
return new opcua.Variant({dataType: opcua.DataType.Double, value: variable1 });
}
};
server.var1 = server.engine.addVariableInFolder(siteName, var1);
var2.value = {
get: function(){
return new opcua.Variant({dataType: opcua.DataType.Double, value: 10});
}
};
server.var2 = server.engine.addVariableInFolder(siteName, var2);
/**
/**
* returns the percentage of free memory on the running machine
* @return {double}
*/
function available_memory() {
// var value = process.memoryUsage().heapUsed / 1000000;
var percentageMemUsed = os.freemem() / os.totalmem() * 100.0;
return percentageMemUsed;
}
var3.value = {
get: function(){
return new opcua.Variant({dataType: opcua.DataType.Double, value: available_memory()});
}
};
server.var3 = server.engine.addVariableInFolder(siteName, var3);
}
construct_my_address_space(server);
server.start(function() {
console.log("Server is now listening ... ( press CTRL+C to stop)");
console.log("port ", server.endpoints[0].port);
var endpointUrl = server.endpoints[0].endpointDescription().endpointUrl;
console.log(" the primary server endpoint url is ", endpointUrl );
});
}
server.initialize(post_initialize);
sample_client代码:
var opcua = require("node-opcua");
var async = require("async");
var client = new opcua.OPCUAClient();
var endpointUrl = "opc.tcp://" + require("os").hostname().toLowerCase() + ":4334/UA/SampleServer";
var session, 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("connected !"); }
callback(err);
});
},
// step 2 : createSession
function(callback) {
client.createSession( function(err, _session) {
if(!err) { session = _session; }
callback(err);
});
},
// step 5: install a subscription and install a monitored item for 10 seconds
function(callback) {
subscription=new opcua.ClientSubscription(session,{
requestedPublishingInterval: 1000,
requestedLifetimeCount: 10,
requestedMaxKeepAliveCount: 2,
maxNotificationsPerPublish: 10,
publishingEnabled: true,
priority: 10
});
subscription.on("started",function(){
console.log("subscriptionId=", subscription.subscriptionId);
}).on("keepalive",function(){ console.log(); })
.on("terminated",function(){ callback(); });
setTimeout(function () {
subscription.terminate();
}, 10000);
// install monitored item
var monitoredItem = subscription.monitor({
nodeId: opcua.resolveNodeId("ns=1;s=free_memory"),
attributeId: opcua.AttributeIds.Value
},
{
samplingInterval: 100,
discardOldest: true,
queueSize: 100
},
opcua.read_service.TimestampsToReturn.Both
);
console.log("-------------------------------------");
monitoredItem.on("changed", function (dataValue) {
console.log(" % free mem = ", dataValue.value.value);
});
},
// close session
function (callback) {
// _"closing session"
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(){});
});
最佳答案
恐怕Archestra没有OPC UA服务器,因此要从node-opcua访问它,您将需要使用OPC UA网关,例如https://www.prosysopc.com/products/opc-ua-gateway/,它可以将OPC DA转换为OPC UA。
关于javascript - nodejs-如何使用node-opcua访问远程opc服务器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45603130/