gpt4 book ai didi

javascript - npm包串行端口与 meteor 的光纤错误

转载 作者:行者123 更新时间:2023-11-29 21:47:23 27 4
gpt4 key购买 nike

我正在使用带有 meteor 的 SerialPort npm 包。我使用 wrapAsync 来列出串行端口,但我不知道如何使用 serialPort.on 方法。当我想在我的 Cars 集合中插入数据时出现错误:

Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.

代码:

Meteor.startup(function () {
SerialPort = Meteor.npmRequire('serialport');
// Wrap method SerialPort.list to call it 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] + " is here");
// Add car to database
Cars.insert({
cid: datas[0],
active: true
});
}

};
},
// Ping bridge
ping: function () {
sendToArduino("LED13\n");
}


});

最佳答案

问题是您传递给 serialPort.on 的回调在被调用时不会在与您的方法相同的纤程中运行。事实上,它们根本不会在光纤中运行,除非您适本地包裹它们。

Meteor.bindEnvironment 在 fiber 中运行传递的函数,但也在周围环境中复制,这是必要的,因为 Meteor 在当前 fiber 中存储各种变量,这些变量可能需要运行有问题的回调。

所以,如果您这样做,它应该会起作用:

serialPort.on('open', Meteor.bindEnvironment(function() {
// Wrapping this one is unnecessary at present as it doesn't
// do anything that needs to be run in a fiber, but you should
// probably wrap it anyway so that you can safely add more code
// if required.
console.log('Port ' + port.value + ' open');
}, function(e) {
// This is an error-handler - you don't have to pass one, but
// if you don't it can make debugging a nightmare.
throw e;
}));
serialPort.on('data', Meteor.bindEnvironment(function(data) {
dispatchMessages(data);
//Watchdog.insert({key: "Receiving data", value: data })
}, function(e) {
throw e;
}));

请注意,您还需要将回调包装在回调等中,这可能会变得非常冗长(并且使得在您的方法文件顶部放置类似 var mBE = Meteor.bindEnvironment 的内容相当麻烦好主意)。

关于javascript - npm包串行端口与 meteor 的光纤错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30606028/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com