Create variable in Kepserver with Node-OPCUA
Asked Answered
S

3

66

I have a Siemens 1200 PLC. Using node-opcua client and Kepserver I am able to read the variables and to change the values. Now I would like to create a new variable in the PLC from Node-OPCUA in KepServer. enter image description here

I've tried to use node-opcua server because in the examples I've seen how to create variables, but I get an error because I am trying to connect to the same port that KepServer does.

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)
    }
});

enter image description here

How could I deal to create a new variable? and to create a group tag from Node-OPCUA?

Is it possible to have a Opcua server in Kepserver and create variables connecting to that server directly?

My Kepserver is in:

opc.tcp://localhost:49320

To connect to this Kepserver I use the nodeopcua client:

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(){});
});

I would like to create new variables from code in my Kepserver. I've seen that with the nodeopcua server code there's a way of creating variables: Creating a Simple Server

I would like to use something such as in KepServer:

server.engine.addressSpace.addVariable

What could I do to solve my problem?

Supinator answered 5/9, 2016 at 11:18 Comment(4)
What do you want ? Variable environnement ?Vermont
I want to create a tag in the PLC memory in a dynamic waySupinator
My client is not even achieving client.connect(). Any idea why?Epigraph
To be able to create variables and other related tasks in kepserverex you need to use its internal api. Use Postman or any other to quickly test it. kepware.com/en-us/products/kepserverex/features/… kepware.com/getattachment/f38ad6fe-be2e-40cc-9481-11d9e85c980c/… youtube.com/watch?v=nnFQ3mU3ZLwRaffaello
B
1

You cannot create variables in KEPServerEx from node-opcua client.

But you do not even have to create them. You can use feature of KEPServerEx to tunneling variables right into PLC. That mean if u try to read variable that is not defines in server variable list, KEPServerEx will try to find them within PLC. So then you do not have to create or even maintain the variable list in KEPServerEx. Just read it by client with right variable address:

session.readVariableValue("ns=2;s=Channel1.Device1.MB0", function(err,dataValue) {
   if (!err) {
      console.log("value=", dataValue.toString());
   }
}
Bunni answered 21/3, 2017 at 9:24 Comment(0)
S
1

U cant create tag with opc client but u can try use to Kepserverex Configuration Api Service tool in kepserverex. It location; Administration -> Settings -> Configuration Api Service

enter image description here

U should enable http or https or both of them what u want. Then u can crud channel device and tags with https get,post and put requests.

In the url in the view browser section, the document of the api is also available

Example;

enter image description here

for example endpoint for api ;

http://127.0.0.1:57412/config/v1/project/channels/Channel1/devices/Device1/tags/basinc [GET]

Response

enter image description here

When you send the json result from the get request to the same endpoint as a post request, it will add the new tag. (You need to delete PROJECT_ID and change the tag name in common.ALLTYPES_NAME)

Scrawny answered 6/11, 2022 at 19:36 Comment(0)
E
-1
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)
    }
});
Ez answered 22/12, 2022 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.