var SerialPort = require('serialport'); var ports = [ 'COM1', // 'COM3', 'COM4', 'COM5', 'COM6', 'COM8', 'COM9' ]; ports.forEach((sport)=>{ console.log('new',sport); var p=new SerialPort(sport,{ baudRate: 9600, dataBits: 8, autoOpen: true }); p.on('error',(e)=>{ console.log('error',e); }); p.on('disconnect',()=>{ console.log('disconnect',sport) }); p.on('close',()=>{ console.log('close',sport) }); p.on('open',()=>{ console.log('opened',sport); setTimeout(()=>{ console.log('closing',sport) p.close((e)=>{ if (!e) console.log('closed',sport) else console.log('close error',sport,e); }); },1000); }); });