Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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();
availableArduinoCOMs.forEach(availableArduinoCOM => {
const port = new SerialPort(availableArduinoCOM.comName, {
parser: require('serialport').parsers.readline('\n'),
baudRate: 9600
}, err => {
if (err) {
return nodecg.log.warn('Error opening propsective port:\n\t', err.message);
}
if (canWriteToSerial(port)) {
port.write('handshake\n', error => {
if (error) {
// We have to just discard this error, because an Arduino programmed in Joypad
// mode will show up as an available COM, but can't actually be written to.
}
});
}
});
setTimeout(function() {
// reconnect after 500ms
_this.serialPort.close();
try {
_this.serialPort = new SerialPort(_this._options.path, {
baudrate: 38400,
parser: serialport.parsers.readline("\r\n")
}, false);
_this.serialPort.on('error', function(error) {
winston.error("H2183Compass: high speed - error event fired", error);
});
_this.serialPort.open( function(error) {
if (error) winston.error("H2183Compass: high speed - error on open", error);
onHighSpeedConnect();
});
} catch (error) {
winston.error("H2183Compass: high speed - exception on open", error);
}
}, 1000);
};
const _ = require('lodash')
const EventEmitter = require('events')
const SerialPort = require('serialport')
const Ready = SerialPort.parsers.Ready
const Readline = SerialPort.parsers.Readline
module.exports = class ReconnectableSerialPort extends EventEmitter{
constructor(pattern, options) {
super()
this.port = null
this.pattern = pattern
this.options = options
// Configure readline parser
// this.ready = new Ready({data: 'init'})
this.readLine = new Readline({
delimiter: '\r\n',
})
var setupSerial = function() {
obj.serial = new serialp(port,{
baudrate: baud,
databits: databits,
parity: parity,
stopbits: stopbits,
parser: serialp.parsers.raw,
autoOpen: true
}, function(err, results) {
if (err) {
if (err.toString() !== olderr) {
olderr = err.toString();
RED.log.error(RED._("serial.errors.error",{port:port,error:olderr}));
}
obj.tout = setTimeout(function() {
setupSerial();
}, settings.serialReconnectTime);
}
});
obj.serial.on('error', function(err) {
RED.log.error(RED._("serial.errors.error",{port:port,error:err.toString()}));
obj._emitter.emit('closed');
obj.tout = setTimeout(function() {
PrinterDriver.prototype.tryBaudrate = function(baudRate, callback) {
var self = this;
if (self.baudrate == 0) {
var sPort = SerialPort.SerialPort;
self.sp = new sPort(self.port, {
baudrate: baudRate,
parser: SerialPort.parsers.readline("\n")
});
self.sp.on('open', function() {
//self.sendRaw("M105", function() {});
setTimeout(function() {
if (self.baudrate === 0) {
self.sp.close();
return callback(null, {success: false, baudrate: baudRate});
}
}, 3000);
console.log('baudrate test with M105 for ' + baudRate);
self.sp.on('data', function(data) {