簡體   English   中英

在node.js中運行exec時出現error.code 139

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

我正在使用node.js執行外部文件(已編譯的cpp)。 如果我自己執行文件,則程序運行正常。 我應該得到以下輸出:

得到響應534,往返延遲:9569

我想使用websokets來獲取價值並顯示在網頁上。 不幸的是,當從node.js運行程序時,我得到以下輸出:

stdout:stderr:exec錯誤:null我不明白發生了什么,為什么如果我從命令行運行應用程序,卻得到所需的輸出?

謝謝您,請原諒我的愚蠢問題!

更改執行文件的方式后,將得到以下輸出:

標准輸出:標准錯誤:執行錯誤: 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) );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM