问题
I have a node process running under Ubuntu 12.04 LTS as a service (it has a configuration file under /etc/init). The system runs on a headless x86 routerboard to which a barcode reader is connected. What I need to do is be able to read the input from the barcode reader (which behaves like a USB keyboard for all intents and purposes) so that it can be sent to a remote node for processing, but I'd like to avoid having to parse raw HID input.
Right now I'm using node-hid
but the solution is temporary since I don't know how to parse the incoming data buffer - I have been able to recognize a pattern and map it in a hashtable so that when I receive, say,
02 00 00 00 00 00 00 00
02 00 03 00 00 00 00 00
00 00 00 00 00 00 00 00
I know I have read a 'C' from a Code39 barcode, but this is not portable and, quite frankly, it's so dirty that I'm almost ashamed of it.
What I do know, though, is that the operating system is perfectly able to decode the input it receives from the barcode reader: I can use it to provide standard stdin
input when the process is in the foreground, using
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(input) { ... });
I can happily read whatever comes from the barcode reader. This, of course, doesn't work when the process runs in the background, so my question is: is there a way to capture the stdin buffer so that it can be read from a NodeJS process running in the background?
Bonus question: if what I'm asking is not possible, can someone point me to a reasonable reference/documentation that can allow me to decipher the HID input I'm receiving (node-hid
provides only the data buffer) so that I can at least write a portable, generic function to decode it? I'm sure it has already been done, I'd like to avoid having to rediscover hot water :)
来源:https://stackoverflow.com/questions/21074452/nodejs-read-stdin-from-background-process