Saturday, 3 June 2017

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

When the number is first sent from nodejs to arduino. I see the error "Error Resource temporarily unavailable Cannot lock port"

I use linux.

I fist update some value (in webserver) and Serialport catch the value. so I can see the value in arduino serial monitor.

but after I update some value again. it cause error.

this is nodejs code.

>

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

this is arduino code

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 = "";

}

help me please..



via kim J

No comments:

Post a Comment