新功能在node-red的功能块中不起作用 更新1:更新2 更新3

我正在尝试在功能块中使用nodes7,因为s7不足以满足我的目的(nodes7是s7使用的基础库)。

node7中给出的示例可以很好地用作独立的节点应用程序。代码如下。

var nodes7 = require('nodes7');  // This is the package name,if the repository is cloned you may need to require 'nodeS7' with uppercase S
var conn = new nodes7;
var doneReading = false;
var doneWriting = false;

var variables = { TEST1: 'MR4',// Memory real at MD4
          TEST2: 'M32.2',// Bit at M32.2
          TEST3: 'M20.0',// Bit at M20.0
          TEST4: 'DB1,REAL0.20',// Array of 20 values in DB1
          TEST5: 'DB1,REAL4',// Single real value
          TEST6: 'DB1,REAL8',// Another single real value
          TEST7: 'DB1,INT12.2',// Two integer value array
          TEST8: 'DB1,LREAL4'       // Single 8-byte real value
};

conn.initiateConnection({port: 102,host: '192.168.0.2',rack: 0,slot: 1},connected); // slot 2 for 300/400,slot 1 for 1200/1500
//conn.initiateConnection({port: 102,localTSAP: 0x0100,remoteTSAP: 0x0200,timeout: 8000},connected); // local and remote TSAP can also be directly specified instead.  The timeout option specifies the TCP timeout.

function connected(err) {
    if (typeof(err) !== "undefined") {
        // We have an error.  Maybe the PLC is not reachable.
        console.log(err);
        process.exit();
    }
    conn.setTranslationCB(function(tag) {return variables[tag];});  // This sets the "translation" to allow us to work with object names
    conn.addItems(['TEST1','TEST4']);
    conn.addItems('TEST6');
//  conn.removeItems(['TEST2','TEST3']);  // We could do this.
//  conn.writeItems(['TEST5','TEST6'],[ 867.5309,9 ],valuesWritten);  // You can write an array of items as well.
    conn.writeItems('TEST7',[ 666,777 ],valuesWritten);  // You can write a single array item too.
    conn.readAllItems(valuesReady);
}

function valuesReady(anythingBad,values) {
    if (anythingBad) { console.log("SOMETHING WENT WRONG READING VALUES!!!!"); }
    console.log(values);
    doneReading = true;
    if (doneWriting) { process.exit(); }
}

function valuesWritten(anythingBad) {
    if (anythingBad) { console.log("SOMETHING WENT WRONG WRITING VALUES!!!!"); }
    console.log("Done writing.");
    doneWriting = true;
    if (doneReading) { process.exit(); }
}

但是,我无法在功能块中使用它。我已经按照node-red社区的建议包含了以下node7,同时需要其他软件包

    functionGlobalContext: {
        nodes7:require('nodes7')
        // os:require('os'),// jfive:require("johnny-five"),// j5board:require("johnny-five").Board({repl:false})
    },

但是,当我按以下示例所示构造对象时,代码中断。

var conn = new nodes7;

我也曾尝试var conn = new nodes7();,但徒劳无功。我收到以下错误。

"TypeError: nodes7 is not a constructor"

因此,正确引用了node7变量,但是node-red抱怨它不是构造函数。我该如何继续。下面是我的整个功能块代码抛出以上错误。

var nodes7 = global.get('nodes7');
var conn = new nodes7();

conn.initiateConnection({port: 102,host: '127.0.0.1',slot 1 for 1200/1500
node.log('This worked');

function connected(err) 
{
    if (typeof(err) !== "undefined") 
    {
        // We have an error.  Maybe the PLC is not reachable.
        node.log('Connection not successful');
    }

}

return msg;

如果有帮助,我只是在观察之前使用了注入节点,在之后使用了调试节点。我是Node-red的新手。请帮忙。

新功能在node-red的功能块中不起作用
      
    更新1:更新2 更新3

更新1:

根据一个用户的请求,我在调用构造函数之前插入了node.warn(nodes7),发现它是未定义的。但为什么?我正在使用global.get,直接安装在正确的位置(我可以在node-red的node_modules文件夹中看到)仍然会发生这种情况。

新功能在node-red的功能块中不起作用
      
    更新1:更新2 更新3

更新2

我更新了usr目录中的settings.js文件,但仍然得到与未定义相同的响应。请在下面的图片中找到。

找到settings.js文件。

新功能在node-red的功能块中不起作用
      
    更新1:更新2 更新3

导航到该文件...

新功能在node-red的功能块中不起作用
      
    更新1:更新2 更新3

通过在此打开vs代码来更新该文件。

新功能在node-red的功能块中不起作用
      
    更新1:更新2 更新3

先安装好nodes7,然后重新启动node-red,我仍然遇到同样的错误。下面是代码。

新功能在node-red的功能块中不起作用
      
    更新1:更新2 更新3

更新3

我的更新2在全部关闭并重新启动后才能工作。

EDAshiyan951 回答:新功能在node-red的功能块中不起作用 更新1:更新2 更新3

settings.js中的functionGlobalContext引用的NPM模块需要安装在userDir中,因为它们是相对于活动settings.js文件加载的。

Node-RED启动时,正在使用的userDIRsettings.js文件都记录在输出的前几行中。

您只应在settings.js中编辑userDir文件。仅在设置新的userDir时才使用Node-RED安装目录中的版本。编辑此文件时,您还需要重新启动Node-RED。

本文链接:https://www.f2er.com/2623908.html

大家都在问