How to get synchronous SerialPort Read in NodeJS
<p>Actually working on an automaton project (based on Arduino &lt;-> Serial &lt;-> Raspberry), I seem to be kind of stuck on the serial interface.</p>

<p>Basically, I'm using Express to read/write on some parameter on the arduino by URL in that kind of syntax (<a href="http://localhost:3000/parameter/6" rel="nofollow">http://localhost:3000/parameter/6</a> or <a href="http://localhost:3000/parameter/6/set?value=10" rel="nofollow">http://localhost:3000/parameter/6/set?value=10</a>).</p>

<p>Since I'm willing to get a simple/short result (as an API), I decided to render a simple json object on each request, there is also <strong>no way for me to use post-rendering scripts/frameworks like <code>Socket.io</code>/<code>jQuery</code>/... for this purpose</strong> (I'd probably often call those URLs from <code>curl</code>/<code>wget</code>/[other html parsers]).</p>

<p>Now the thing is that there is no problem to write on the port, but for the reading, I'd like to <strong>wait for the buffer to be returned from <code>serialport.on('data', function(buffer) { ... })</code> before rendering the page</strong>.</p>

<p>As <code>serialport.on('data', ... )</code> does seem to be called only once per request, the only way I found for the moment is to redirect to the same page until the buffer has been read, which seem kind of a <strong>nasty way of getting things done</strong>, also it is incomplete ...</p>

<p><strong>Here's a bit of code to get an idea of the actual logic :</strong></p>

<p><strong>Library :</strong></p>

<pre><code>// -&gt; lib/serial.js - LIBRARY
var SerialPort = require("serialport");
var data = {'state': 0};


var serialPort = new SerialPort(port, {
  baudrate: 115200,
  parser: SerialPort.parsers.readline('\r\n'),
});

serialPort.on('open', function() {
  console.log('Serial port Open');
  serialPort.on('data', function(buffer) { data.buffer = buffer, data.state = 1 });
});

function readConfig(cmd, paramNb) {
  var cmd = String.fromCharCode(cmd);
  var param = String.fromCharCode(paramNb);
  if (serialPort.isOpen() == true) {
    serialPort.write(cmd + param + '\n', function(err) {
      if (err) {
        return console.log('Error on write: ', err.message);
      }
      console.log('message written');
    });
  }
  return data;
};

function writeConfig(cmd, paramNb, value) {
  var cmd = String.fromCharCode(cmd);
  var param = String.fromCharCode(paramNb);
  var value = String.fromCharCode(value);
  if (serialPort.isOpen() == true) {
    serialPort.write(cmd + param + value + '\n', function(err) {
      if (err) {
        return console.log('Error on write: ', err.message);
      }
      console.log('message written');
    });
  }
};

exports.readConfig = readConfig;
exports.writeConfig = writeConfig;
</code></pre>

<p><strong>Route :</strong></p>

<pre><code>// -&gt; parameter.js - ROUTE
var express = require('express');
var router = express.Router();
var serial = require('../../lib/serial');
var sleep = require('sleep');

/* SET Parameter. */
router.get('/:id/set', function(req, res) {
  var id = req.params.id;
  var value = req.query.value;
  serial.writeConfig('b', id, value);
  res.json({'result': value, 'id': id});
});

/* GET Parameter. */
router.get('/:id', function(req, res) {
  var id = req.params.id;
  var buffer = serial.readConfig('a', id);
  if (buffer.state != 0) {
    res.json({'result': buffer});
  }
  else {
    sleep.usleep(100000);
    res.redirect('/api/parameter/' + id);       // &lt;- Nasty Bulls**t
  }
});

module.exports = router;
</code></pre>

<p>The first idea I came up with was to find a synchronous way to read the port so I can call something like this (pseudo-code) :</p>

<pre><code>while (buffer == '') { var buffer = serial.read; }
res.json({buffer});
</code></pre>

<p>Anyway thanks everyone.</p>