繁体   English   中英

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

[英]Unhandled ''error'' event in Node.JS

我正在尝试通过node.js激活RFID阅读器,然后将标签发送回去。

效果很好。 它读取标签,用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');
 }
 });
 });


 });
});

添加一个侦听器以处理串行端口中的错误,例如以下代码:

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

暂无
暂无

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

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