const SerialPort = require('serialport') const Readline = require('@serialport/parser-readline') const port = new SerialPort('/dev/ttyUSB2') const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) const parser = port.pipe(new Readline({ delimiter: '\r\n' })) parser.on('data', (data) => { if (data.includes('+CSQ:')) { try { const str = data.substring(data.indexOf(':') + 2) const list = str.split(',') // 分3等份正常信号值10~31 const s = +list[0] if (s >= 10 && s <= 31) { if (s <= 17) { console.log('信号差', 1, s); } else if (s > 17 && s <= 24) { console.log('信号弱', 2, s); } else { console.log('信号强', 3, s); } } else { console.log('信号差', 1); } } catch (error) { console.log(error); } } }) port.write(Buffer.from('AT+CSQ\r\n')) test() async function test() { await sleep(5000) port.write(Buffer.from('AT+CSQ\r\n')) await test() }