使用node-serialport的NodeJS有时无响应 [英] NodeJS using node-serialport sometimes becomes unresponsive

查看:445
本文介绍了使用node-serialport的NodeJS有时无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NodeJS将条形码扫描仪连接到Web前端.条形码扫描仪的串行端口通过USB到串行转换器连接到PC(Windows7 pro x64).我正在使用 node-serialport .此设置在博物馆的某个设备上运行.

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.

计算机每天都会重新启动,有时条形码扫描仪会整天正常工作,但是大约有25%的时间将整天停止工作.随着停止工作,我的意思是NodeJS不再中继传入的串行流.条形码扫描仪本身似乎运行良好,但仍会愉快地发出哔哔声,并且在重新启动后仍能正常运行..直到NodeJS再次停止工作.

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.

当扫描仪停止工作时,NodeJS窗口不会显示任何错误代码,但是到那时它完全没有响应.到那时,关闭NodeJS实例似乎是不可能的.我尝试了Ctrl-C,通过任务管理器结束了该过程,并使用taskkill从CMD窗口中杀死了NodeJS的PID.它们都具有零效果.使用Taskkill时,CMD窗口会高兴地说该进程已被杀死,但是NodeJS窗口不会关闭,并且仍存在于任务管理器中.只有强行重启才能解决"此问题.

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.

推荐答案

我的代码存在问题,因为3次尝试后serialport.write命令将失败.看来问题出在我的serialport.write函数在写之后等待响应.但是我也有一些同步代码..(console.log),它们不能执行,因为在运行由串行端口触发的操作之前,回调响应函数将不会运行.这似乎使nodejs感到困惑,似乎您不能在同一例程中混合使用异步回调和同步代码

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

这篇关于使用node-serialport的NodeJS有时无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆