繁体   English   中英

带有串行端口的node.js <-> arduino。 错误资源暂时不可用无法锁定端口

[英]node.js <-> arduino with serial port. Error Resource temporarily unavailable Cannot lock port

当数字首次从NodeJS发送到Arduino时,我看到此错误:

Error Resource temporarily unavailable Cannot lock port

我使用Linux。

我首先更新了一些值(在Web服务器中),并且串行端口捕获了该值,所以我可以在Arduino串行监视器中看到该值。

但是,当我再次更新某些值后,它将导致错误。

这是NodeJS代码:

var serialport = require('serialport'), 

SerialPort = serialport.SerialPort, 
portName = '/dev/ttyACM0',       
portConfig = {
    baudRate: 9600,
    // call myPort.on('data') when a newline is received:
    //parser: serialport.parsers.readline('\n')
};

... (code) ... 

pool.getConnection(function(err,connection)
{
    var sql = "update value set temperature=? , humidity=? where idx=?";
    connection.query(sql,[temperature,humidity,idx],function(err,result)
    {
        console.log(result);
        if(err) console.error("update errrr : ",err);
        var myPort = new SerialPort(portName, portConfig);
        myPort.on('open', openPort);

    function openPort() {
          var temp = temperature; 
            console.log('port open');
              console.log('baud rate: ' + myPort.options.baudRate);

        function sendData() {

        //myPort.write(temp.toString());        for(var i=0; i<temp.length; i++) {
      myPort.write(new Buffer(temp[i], 'ascii'), function(err, results) {
        });

      }

        console.log('Sending ' + temp + ' out the serial port');
    }       setTimeout(sendData, 500);      myPort.close

   }
     res.redirect('/');
         connection.release();
    });

}); });

这是Arduino代码:

String inData = "";
int led = 13;

void setup()  
{
  pinMode(led, OUTPUT);
   Serial.begin(9600);
   analogReference(INTERNAL);
}

void loop()  
{
  while (Serial.available() > 0) {
     long received = Serial.parseInt();
      inData.concat(received);

     Serial.println(inData);
  }

     inData = "";

}

我找到了答案

function openPort() {
            var temp = temperature; 
        console.log('port open');
        console.log('baud rate: ' + myPort.options.baudRate);
        setTimeout(sendData, 1);
        function sendData() {myPort.write(new Buffer(temp, 'ascii'), function(err, results) {});
            console.log('Sending ' + temp + ' out the serial port');
    myPort.close(function () {console.log('port Closed.');});
        }
    }

暂无
暂无

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

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