Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function openPort () {
let start_f = Math.floor ( START_FREQ / 1000 );
let stop_f = Math.floor ( STOP_FREQ / 1000 );
if( !PORT_MENU_SELECTION || PORT_MENU_SELECTION === 'AUTO' ) { // Automatic port selection
SerialPort.list().then ( (ports, err) => {
if ( err ) {
console.log ( err );
return;
}
let i = 0;
console.log ( "Trying port " + ports[i].comName + " ...");
port = new SerialPort ( ports[i].comName, { baudRate : 500000 }, function ( err ) {
if ( err ) // Most likely the reason for the error is that the RF Explorer is not connected to this port. So we don't print an error message here.
return;
setCallbacks();
sendAnalyzer_SetConfig ( start_f, stop_f );
});
const refreshPortList = () => {
logger.info('serialRelay: refreshPortList');
serialport.list().then((list) => {
Object.keys(list).forEach((key) => {
const portObj = list[key];
const { comName, manufacturer } = portObj;
// Scrape for Arduinos...
if (autoEnableArduinos === true
&& manufacturer !== undefined) {
if (manufacturer.indexOf('Arduino') !== -1
|| manufacturer.indexOf('Silicon Labs') !== -1) {
console.log(`Auto-enabling: ${comName} - ${manufacturer}`);
logger.info(`serialRelay: Auto-enabling: ${comName} - ${manufacturer}`);
enableSerialPort(comName, { baudRate: defaultBaudRate });
}
}
});
ipcMain.on ( "SET_COUNTRY", (event, message) => {
menuJSON[MENU_COUNTRY].submenu.forEach ( function ( elem ) {
if ( elem.code === message.country_code ) {
elem.checked = true;
Menu.setApplicationMenu ( Menu.buildFromTemplate ( menuJSON ) );
}
});
});
menuJSON.push ( portMenuJSON );
menuJSON.push ( toolsMenuJSON );
menuJSON.push ( helpMenuJSON );
// Add serial ports to the menu
SerialPort.list().then ( (ports, err) => {
let portNameArr = [];
if ( err ) {
console.log ( err );
return;
}
ports.forEach ( ( port ) => {
portNameArr.push ( port.comName );
});
menuJSON[MENU_PORT].submenu[0] = { label: 'Auto', type: 'radio', click () { wc.send ( 'SET_PORT', portNameArr ); } }
portNameArr.forEach ( ( port ) => {
menuJSON[MENU_PORT].submenu.push (
{
function port_list(callback) {
SerialPort.list()
.then((ports) => {
// for (let i=0, l=ports.length; i {
return port.manufacturer == "0403" || port.vendorId == "0403";
}));
})
.catch(() => {
callback([]);
});
}
refreshPorts = (autoconnect = false) => {
if (!_.isBoolean(autoconnect)) autoconnect = false
SerialPort.list((error, ports) => {
if (error) {
console.log('failed to get devices')
} else {
ports = _.map(ports, (port) => port.comName)
ports = _.filter(ports, (port) => !port.match(/[Bb]luetooth/) && port.match(/\/dev\/cu/))
this.ports = ports
let defaultDevice = this._findDefaultDevice(this.ports)
if (defaultDevice) {
this.portSelected = defaultDevice
if (autoconnect === true) this.connect()
} else {
this.portSelected = this.ports[0]
}
}
function scanForPorts(callback) {
serialport.list(callback);
}
connect() {
SerialPort.list().then((ports) => {
atom.notifications.addInfo("Attempting to locate board");
ports = ports.map((v) => {
if (!v.vendorId) {
v.vendorId = "";
}
return v;
});
let chosenBoard = ports.find(e => vendorIDs.indexOf(e.vendorId.toLowerCase()) != -1);
if (!chosenBoard) {
atom.notifications.addError("Could not find a valid device");
return;
}
function probeSerial(then) {
let match = null;
SerialPort.list((err, ports) => {
ports.forEach(port => {
if (port.pnpId) {
match = port.comName;
} else if (port.manufacturer && port.manufacturer.toLowerCase().indexOf("arduino") >= 0) {
match = port.comName;
} else if (!match && (port.vendorId || port.productId || port.serialNumber)) {
match = port.comnName;
}
});
then(match);
});
}
RED.httpAdmin.get('/modbus/serial/ports', RED.auth.needsPermission('serial.read'), function (req, res) {
const SerialPort = require('serialport')
SerialPort.list().then(ports => {
res.json(ports)
}).catch(err => {
coreModbusClient.internalDebug(err.message)
})
})
}
async function list() {
return SerialPort.list();
}