当这个号码首先从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 = "";
}
答案 0 :(得分:0)
我找到了答案
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.');});
}
}