未处理的'错误'' Node.JS中的事件

时间:2014-05-23 21:08:23

标签: javascript node.js sockets

我试图通过node.js激活RFID阅读器,然后发回标签。

效果很好。它读取标记,以ID响应,然后将ID发送到pinging节点客户端。

但是,每当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');
 }
 });
 });


 });
});

1 个答案:

答案 0 :(得分:0)

添加一个侦听器来处理串口中的错误,如下面的代码所示:

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