通过串行连接时,立即发生“打开”事件,但是如果添加了setTimeout,它将在该时间之前延迟。我该如何解决?
const Serialport = require('serialport');
class Serial {
connect() {
const client = new Serialport('COM2', {
autoOpen: false,
});
console.time('Complete Connect');
client.open(err => {
console.timeEnd('Complete Connect');
});
}
}
const serialport = new Serial();
serialport.connect();
setTimeout(() => {
console.log('any work');
}, 3000);
打印:
1. any work
2. Complete Connect: 3025.978ms