2014-05-23 121 views
1

我試圖通過node.js激活RFID閱讀器,然後發回標籤。Node.JS中未處理的「錯誤」事件

它很好用。它讀取標籤,以ID作爲響應,然後將ID發送到ping節點客戶端。

然而,每一個Node.js的程序拾取一組從RFID標籤數據的時間,它發送之後,它關閉下來,錯誤如下:

events.js:72 
     throw er; // Unhandled 'error' event 
      ^
Error: EBADF, read 

由此,節點處理退出每時每刻。這裏可能是什麼問題?

我的代碼如下:

// Socket.io server details 
var io = require('socket.io').listen(3000); 
// Serialport plugin declared and made a serialport variable 
var serialport = require("serialport"); 
var SerialPort = serialport.SerialPort; 
// Variable containing technical USB port details 
var serialPort = new SerialPort("/dev/ttyUSB0", 
{baudrate: 2400, parser: serialport.parsers.readline("\n")}, 
false); // this is the openImmediately flag [default is true] 



io.sockets.on('connection', function (socket) { 

console.log('user connected'); 

socket.on('ping', function (data) { 

serialPort.open(function() { 

// Open notification 
console.log('open'); 

//Start listening 
serialPort.on('data', function(data) { 


// If content is empty, filter out 
if (data.trim() !== '') { 
line = data; 

//Execute function again, get tag, handle tag and end process 
serialPort.close(function() { 
console.log('De uiteindelijke tag is ' + data); 
console.log('Ping received with data: ' + data); 
socket.emit('pong', data); 
console.log('closing'); 
}); 
console.log('hallo'); 
} 
}); 
}); 


}); 
}); 

回答

0

添加監聽器來處理錯誤的串行端口,如下面的代碼:

serialPort.on('error', function(error) { 
    console.log('The error: '+error); 
    //... 
}); 
+1

感謝您的評論。它捕獲錯誤,但應用程序確實關閉。怎麼會這樣? – MichaelP

+0

好的,但是是關閉應用程序相同的錯誤? – fmodos

+0

剛剛在你的console.log輸入後說''Aborted''。 – MichaelP

相關問題