简体   繁体   English

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

[英]error.code 139 when running exec in node.js

I'm using node.js to execute an external file (compiled cpp). 我正在使用node.js执行外部文件(已编译的cpp)。 If I execute the file on it's own, the program works ok. 如果我自己执行文件,则程序运行正常。 I should get the following output: 我应该得到以下输出:

Got response 534, round-trip delay: 9569 得到响应534,往返延迟:9569

I would like to use websokets to take the value and display on a webpage. 我想使用websokets来获取价值并显示在网页上。 Unfortunately, when running the program from node.js I have the following output: 不幸的是,当从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错误:null我不明白发生了什么,为什么如果我从命令行运行应用程序,却得到所需的输出?

Thank you and please forgive me for the dumb question! 谢谢您,请原谅我的愚蠢问题!

After changing the way I execute the file I get the following output: 更改执行文件的方式后,将得到以下输出:

stdout: stderr: exec error: 139 标准输出:标准错误:执行错误: 139

the changes I have made are: 我所做的更改是:

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 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 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;
     }
}

I noticed that if I comment below line, the script works ok. 我注意到,如果我在下面的行中注释,该脚本可以正常工作。 The example I'm trying to use is this one: http://hack.lenotta.com/arduino-raspberry-pi-switching-light-with-nrf24l01/ 我尝试使用的示例就是以下示例: http : //hack.lenotta.com/arduino-raspberry-pi-switching-light-with-nrf24l01/

This made me to check the differences of RF24 libraries (the one I use and the one used in the example) and it seems that they are using a different library. 这使我检查了RF24库的差异(我使用的和本示例中使用的一个),似乎它们使用的是不同的库。 I'll try to change it and see what it happens. 我将尝试更改它,看看会发生什么。

radio.read( &got_time, sizeof(unsigned long) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM