node-serialport only communicates with Arduino if another app has already connected to the port

蹲街弑〆低调 提交于 2020-01-17 01:39:06

问题


Running on node in OS X, I am trying to use node-serialport to talk to an Arduino. All communication to the Arduino works as expected when using Arduino IDE's Serial Monitor, or the OS X utility SerialTools. However, when just running my node app, node-serialport tells me the connection is successful, but I get no communication. If I first make a connection to the arduino with Arduino IDE's Serial Monitor or SerialPorts, then run my node app, the node app sends and receives data just fine using node-serialport.

I'm not familiar with serial communication, but it seems like the other serial utilities are able to properly start a connection (which is then available to node-serialport), but node-serialport is not able to connect on its own.

Is there a way to get absolutely all connection information, so I can compar the utilities' successful connections to node-serialports non-working connection?

Any other ideas as to why this would be happening?


回答1:


I have a working solution, but unfortunately not a complete explanation. Reviewing some related issues such as What's going on after DTR/RTS is sent to an FTDI-based Arduino board?, I determined that even just restarting the node app (rather than requiring another serial connection app) gave node the ability to communicate through the serial port. I'm beyond my depth, but I suspect that initially establishing the RTS connection restarts the arduino, and only after that happens can node-serialport communicate through the connection.

My workaround is to simply give the Arduino some time to reset before attempting a second serialport connection, which works.

var firstConnect = true;
serialPort.open(function (error) { 
    if (firstConnect){
        firstConnect = false;
        //First connection, letting Arduino reset
        setTimeout(function(){serialPort.open()},4000)
    } else {
        //Second connection, which will work
        serialPort.on('data', function(data) {
            //data parsing function
            //...
        }
    }
});


来源:https://stackoverflow.com/questions/29735047/node-serialport-only-communicates-with-arduino-if-another-app-has-already-connec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!