There are some peculiarities.
You can open port on application start and reconnect on port close or open port on each request. It defines how work with data flow. If you send request to port then answer can contain data of previous requests (more than one). You can ignore this problem (if answer is short and request interval is enough large) or send request with assign id and search answer with this id.

SerialPort.list(function (err, ports) {
    ports.forEach(function(port) {
        console.log(port.comName, port.pnpId, port.manufacturer); // or console.log(port)
    });
});

router.get('/', function(req, res){
    function sendData(code, msg) {
        res.statusCode = 500;
        res.write(msg);
        console.log(msg);   
    }

    var port = new SerialPort("COM5", {
        baudRate: 38400
    });

    port.on('open', function() {
        port.write(Buffer.from('status1', 'ascii'), function(err) {
            if (err) 
                return sendData(500, err.message);

            console.log('message written');
        });
    });

    var buffer = '';
    port.on('data', function(chunk) {
        buffer += chunk;
        var answers = buffer.split(/\r?\n/); \\ Split data by new line character or smth-else
        buffer = answers.pop(); \\ Store unfinished data

        if (answer.length > 0) 
            sendData(200, answer[0]);
    });

    port.on('error', function(err) {
        sendData(500, err.message);
    });
});

module.exports = router;
Answer from Aikon Mogwai on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 71550265 › read-serial-port-data-using-node-js
node.js - Read Serial port data using node js - Stack Overflow
const http = require('http'); const hostname = 'localhost'; const { SerialPort } = require('serialport') const { ReadlineParser } = require('@serialport/parser-readline') const { io } = require('socket.io'); let express = require('express') const serialPort = new SerialPort({ path: 'COM4', baudRate: 9600 , }) const parser = serialPort.pipe(new ReadlineParser({ delimiter: '\r\n' })) let app = express(); var port = 8080; const server = http.createServer(app); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); }); app.get('/get_data', function(req, res) { parser.on('data', function(data) { res.json({'weight': data}); }); }); When i am try to get data i got ERR_HTTP_HEADERS_SENT: Cannot set headers after they are sent to the client I want serial port data when requested from localhost:8080/get_data anyone can help ?
🌐
NYU ITP
itp.nyu.edu › physcomp › labs › labs-serial-communication › lab-serial-communication-with-node-js
Lab: Serial Communication with Node.js – ITP Physical Computing
Then invoke it as follows, replacing ... called /dev/cu.usbmodem1411, then you’d type: ... When the port opens, you’ll see the message from the showPortOpen() function, then the output from the Arduino....
🌐
Stack Overflow
stackoverflow.com › questions › 55414645 › nodejs-read-data-from-com-serial-port
node.js - NodeJs: Read data from COM serial port - Stack Overflow
I am facing an issue when I am trying to read data from the device. Please find my code below: var port = new SerialPort("COM5", { baudRate: 38400 }); port.on('data', function(chunk) { console.log(chunk); }); port.on('error', function(err) { sendData(500, err.message); });
🌐
GitHub
github.com › onaralili › node-serialport-reader
GitHub - onaralili/node-serialport-reader: Node.js app for reading data from serial-port via user interactive interface 🔌
A node.js app. reads data from serial-port. For interaction, app will read connected devices and ask you to choose one of them to connect.
Author   onaralili
🌐
GitHub
github.com › heaversm › serial-reader
GitHub - heaversm/serial-reader: Use nodejs to read serial data from the USB Port of your computer and send it to a front end web application using websockets · GitHub
run node serial-ws.js from this directory in terminal to begin listening for serial data. Make sure the name in the portName variable matches your port name from listPorts.js. If your serial device uses a delimiter between messages, make sure ...
Starred by 22 users
Forked by 9 users
Languages   JavaScript 68.0% | HTML 32.0%
🌐
Gaelbillon
dev.gaelbillon.com › a-node-js-module-to-read-serial-port-data
A Node.js module to read serial port data – Gaël Billon
November 22, 2021 - The following lines in App.js are configuring and initalizing the websocket connection : var io = require('socket.io').listen(server); io.sockets.on('connection', socket); var serialport = require("serialport"); var SerialPort = serialport.SerialPort; var sp = new SerialPort("/dev/tty.usbserial-A6023L0J", { parser: serialport.parsers.readline("n"), baudrate: 57600 }); sp.on("open", function() { console.log('open'); sp.on('data', function(data) { console.log('data received: ' + data); }); });
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 41026744 › read-serial-port-after-writing-using-node-js
Read Serial Port After Writing using Node.js - Stack Overflow
} }); port.write('#01VER\r', function(err) { if(err) console.log('Write error') else { // HOW TO READ RESPONSE FROM DEVICE? } }); }); ... I think this is a better way to do it. By function calling, first create your function of reading and writing: var serialport = require("serialport"); var SerialPort = serialport.SerialPort; var sp = new SerialPort("/dev/ttyACM0", { baudrate: 9600, parser: serialport.parsers.readline("\n") }); function write() //for writing { sp.on('data', function (data) { sp.write("Write your data here"); }); } function read () // for reading { sp.on('data', function(data) { console.log(data); }); } sp.on('open', function() { // execute your functions write(); read(); });
🌐
Serialport
serialport.io › serialport usage
SerialPort Usage | Node SerialPort
November 8, 2024 - Get updates about new data arriving through the serial port as follows: // Read data that is available but keep the stream in "paused mode" port.on('readable', function () { console.log('Data:', port.read()) }) // Switches the port into "flowing mode" port.on('data', function (data) { ...
🌐
npm
npmjs.com › package › serialport
serialport - npm
@serialport/stream our traditional Node.js Stream interface · Parsers are used to take raw binary data and transform them into usable messages.
      » npm install serialport
    
Published   Dec 24, 2024
Version   13.0.0
🌐
Serialport
serialport.io › 📦 stream
📦 stream | Node SerialPort
November 8, 2024 - Data may not yet be drained to the underlying port. Returns false if the stream wishes for the calling code to wait for the drain event to be emitted before continuing to write additional data; otherwise true. serialport.read(size?: number): string|Buffer|null
🌐
Medium
medium.com › hackernoon › arduino-serial-data-796c4f7d27ce
Read Arduino Serial Monitor Using NodeJS | by Madhav Bahl | HackerNoon.com | Medium
April 23, 2019 - Read Arduino Serial Monitor Using NodeJS This is a short tutorial article on how to read the serial port values from arduino to in NodeJS. Motivation So, there was a project I was working on where I …
🌐
Medium
medium.com › @pkl9231 › serial-communication-between-node-js-and-arduno-read-and-write-data-2a712e07d337
Serial Communication between node js and Arduino (read and write data) | by Purushotam Kumar | Medium
December 11, 2021 - Reading from the arduino is where things become slightly more fuzzy. In our case, since we control the other end of the serial port, we control how the data will be sent. In order to simplify the process considerably, I strongly recommend you: ... port.write('hello from node\n', (err) => { if (err) { return console.log('Error on write: ', err.message); } console.log('message written'); });
🌐
GitHub
github.com › DeekyJay › serialport
GitHub - DeekyJay/serialport: Node.js package to access serial ports for reading and writing. Welcome your robotic JavaScript overlords. Better yet, program them! · GitHub
Data may not yet be flushed to the underlying port, no arguments. Request a number of bytes from the SerialPort. The read() method pulls some data out of the internal buffer and returns it. If no data available to be read, null is returned.
Author   DeekyJay
🌐
Stack Overflow
stackoverflow.com › questions › 70503175 › how-to-get-the-data-from-serial-port-and-store-it-in-a-variable-using-node-js
How to get the data from serial port and store it in a variable using Node js
December 28, 2021 - I have just started using serialport libraries on Nodejs, and I am not sure how to read the data and store it in a variable, so as to use that data later on to make comparisons or decisions based on it. This is the the code I have come up with till now, its very basic but this is what I could manage · const SerialPort = require("serialport"); const Readline = require("@serialport/parser-readline"); const port = new SerialPort("COM5", {baudRate: 9600 }); const parser = new Readline('/r/n'); port.pipe(parser); //setInterval(function(){ alert("Hello"); }, 3000); parser.on("data", (line) => console.log(line));
🌐
GitHub
github.com › serialport › node-serialport › issues › 1668
Not able to read the data from serial port · Issue #1668 · serialport/node-serialport
September 20, 2018 - var serialport = require('serialport'); var portName = '/dev/ttyS0'; var port = new serialport(portName, { baudRate: 115200, dataBits: 8, parity: 'none', stopBits: 1, flowControl: false }); port.on('open', onOpen); function onOpen(){ console.log('Open connections!'); } function onData(data){ console.log('on Data ' + data); } port.on('readable', function () { console.log('Read Data:', port.read()); }); var buffer = new Buffer(9); buffer[0] = 0x01; buffer[1] = 0x30; buffer[2] = 0x02; buffer[3] = 0x07; buffer[4] = 0x00; buffer[5] = 0x00; buffer[6] = 0x02; buffer[7] = 0x00; buffer[8] = 0x00; port.write(buffer, function(err) { if (err) { return console.log('Error on write: ', err.message); } console.log('message written'); }); port.on('data', onData);
Author   serialport
🌐
Thinkingonthinking
thinkingonthinking.com › serial-communication-with-nodejs
Serial communication with NodeJS
setup() { Serial.begin(9600); } int i=0; void loop() { Serial.println(i++); delay(100); // poll every 100ms } The last “delay” gives the laptop some time to process the data. Providing a delay is a good idea when you want to watch a data stream with human eyes instead of a CPU’s. To read ...