Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function VirtualSerialPortFactory() {
try {
var SerialPort = require('serialport');
var serialportPackage = require('serialport/package.json');
var semver = require('semver');
// for v2.x serialport API
if(semver.satisfies(serialportPackage.version, '<3.X')) {
this.SerialPort = VirtualSerialPort;
this.parsers = SerialPort.parsers;
return this;
}
VirtualSerialPort.parsers = SerialPort.parsers;
return VirtualSerialPort;
} catch (error) {
console.warn('VirtualSerialPort - NO parsers available');
}
return VirtualSerialPort;
}
socket.on('connectTo', function(data) { // If a user picks a port to connect to, open a Node SerialPort Instance to it
data = data.split(',');
console.log(chalk.yellow('WARN:'), chalk.blue('Connecting to Port ' + data));
if (!isConnected) {
port = new serialport(data[0], { parser: serialport.parsers.readline("\n"), baudrate: parseInt(data[1]) });
} else {
socket.emit("connectStatus", 'Already Connected');
port.write("?\n"); // Lets check if its LasaurGrbl?
port.write("M115\n"); // Lets check if its Marlin?
port.write("version\n"); // Lets check if its Smoothieware?
port.write("$fb\n"); // Lets check if its TinyG
}
socket.on('closePort', function(data) { // If a user picks a port to connect to, open a Node SerialPort Instance to it
console.log(chalk.yellow('WARN:'), chalk.blue('Closing Port ' + port.path));
socket.emit("connectStatus", 'closed:'+port.path);
port.close();
});
port.on('open', function() {
socket.emit("connectStatus", 'opened:'+port.path);
!function outer(i){
sp[i] = {};
sp[i].port = ports[i].comName;
sp[i].manufacturer = ports[i].manufacturer;
sp[i].firmware = ""
sp[i].q = [];
sp[i].qCurrentMax = 0;
sp[i].lastSerialWrite = [];
sp[i].lastSerialReadLine = '';
sp[i].handle = new SerialPort(ports[i].comName, {
parser: serialport.parsers.readline("\n"),
baudrate: config.serialBaudRate
});
sp[i].sockets = [];
sp[i].handle.on("open", function() {
//console.log(
// chalk.green('Connecting to'),
// chalk.blue(sp[i].port),
// chalk.green('at'),
// chalk.blue(config.serialBaudRate)
//);
sp[i].handle.write("?\n"); // Lets check if its LasaurGrbl?
sp[i].handle.write("M115\n"); // Lets check if its Marlin?
sp[i].handle.write("version\n"); // Lets check if its Smoothieware?
sp[i].handle.write("$fb\n"); // Lets check if its TinyG
var socket = new WebSocket( 'ws://sockets.mybluemix.net' );
var open = false;
// Connected
socket.on( 'open', function() {
// Debug
console.log( 'WebSocket connected.' );
// Track connection
open = true;
} );
// Connect to serial port
// Look for newline delimiter
var serial = new SerialPort.SerialPort( '/dev/cu.usbmodem1411', {
parser: SerialPort.parsers.readline( '\n' )
} );
// Serial data arrived
serial.on( 'data', function( data ) {
var message = null;
// Send to console for debugging
console.log( data );
// Connected to socket
if( open ) {
// Assemble message
message = {
action: 'photocell',
value: parseInt( data.trim() )
};
// Port name looked up in Arduino IDE
var ARDUINO_PORT = '/dev/cu.usbmodem1411';
// List ports
// Arduino will display for manufacturer
// Here for debugging purposes
SerialPort.list( function( err, ports ) {
ports.forEach( function(port) {
console.log( port.comName )
} );
} );
// Connect to Arduino
// Look for newline delimiter
var serial = new SerialPort.SerialPort( ARDUINO_PORT, {
parser: SerialPort.parsers.readline( '\n' )
} );
// Used for alternative to sub-second delivery
var light = null;
// Serial data captured from Arduino
// Note that this happens at a sub-second rate
// You may want to adjust to fit your needs
serial.on( 'data', function( data ) {
// Send to console for debugging
// console.log( data );
// Alternative to sub-second delivery
// Store off for slower delivery
// Picked up by setInterval later
light = data.trim();
////////////////////////////////////////////////////////
// Use the cool library //
// git://github.com/voodootikigod/node-serialport.git //
// to send break condition. //
////////////////////////////////////////////////////////
var com = require("serialport");
var serialPort = new com.SerialPort("/dev/tty.usbserial-A700eTSd", {
baudrate: 115200,
parser: com.parsers.readline('\r\n')
}, false);
serialPort.on('open',function() {
console.log('Port open');
});
serialPort.on('data', function(data) {
console.log(data);
});
function asserting() {
console.log('asserting');
serialPort.set({brk:true}, function(err, something) {
console.log('asserted');
function openPort(port) {
var serialPort = new serial.SerialPort(port.comName, {
baudrate: port.baudrate
}, false);
// TODO: Retry open
serialPort.open(function (err) {
if (err) throw err;
console.log('Port opened:', port.comName);
makeTcpBridge(serialPort);
});
}
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 (
{