在node.js中运行exec时error.code 139

我使用node.js来执行一个外部文件(编译后的cpp)。 如果我自己执行文件,程序工作正常。 我应该得到以下输出:

有回应534,来回延迟:9569

我想使用websokets取得的价值,并在网页上显示。 不幸的是,当从node.js运行程序时,我有以下输出:

stdout:stderr:exec错误:null我不明白发生了什么,为什么如果我从命令行运行应用程序,我得到所需的输出?

谢谢,请原谅我的愚蠢的问题!

改变我执行文件的方式后,我得到以下输出:

stdout:stderr:exec错误: 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); } 

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

light.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) );