Node.js 使用节点opcua在Kepserver中创建变量

Node.js 使用节点opcua在Kepserver中创建变量,node.js,node-opcua,kepserverex,Node.js,Node Opcua,Kepserverex,我有一台西门子1200 PLC。使用node opcuaclient和Kepserver,我能够读取变量并更改值。 现在,我想从KepServer中的节点opcua在PLC中创建一个新变量。 我曾尝试使用node opcua server,因为在示例中我看到了如何创建变量,但我遇到了一个错误,因为我试图连接到KepServer的同一端口 var server = new opcua.OPCUAServer({ port: 49320, // the port of the listen

我有一台西门子1200 PLC。使用
node opcua
client和Kepserver,我能够读取变量并更改值。 现在,我想从KepServer中的节点opcua在PLC中创建一个新变量。

我曾尝试使用node opcua server,因为在示例中我看到了如何创建变量,但我遇到了一个错误,因为我试图连接到KepServer的同一端口

var server = new opcua.OPCUAServer({
    port: 49320, // the port of the listening socket of the server
    resourcePath: "", // this path will be added to the endpoint resource name
     buildInfo : {
        productName: "MySampleServer1",
        buildNumber: "7658",
        buildDate: new Date(2014,5,2)
    }
});

如何创建新变量?以及从节点opcua创建组标记

是否可以在Kepserver中使用opcua服务器并创建直接连接到该服务器的变量? 我的Kepserver在:opc中。tcp://localhost:49320 要连接到此Kepserver,我使用nodeopcua客户端:

var opcua = require("node-opcua");
var client = new opcua.OPCUAClient();
var endpointUrl = "opc.tcp://127.0.0.1:49320";
var the_session = null;
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) {
                the_session = session;
            }
            callback(err);
        });

    },
    // step 3 : browse
    function(callback) {

        the_session.browse("RootFolder", function(err,browse_result,diagnostics){
            if(!err) {
                browse_result[0].references.forEach(function(reference) {
                    console.log( reference.browseName);
                });
            }
            callback(err);
        });
    },
    // step 4 : read a variable
    function(callback) {
        the_session.readVariableValue("ns=2;s=S7.1200.nombre", function(err,dataValue) {
            if (!err) {
                console.log(" temperature = " , dataValue.toString());
            }
            callback(err);
        })
    },

    // step 5: install a subscription and monitored item
    //
    // -----------------------------------------
    // create subscription
    function(callback) {

        the_subscription=new opcua.ClientSubscription(the_session,{
            requestedPublishingInterval: 1000,
            requestedLifetimeCount: 10,
            requestedMaxKeepAliveCount: 200,
            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();
        },100000);


        // install monitored item
        //
        var monitoredItem  = the_subscription.monitor({
            nodeId: opcua.resolveNodeId("ns=2;s=S7.1200.nombre"),
            attributeId: 13
          //, dataEncoding: { namespaceIndex: 0, name:null }
        },
        { 
            samplingInterval: 100,
            discardOldest: true,
            queueSize: 10 
        });
        console.log("-------------------------------------");

        // subscription.on("item_added",function(monitoredItem){
        //xx monitoredItem.on("initialized",function(){ });
        //xx monitoredItem.on("terminated",function(value){ });


        monitoredItem.on("changed",function(value){
           console.log(" New Value = ",value.toString());
        });

    },

    // ------------------------------------------------
    // closing session
    //
    function(callback) {
        console.log(" closing session");
        the_session.close(function(err){

            console.log(" session closed");
            callback();
        });
    },


],
    function(err) {
        if (err) {
            console.log(" failure ",err);
        } else {
            console.log("done!")
        }
        client.disconnect(function(){});
});
我想从Kepserver中的代码创建新变量。我已经看到,使用nodeopcua服务器代码可以创建变量:

我想在KepServer中使用以下内容:

server.engine.addressSpace.addVariable

我能做些什么来解决我的问题?

您不能从客户端在
KEPServerEx
中创建变量

但你甚至不必创建它们。您可以使用KEPServerEx的功能将变量直接隧道到PLC中。这意味着,若您尝试读取服务器变量列表中未定义的变量,KEPServerEx将尝试在PLC中查找它们。因此,您不必在KEPServerEx中创建甚至维护变量列表。只需使用正确的变量地址由客户端读取即可:

session.readVariableValue("ns=2;s=Channel1.Device1.MB0", function(err,dataValue) {
   if (!err) {
      console.log("value=", dataValue.toString());
   }
}

你想要什么?变量环境?我想以动态方式在PLC内存中创建一个标记,我的客户端甚至没有实现
client.connect()
。知道为什么吗?为了能够在kepserverex中创建变量和其他相关任务,您需要使用其内部api。使用邮递员或任何其他工具快速测试它。