NodeJS using node-serialport sometimes becomes unresponsive

烈酒焚心 提交于 2020-01-24 17:04:55

问题


I'm using NodeJS to connect a barcode scanner to a web front-end. The barcode scanner's serial port runs to the PC (Windows7 pro x64) through a usb-to-serial converter. I'm using node-serialport. This setup is running at an installation in a museum.

The computers reboot daily and sometimes the barcode scanner will work fine the whole day, but about 25% of the time it will stop working throughout the day. With stop working I mean that NodeJS isn't relaying the incoming serial stream anymore. The barcode scanner itself seems to be doing fine, it still bleeps happily and after a reboot it will function just fine.. until NodeJS stops working again.

When the scanner stops working the NodeJS windows doesn't show any error codes but it's fully unresponsive by then. By then it's seemingly impossible to close the NodeJS instance. I've tried Ctrl-C, ending the process through the task manager and using taskkill to kill the PID of NodeJS from a CMD window. They all have ZERO effect. When using Taskkill the CMD window will happily say that the process was killed, but the NodeJS window won't close and also still exists in the task manager. Only a forceful reboot will 'solve' this.

I feel I have very little information to go on. I'd love a few tips on what to try next.


回答1:


I had an issue with my code in that the serialport.write command would fail after 3 attempts. It looked like the issue was that my serialport.write function waits for a response after the write . However I also have some synchronous code..(console.log) which cannot be executed as the callback response function will not run until the action triggered by the serialport is ran. This seems to confuse nodejs, seems you cannot mix async callbacks and sync code in the same routine

global.serialPort.write("a", function(err, results) {

   console.log('results ' + results);  });

console.log('completed');

Removing that last line meant that routine worked. Take a look at your code and see if you have that mixture



来源:https://stackoverflow.com/questions/17113379/nodejs-using-node-serialport-sometimes-becomes-unresponsive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!