enocean-js / node-enocean-button

a button for node-enocean
GNU General Public License v2.0
2 stars 1 forks source link

example crash on enocean.close() #2

Closed Cowprod closed 7 years ago

Cowprod commented 7 years ago

Hi,

First thanks for your work, this module works great ! but the example crash on : enocean.close() with the following error message:

Unhandled rejection Error: Port is not open at SerialPort.write (/home/pi/node_modules/serialport/lib/serialport.js:259:24) at SerialPortListener. (/home/pi/node_modules/node-enocean/index.js:148:17) at SerialPortListener.sendAsync (/home/pi/node_modules/node-enocean/index.js:145:12) at . (/home/pi/node_modules/node-enocean-button/index.js:86:15) at tryBlock (/home/pi/node_modules/asyncawait/src/async/fiberManager.js:39:33) at runInFiber (/home/pi/node_modules/asyncawait/src/async/fiberManager.js:26:9)

Tested on raspbian jessie light, node 6.8.1.

Regards

Holger-Will commented 7 years ago

Thanks for the report. i will have a look at the issue...

Holger-Will commented 7 years ago

I can not reproduce the issue... i have just testet with an old RPi2 Noobs and Node 6.4.0,6.8.1,6.9.1... and some Linux machines (Arch, Ubuntu) with the same Node versions.

There was a version of this button that crashed for me as well, this was before i used asycawait. Are you sure you work with the latest version? Is this an npm install or a clone?

Cowprod commented 7 years ago

In the code:

var async = require('asyncawait/async'); var await = require('asyncawait/await');

It was via npm install on a RPI 3 and an up to date raspbian jessie light.

I just comment the line that crashed to make your project work like a charm, the communication port may close automatically...

2016-10-21 12:26 GMT+02:00 Holger Will notifications@github.com:

I can not reproduce the issue... i have just testet with an old RPi2 Noobs and Node 6.4.0,6.8.1,6.9.1... and some Linux machines (Arch, Ubuntu) with the same Node versions.

There was a version of this button that crashed for me as well, this was before i used asycawait. Are you sure you work with the latest version? Is this an npm install or a clone?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Holger-Will/node-enocean-button/issues/2#issuecomment-255347658, or mute the thread https://github.com/notifications/unsubscribe-auth/AJns_z3Fn3685kPIALRMyAbKu5iKZypuks5q2JNGgaJpZM4KZriC .

Holger-Will commented 7 years ago

Thanks for the info. the port should definitely not close without beeing told to do so... :-( I will try on exactly your setup (RPi3 + Raspian) as soon as i find some time... maybe this weekend...

mwittig commented 7 years ago

I am getting the same exception running on Raspi2, node v4.6.2. I think the problem is that the close command is executed before the data has been written to serialport. If I defer the close by 100ms as follows, it works fine for me. I have also tried process.nextTick but this did not succeed.

var enocean = require("node-enocean")();
var Button = require("node-enocean-button")

enocean.listen("/dev/ttyAMA0")

enocean.on("ready",function(){
    console.log("ready")
    var button = new Button(enocean,1)
    if(process.argv[2] == "on"){
        button.B1.click()
    }else{
        button.B0.click()
    }
    setTimeout(function() {
        enocean.close()
    }, 100);
});
Holger-Will commented 7 years ago

the current example should look like this:

var enocean = require("node-enocean")(); var Button = require("node-enocean-button")

enocean.listen("/dev/ttyAMA0")

enocean.on("ready", async(function(){
    console.log("ready")
    var button = new Button(enocean,1)
    if(process.argv[2] == "on"){
        await(button.B1.click())
    }else{
        await(button.B0.click())
    }
    setTimeout(function() {
        enocean.close()
    }, 100);
}));

notioce the async() and await(), this should make every workaround with setTimeout obsolete... this is not real async await though. as soon as async await is switch on by default (in node 8.0.0 i think..) i will switch to real async await...

Holger-Will commented 7 years ago

just tested this on a fresh Debian jessie (8.0) on a Pi3 and node 7.2.1. I tested with a cloned version as well as with npm installed one. Both versions run as expected. I still can not reproduce the error... I'm in the midst of turning the button into a real promisse based solution so it runs with co, koa, is yieldable in generators as well as playing nicely with real async await.

Holger-Will commented 7 years ago

ohh... sorry. I just realized, that the example code in the readme is still the old code and that is causing the crash... everything will be updated soon...

Holger-Will commented 7 years ago

Fixed! Thanks again for reporting! Everything should run as expected now that i use Promises.