main.js
function _setupSerialConnection() {
  var port = availablePorts[0];

  debug.log('Trying to connect to Davis VUE via port: ' + port);

  // Open serial port connection
  var sp = new serialPort(port, config.serialPort);

  var received = '';

  sp.on('open', function () {
    debug.log('Serial connection established, waking up device.');
    sp.write('\n', function(err) {
      if (err) {
        return constructor.emit('Error on write: ', err.message);
      }
    });


    sp.on('data', function (data) {
      if (!deviceAwake){
        if (data.toString() === '\n\r'){
          debug.log('Device is awake');
          serialPortUsed = port;
          constructor.emit('connected', port);

          sp.write('LOOP 1\n');
          return;
        }
      }
      debug.log("Received data, length:" + data.length);
      if (data.length == 100){
        // remove ack
        data = data.slice(1);
      }
      var parsedData = parsePacket(data);
      constructor.emit('data', parsedData);
      setTimeout(function () {
        sp.write('LOOP 1\n');
      }, 2000);
    });
  });

  sp.on('error', function (error) {
    constructor.emit('error', error);

    // Reject this port if we haven't found the correct port yet
    if (!serialPortUsed) {
      _tryNextSerialPort();
    }
  });

  sp.on('close', function () {
    deviceAwake = false;
    constructor.emit('close');
  });
}
function _setupSerialConnection() {
  var port = availablePorts[0];

  debug.log('Trying to connect to Davis VUE via port: ' + port);

  // Open serial port connection
  var sp = new serialPort(port, config.serialPort);

  var received = '';

  sp.on('open', function () {
    debug.log('Serial connection established, waking up device.');
    sp.write('\n', function(err) {
      if (err) {
        return constructor.emit('Error on write: ', err.message);
      }
    });


    sp.on('data', function (data) {
      if (!deviceAwake){
        if (data.toString() === '\n\r'){
          debug.log('Device is awake');
          serialPortUsed = port;
          constructor.emit('connected', port);

          sp.write('LOOP 1\n');
          return;
        }
      }
      debug.log("Received data, length:" + data.length);
      if (data.length == 100){
        // remove ack
        data = data.slice(1);
      }
      var parsedData = parsePacket(data);
      constructor.emit('data', parsedData);
      setTimeout(function () {
        sp.write('LOOP 1\n');
      }, 2000);
    });
  });

  sp.on('error', function (error) {
    constructor.emit('error', error);

    // Reject this port if we haven't found the correct port yet
    if (!serialPortUsed) {
      _tryNextSerialPort();
    }
  });

  sp.on('close', function () {
    deviceAwake = false;
    constructor.emit('close');
  });
}
function _setupSerialConnection ( ) { var port = availablePorts [ 0 ] ; debug . log ( 'Trying to connect to Davis VUE via port: ' + port ) ; // Open serial port connection var sp = new serialPort ( port , config . serialPort ) ; var received = '' ; sp . on ( 'open' , function ( ) { debug . log ( 'Serial connection established, waking up device.' ) ; sp . write ( '\n' , function ( err ) { if ( err ) { return constructor . emit ( 'Error on write: ' , err . message ) ; } } ) ; sp . on ( 'data' , function ( data ) { if ( ! deviceAwake ) { if ( data . toString ( ) === '\n\r' ) { debug . log ( 'Device is awake' ) ; serialPortUsed = port ; constructor . emit ( 'connected' , port ) ; sp . write ( 'LOOP 1\n' ) ; return ; } } debug . log ( "Received data, length:" + data . length ) ; if ( data . length == 100 ) { // remove ack data = data . slice ( 1 ) ; } var parsedData = parsePacket ( data ) ; constructor . emit ( 'data' , parsedData ) ; setTimeout ( function ( ) { sp . write ( 'LOOP 1\n' ) ; } , 2000 ) ; } ) ; } ) ; sp . on ( 'error' , function ( error ) { constructor . emit ( 'error' , error ) ; // Reject this port if we haven't found the correct port yet if ( ! serialPortUsed ) { _tryNextSerialPort ( ) ; } } ) ; sp . on ( 'close' , function ( ) { deviceAwake = false ; constructor . emit ( 'close' ) ; } ) ; }
Setup serial port connection
Setup serial port connection