我有8个端口的SMS调制解调器,我想检查可用的端口,然后执行命令(如果该端口已被使用),它将检查可用的端口 这是代码
var SerialPort = require('serialport');
var serialPort = SerialPort.serialPort;
SerialPort.list(function (err, ports) {
ports.forEach(function(port) {
if(port.manufacturer == 'FTDI'){
//console.log(port.comName, port.pnpId, port.manufacturer);
var port = new SerialPort(port.comName , {
baudRate: 115200, dataBits: 8, parity: 'none', stopBits: 1, flowControl: false, xon : false, rtscts:false, xoff:false, xany:false, buffersize:0
});
port.on("open", onOpen);
port.on('error', onError);
port.on('data', onDataReceived);
async function onOpen(error) {
if(!error){
console.log('Port open sucessfully');
console.log(port.comName)
if(port.isOpen == true){
await send(port, "0XXXXXXXXXX", "Hi Test");
await read(port);
}
}
}
}
});
});