All,
I'm using node-hid to send commands to a USB to I2C adapter. It is possible for the firmware on that adapter to get locked up when unplugging and replugging the device while my program is running. I have been able to determine that my program is hanging at my call to hid write which is a synchronous call. When the firmware locks up my program becomes unresponsive. If I replug the device and let the firmware recover then I can catch the write failure and handle appropriately. What I'm trying to do is alert the user that the device seems to have been locked up and they need to unplug and replug it.
I tried doing something like the below to set a timeout for the write call and if it hadn't completed and cleared the timeout in 2 seconds then tell the user to reset the device.
function alertUser() {
alert("It appears that the USB2ANY has had an error\nPlease disconnect and reconnect the device and clear this alert");
}
try {
//TODO: Gets stuck here when USB2ANY goes south. Need a way to stop this or alert user to reset the USB2ANY
// write is a synchronous call
var timer = setInterval(alertUser, 2000);
console.log("Calling hid write for " + command);
this.hid.write(temp);
clearInterval(timer);
console.log("Finished hid write for " + command);
} catch(err) {
console.log("HID write failed = " + err);
u2aFlushCallbacks();
}
Unfortunately since the write command is synchronous the timer doesn't fire. I'm not sure how to work around this. Can I wrap this call somehow to make it asynchronous?