neurosity / openbci-observable

Making OpenBCI for Node Reactive
MIT License
37 stars 7 forks source link

UnhandledPromiseRejectionWarning: TypeError: No path specified #24

Open petewall opened 6 years ago

petewall commented 6 years ago

I'm trying to run the Cyton example code and I'm getting a "No path specified" exception

Here's the code that I'm using:

const { Cyton } = require('openbci-observable');

async function init () {
    const cyton = new Cyton();
    await cyton.connect();
    await cyton.start();

    cyton.stream.subscribe(sample =>
        console.log('sample', sample)
    );
}

init();

Inside new Cyton(), the init function calls autoFindOpenBCIBoard(), which tries to find a valid serial port. If successful, it ends with this code:

          if (this.options.verbose) console.log('auto found board');
          resolve(this.portName);

However, this doesn't block the call to await cyton.connect(); which tries to connect to portName which is undefined.

petewall commented 6 years ago

I tried to delay the call to connect by doing this:

const { Cyton } = require('openbci-observable');
async function init () {
    const cyton = new Cyton({ verbose: true });
    setTimeout(async function () {
        await cyton.connect();
        await cyton.start();
        cyton.stream.subscribe(sample =>
            console.log('sample', sample)
        );

    }, 1000);
}
init();

However, I noticed that the call to connect() is actually blowing away the portName.

Cyton.prototype.connect = function (portName) {
...
      this.portName = portName;
      if (this.options.verbose) console.log('using real board ' + portName);
      this.serial = new SerialPort(portName, {
...

So, calling connect() with no arguments will not work.

I changed the line in my code to await cyton.connect(cyton.portName);, now will use the auto discovered serial port.

petewall commented 6 years ago

This however doesn't solve the problem of connect(portName) being called before the constructor has resolved the port in the constructor's call to the init() function. Since a constructor doesn't return a promise, I don't know if we can await it. However, if I just resign to auto-discover the port a second time, I can await on that.

This is my code that can successfully connect to my Cyton board and stream data:

const { Cyton } = require('openbci-observable');
async function init () {
    const cyton = new Cyton({ verbose: true });
    await cyton.autoFindOpenBCIBoard();
    await cyton.connect(cyton.portName);
    await cyton.start();
    cyton.stream.subscribe(sample =>
        console.log('sample', sample)
    );
}
init();
petewall commented 6 years ago

If it helps. I'm using Mac OS X 10.13.3, with node version 9.8.0.