We are using an external library called node-hid-stream to get data from a barcode reader. The library was installed with npm and "imported" via settings.js into Node-RED:
functionGlobalContext: {
nodehidstream:require('node-hid-stream')
},
To catch data from the scanner we have created a listener inside a function node. This is probably not recommended but we have not found another/better solution so far. The function node "fires" whenever data from the scanner is coming in. The function node looks like this:
try{
//device = new hid.HID(0x05e0, 0x1200);
msg.payload = "connected";
msg.color = "green";
var KeyboardLines = global.get("nodehidstream").KeyboardLines;
flow.set("keyboard_lines", KeyboardLines);
var device = new KeyboardLines({ vendorId: 0x05e0, productId: 0x1200 });
flow.set("scanner_device", device);
// this listener handles data from barcode and is fired everytime a code is scanned
device.on("data", function(data) {
node.warn("from scanner node (data): "+data);
msg.payload = data;
node.send([null,msg]);
});
flow.set("scanner", true);
}catch(e){
node.warn(e);
flow.set("scanner", false);
msg.color = "red";
msg.payload = "not connected";
}
return [msg, null];
In the beginning we did not add any code inside the closing tab and therefore run into problems, because a new listener was registered every time we deployed our flow. The closing tab looks as follows:
flow.get("scanner_device").close();
flow.set("scanner_device", undefined);
flow.set("keyboard_lines", undefined);
flow.set("scanner", false);
After a few deploys the Node-RED service hangs and neither the flow nor the gui (dashboard) is accessible anymore and Node-RED has to be restarted manually. We are quite sure that the problem is cause either by our function node or the external library node-hid-stream but are unable to solve the problem.
After stopping the Node-RED service the above mentioned close() command causes a timeout error, something like "error while closing the node: Close timed out"
We are using Node-RED 1.2.9
node-hid: 2.1.1
node-hid-stream: 1.1.0