简体   繁体   English

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

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

I'm trying to active an RFID reader through node.js and then send the tag back. 我正在尝试通过node.js激活RFID阅读器,然后将标签发送回去。

It works great. 效果很好。 It reads the tag, responds with an ID, then send the ID to the pinging node client. 它读取标签,用ID响应,然后将ID发送到ping节点客户端。

However, every time the node.JS program picks up a set of data from an RFID tag, after it sends, it closes down with the following error: 但是,每次node.JS程序从RFID标签中拾取一组数据后,在发送后都会关闭,并显示以下错误:

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

This causes the node process to quit all the time. 这将导致节点进程始终退出。 What could be the problem here? 这可能是什么问题?

My code is the following; 我的代码如下;

// 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');
 }
 });
 });


 });
});

Add a listener to handle the error in the serial port, like the code below: 添加一个侦听器以处理串行端口中的错误,例如以下代码:

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

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

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