我正在使用 node.js 执行外部文件(已编译的 cpp)。如果我自己执行文件,程序运行正常。我应该得到以下输出:
Got response 534, round-trip delay: 9569
我想用websokets取值并显示在网页上。不幸的是,当从 node.js 运行程序时,我得到以下输出:
stdout: stderr: exec error: null I don't understand what is happening and why if I run the application from command line I get the desired output?
谢谢,请原谅我的愚蠢问题!
更改执行文件的方式后,我得到以下输出:
stdout: stderr: exec error: 139
我所做的改变是:
exec('sudo /home/pi/gitwork/RF24/RPi/RF24/examples/light -m 1',
function (error, stdout, stderr) {
socket.emit("response");
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error.code);
}
服务器.js
function sendMessage(socket){
console.log("execute app");
exec.execFile('/home/pi/gitwork/RF24/RPi/RF24/examples/light',
['-m', 1],
function (error, stdout, stderr) {
socket.emit("response");
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error.code);
}
});
}
io.sockets.on('connection', function (socket) {
socket.on('get', function (data) {
console.log("get received");
sendMessage(socket);
});
});
灯光.cpp
bool switchLight(int action){
radio.startListening();
bool timeout = false;
while ( ! radio.available() ) {
sleep(10);
}
if (radio.available()){
unsigned long got_time=0;
radio.read( &got_time, sizeof(unsigned long) );
printf("Got response %lu, round-trip delay: %lu\n\r",got_time,millis()-got_time);
return true;
}else{
printf("Failed, response timed out.\n\r");
return false;
}
}
int main( int argc, char ** argv){
char choice;
setup();
bool switched = false;
int counter = 0;
while(( choice = getopt( argc, argv, "m:")) != -1){
if (choice == 'm'){
printf("\nOpening the gates...\n");
while(switched == false && counter < 5){
// switched = true;
switched = switchLight(atoi(optarg));
counter ++;
}
}else{
// A little help:
return 4;
printf("\n\rIt's time to make some choices...\n");
}
//return 0 if everything went good, 2 otherwise
if (counter < 5 )
// printf("ok ok ok");
return 0;
else
return 2;
}
}
最佳答案
我注意到,如果我在行下方发表评论,脚本就可以正常工作。我尝试使用的例子是这个:http://hack.lenotta.com/arduino-raspberry-pi-switching-light-with-nrf24l01/
这让我检查了 RF24 库(我使用的库和示例中使用的库)的差异,看起来它们使用的是不同的库。我会尝试改变它,看看会发生什么。
radio.read( &got_time, sizeof(unsigned long) );
关于javascript - error.code 139 在 node.js 中运行 exec 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26976268/