新的不在节点红色的功能块中工作
new not working in function block of node-red
我正在尝试使用 nodes7 in a function block, as 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, host: '192.168.0.2', 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 社区的建议包含了如下节点 7,同时需要额外的包
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"
因此正确引用了 nodes7 变量,但 node-red 抱怨它不是构造函数。我该如何继续。下面是我抛出上述错误的整个功能块代码。
var nodes7 = global.get('nodes7');
var conn = new nodes7();
conn.initiateConnection({port: 102, host: '127.0.0.1', rack: 0, slot: 1}, connected); // slot 2 for 300/400, 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;
如果有帮助,我只是在之前使用了一个注入节点,在之后使用了一个调试节点,只是为了观察。我是节点红色的新手。请帮忙。
更新 1:
应一位用户的要求,我在调用构造函数之前插入了 node.warn(nodes7)
并观察到它未定义。但为什么?我正在使用 global.get,直接安装在适当的地方(我可以在 node-red 的 node_modules 文件夹中看到)仍然发生这种情况。
更新 2
我更新了 usr 目录中的 settings.js 文件,但仍然得到与未定义相同的响应。请在下面找到图片。
正在查找 settings.js 文件..
导航到该文件...
正在通过打开 vs 代码更新该文件..
安装nodes7然后重启node-red,我仍然得到同样的错误..下面是代码..
更新 3
我的更新 2 在关闭所有并重新启动后工作..
从 functionGlobalContext
中的 settings.js
引用的 NPM 模块需要安装在 userDir
中,因为它们是相对于活动 settings.js
文件加载的。
Node-RED 启动时,正在使用的 userDIR
和 settings.js
文件都记录在输出的前几行中。
您应该只编辑 userDir
中的 settings.js
文件。 Node-RED 安装目录中的版本仅在设置新 userDir
时使用。编辑此文件时,您还需要重新启动 Node-RED。
我正在尝试使用 nodes7 in a function block, as 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, host: '192.168.0.2', 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 社区的建议包含了如下节点 7,同时需要额外的包
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"
因此正确引用了 nodes7 变量,但 node-red 抱怨它不是构造函数。我该如何继续。下面是我抛出上述错误的整个功能块代码。
var nodes7 = global.get('nodes7');
var conn = new nodes7();
conn.initiateConnection({port: 102, host: '127.0.0.1', rack: 0, slot: 1}, connected); // slot 2 for 300/400, 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;
如果有帮助,我只是在之前使用了一个注入节点,在之后使用了一个调试节点,只是为了观察。我是节点红色的新手。请帮忙。
更新 1:
应一位用户的要求,我在调用构造函数之前插入了 node.warn(nodes7)
并观察到它未定义。但为什么?我正在使用 global.get,直接安装在适当的地方(我可以在 node-red 的 node_modules 文件夹中看到)仍然发生这种情况。
更新 2
我更新了 usr 目录中的 settings.js 文件,但仍然得到与未定义相同的响应。请在下面找到图片。
正在查找 settings.js 文件..
导航到该文件...
正在通过打开 vs 代码更新该文件..
安装nodes7然后重启node-red,我仍然得到同样的错误..下面是代码..
更新 3
我的更新 2 在关闭所有并重新启动后工作..
从 functionGlobalContext
中的 settings.js
引用的 NPM 模块需要安装在 userDir
中,因为它们是相对于活动 settings.js
文件加载的。
Node-RED 启动时,正在使用的 userDIR
和 settings.js
文件都记录在输出的前几行中。
您应该只编辑 userDir
中的 settings.js
文件。 Node-RED 安装目录中的版本仅在设置新 userDir
时使用。编辑此文件时,您还需要重新启动 Node-RED。