javascript - Fiber Error with npm package serial-port with meteor -
i'm using serialport npm package meteor. i've used wrapasync list serial ports don't know how serialport.on method. i've error when want inser datas in cars collection :
meteor code must run within fiber. try wrapping callbacks pass non-meteor libraries meteor.bindenvironment.
code :
meteor.startup(function () { serialport = meteor.npmrequire('serialport'); // wrap method serialport.list call synchronously listserialports = function(callback) { serialport.list(function (err, ports) { callback(null, ports); }); } // reset cars collection }); meteor.methods({ serialportsrefresh: function () { // todo : problem when several arduinos ? config.remove({key:'serialports'}); // call serialport.list var asynclistserialports = meteor.wrapasync(listserialports); var resultslistserialports = asynclistserialports(); // insert results in database var configserialports = {key: "serialports", value: resultslistserialports[0].comname }; config.insert(configserialports); }, // connect serial port serialportconnect: function (port) { // debugger; // serialport = new serialport(port.value, {baudrate: 9600}); serialport = new serialport.serialport("/dev/ttyusb0", {baudrate: 9600, parser: serialport.parsers.readline("\n")}); // connectserialport(port); serialport.on('open', function() { console.log('port ' + port.value + ' open'); }); serialport.on('data', function(data) { dispatchmessages(data); //watchdog.insert({key: "receiving data", value: data }) }); sendtoarduino = function(message) { console.log(message); serialport.write(message); }; dispatchmessages = function(data) { console.log(data); //split data var datas = data.split(" "); if (datas[1] == "ok") { console.log("car " + datas[0] + " here"); // add car database cars.insert({ cid: datas[0], active: true }); } }; }, // ping bridge ping: function () { sendtoarduino("led13\n"); } });
the problem callbacks you're passing serialport.on
not run within same fiber method when they're invoked. in fact, won't run within fiber @ all, unless wrap them appropriately.
meteor.bindenvironment
runs passed function within fiber, copies in surrounding environment, necessary meteor stores sorts of variables within current fiber might required run callback in question.
so, if should work:
serialport.on('open', meteor.bindenvironment(function() { // wrapping 1 unnecessary @ present doesn't // needs run in fiber, should // wrap anyway can safely add more code // if required. console.log('port ' + port.value + ' open'); }, function(e) { // error-handler - don't have pass one, // if don't can make debugging nightmare. throw e; })); serialport.on('data', meteor.bindenvironment(function(data) { dispatchmessages(data); //watchdog.insert({key: "receiving data", value: data }) }, function(e) { throw e; }));
note need wrap callbacks within callbacks, etc., can become quite verbose (and makes putting var mbe = meteor.bindenvironment
@ top of methods file quite idea).
Comments
Post a Comment