【发布时间】:2019-03-26 06:04:37
【问题描述】:
我正在使用 Node.js 构建一个可以读取 USB LoRa 模块数据并控制 Raspberry Pi GPIO 引脚的应用程序。
主要有三个功能:
设置 GPIO 开启(给定引脚 0 信号)
关闭 GPIO(给定引脚 1 信号)
设置GPIO在一段时间后关闭(使用
setTimeout函数)
主要功能运行良好。
问题是在setTimeout循环中,我想随时终止它。
我尝试使用clearTimeout 取消setTimeout 循环,但它似乎不起作用。我做错了吗?
例如。
在 LoRa 接收到的数据包中使用字符串“TurnOn15s”。 我无法随时终止超时。我必须等到 15 秒结束才能关闭 GPIO 引脚。如果我在超时执行期间调用 deviceTurnOff(1) 函数,我会在循环结束后得到两个输出。
程序输出如下:
// Output by `deviceTurnOn(0,15000)` from received LoRa packet.
Received:0, the solenoid valve has been opened!
Received packet: !20!96 0 0 0 53 0 0 28 11 3 TurnOn15s RSSI: -40 SNR: 6
// Output by `setTimeout` function.
Received:1, the solenoid valve has been closed!
// Output by deviceTurnOff(1) from received LoRa packet.
Received:1, the solenoid valve has been closed!
Received packet: !18!96 0 0 0 53 0 0 29 9 3 TurnOff RSSI: -41 SNR: 7
这里是部分代码:
var turnOffTimeout = null; // turnOffTimeout
// Control Raspberry Pi GPIO pins.
function deviceTurnOn(controlValueType, timeout) {
// Do Turn On Raspberry Pi GPIO pins.
if (timeout == -1) {
// if no time string in the packet.
console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
OutputDevice.writeSync(controlValueType);
} else {
// if contains time string in the packet.
console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
OutputDevice.writeSync(controlValueType);
turnOffTimer(timeout);
}
}
// Control Raspberry Pi GPIO pins.
function deviceTurnOff(controlValueType) {
// Do Turn Off Raspberry Pi GPIO pins.
console.log(`Received:${controlValueType}, the solenoid valve has been closed!`);
OutputDevice.writeSync(controlValueType);
// clearTimeout
if (turnOffTimeout) {
clearTimeout(turnOffTimeout);
}
}
// Raspberry Pi GPIO pins turn off timeout.
function turnOffTimer(timeout) {
turnOffTimeout = setTimeout(function() {
deviceTurnOff(1);
}, timeout);
}
包含三个功能:
deviceTurnOn、deviceTurnOff、turnOffTimer
函数调用的格式是
deviceTurnOff(1);
deviceTurnOn(0,timeout);
// Timeout is milliseconds
更新;
我尝试修改我的程序如下,得到的输出和之前版本的代码一样,问题还是没有解决。
var turnOffTimeout = null; // turnOffTimeout
// Control Raspberry Pi GPIO pins.
function deviceTurnOn(controlValueType, timeout) {
// Do Turn On Raspberry Pi GPIO pins.
if (timeout == -1) {
// if no time string in the packet.
console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
OutputDevice.writeSync(controlValueType);
}
else {
// if contains time string in the packet.
console.log(`Received:${controlValueType}, the solenoid valve has been opened!`);
OutputDevice.writeSync(controlValueType);
// clearTimeout.
clearTimeout(turnOffTimeout);
// set new timeout.
turnOffTimeout = setTimeout(function () {
deviceTurnOff(1);
}, timeout);
}
}
// Control Raspberry Pi GPIO pins.
function deviceTurnOff(controlValueType) {
// Do Turn Off Raspberry Pi GPIO pins.
console.log(`Received:${controlValueType}, the solenoid valve has been closed!`);
clearTimeout(turnOffTimeout);
OutputDevice.writeSync(controlValueType);
}
【问题讨论】:
-
“但它似乎不起作用”——你看到了什么行为?
-
在设置新的超时之前,您似乎没有清除超时。结果,您有一个不再被变量引用的超时,同时给您多个超时。将变量设置为新的时,请务必清除现有的超时。
-
@brian-lives-outdoors 例如,超时设置为 15(秒)。调用 deviceTurnOff(1)。执行循环时,我必须等待 15(秒)才能关闭 GPIO。 15(秒)后,同时得到两个输出,一个是setTimeout提供的,另一个是deviceTurnOff(1)调用的结果。
-
@MarkMeyer 谢谢提醒,我没注意这个,我试试。
标签: javascript node.js raspberry-pi settimeout cleartimeout