已调用 NodeJS 异步回调



我一直在搜索,但我在代码上找不到导致此错误的原因:

(节点:7672( 未处理的承诺拒绝警告:错误:回调是 已经在 C:\Respaldos\SerialReceiveode_modules\async\dist\async.js:966:32 调用

在串行端口。(C:\Respaldos\SerialReceive\receive.js:35:4(

这是我的代码:

function receiveEnq(callback) {
port.on('data', function (data) {
if ((data.length == 1) && (data[0] == 5)) {
console.log('Received: ENQ');
return callback(null, 'done');
} else {
callback('Error', 'Received:' + data);
}
});
}
function sendAck(err, callback) {
port.write(Buffer.from([6]));
console.log('Transmitted: ACK');
callback(null, payload[sequence]);
}
function receiveData(data, callback) {
port.on('data', function (data) {
console.log(data);
callback(null, 'done');
});
}
async.waterfall([
receiveEnq,
sendAck,
receiveData
], function (err, result) {
console.log(result)
if (err) {
process.abort();
}
});

你能帮我发现错误吗?

问候

我不得不完全重写我的函数。我把它留在这里以防有人有同样的问题

const SerialPort = require('serialport');
const ByteLength = SerialPort.parsers.ByteLength;
const port = new SerialPort("COM6");
const parser = new ByteLength({length: 1});
port.pipe(parser);
var state = 0;
var cache = [];
var history;
parser.on('data', function (data) {
cache.push(data[0]);
flowcontrol();
});
function porterr() {
console.log('error');
process.exit(1);
}
function flowcontrol() {
switch (state) {
case 0:
// Recives 1 byte
if (cache.length !== 1) {
return;
}
// Sends Answer and changes to next state
port.write(Buffer.from([6]));
state++;
cache.length = 0;
break;
case 1:
// Recives 1 byte
if (cache.length !== 1) {
return;
}
// Sends Answer and changes to next state
port.write(Buffer.from([6]));
state++;
cache.length = 0;
break;
// Define more states here... 
// .....
// .....
// .....
default:
console.log('state not defined');
break;
}
}

最新更新