Reading data from serial port with node.js and display it in web-page in real-time

Reading data from serial port with node.js and display it in web-page in real-time



In this tutorial, we will understand the methodology of connecting a serial device to a browser for data display in HTML format. Browser will read serial device and display the output of serial device in real-time in a web-page. 

Following software/hardware will be used for this tutorial:
Operating System: Windows 7 or Windows 10


A device which is transmitting data via. RS232 serial port interface. Connect your hardware transmitting RS232 Serial data to computer. To verify, use HyperTerminal Private Edition trial version for Windows program to verify data is being received on COM port.Note down following details:

• COM port number i.e. COM1 or COM2 or …
• Baud Rate
• Data bit
• Parity
• Stop bit
• Flow control

Following are the steps for software construction:

Step 1:

Install node.js on your computer. It is a server side scripting engine which can be used to create your own web-server. Download node.js from here ( and install it on your computer.
Let us assume you have installed node.js on your computer in path c:\node.

Step 2:

Verify installation of node.js.
Open command-line terminal and go to path c:\node.

c:\node>node -v
It will display the version of Node.js

c:\node>npm -v

It will display the version of npm (Node Package Manager)
If version numbers are displayed, it indicates that Node.js has been installed correctly.

Step 3:

Install "express". It is a Node.js library for creating web-server.

Change to c:\node.

c:\node>npm install express

Once it is installed, a new directory called "node_modules" inside c:\node will be created.

Step 4:

Install socket library ""

c:\node>npm install

Step 5:

Install serial port library.

c:\node:>npm install serialport

Serialport allows you to access computer’s serial port with read and write operations.

Step 6:

Let us create a server "server.js" like this:
var BaudRate = 9600;
var ServerPort = 8080;
var DocumentPath = "c:/node";
var fs = require("fs");
var readline = require('readline');
var rl = readline.createInterface({
 input : fs.createReadStream('server_config.txt'),
 output: process.stdout,
 terminal: false

 console.log(line) // parse line
var express = require('express'); 
 io = require(''), // include package
 app = express(), // make an instance of express.js module
 //server = app.listen(8080), // start a server on the port
 server = app.listen(ServerPort), // start a server on the port
 socketServer = io(server); // create a socket server using the express server 
//initialize serial port initialization

var serialport = require('serialport'), // include the serialport package
 SerialPort = serialport.SerialPort,    // make a local instance of serial port package
 portName = process.argv[2], // retrieve the port name from the command line argument
 portConfig = {
 //baudRate: 9600,
 baudRate: BaudRate,
 // call myPort.on('data') when a newline is received:
 parser: serialport.parsers.readline('\n')
// open the serial port
var myPort = new SerialPort(portName, portConfig);
//set up server and socketServer listener functions:
//app.use(express.static('d:/sp')); // serve files from the public folder
app.use(express.static(DocumentPath)); // serve files from the public folder
app.get('/:name', serveFiles); // listener for all static file requests
socketServer.on('connection', openSocket);     // listener for websocket data

function serveFiles(request, response) {
 var fileName =; // get the file name from the request
 response.sendFile(fileName);  // send the file
 //res.sendFile('d:/sp/'+fileName , { root : __dirname});

function openSocket(socket){
 console.log('new user address: ' + socket.handshake.address);
 // send something to the web client with the data:
 socket.emit('message', 'Server listening on address : ' + socket.handshake.address);
 // this function runs if there's input from the client:
 socket.on('message', function(data) {
 myPort.write(data); // send the data to the serial device

 // this function runs if there's input from the serialport:
 myPort.on('data', function(data) {
 socket.emit('message', data); // send the data to the client

Step 7:

Run the server "server.js". Replace "COM2" with your port name.

c:\node>node server.js "COM2"

Step 8:

Let us create a client program "c:\node\client.js" which will listen to "server.js" like this:

var socket = io();   
// instance. 
//It is used to connect to server.js on the port with socket lib

function readData (data) {

//read data and call function when data arrival event happens 
socket.on('message', readData); 

Step 9:

Let us create a file "c:\node\index.html" which will be served by "server.js"

<script src=""></script>
<script src=""></script>
<script src=""></script>
<script type="text/javascript" src="client.js"></script>
<div id="data_window">

Step 10:

Install XAMPP server on computer so that PHP scripts can be executed. Start Apache and MySql server processes. In order to verify that php script can be executed, create a "testphp.php" file in "c:\xampp\htdocs" folder like this:

echo phpinfo();

Execute it in browser like:

The list of modules and variables of PHP scripting engine will be displayed in the browser.

Step 11 :

Create a file "data_window.php" in "c:\xampp\htdocs" folder.
<!-- data_window.php -->
<script src=""></script>
<script src=""></script>
<script src=""></script>
<title>Data Window</title>
<iframe src="http://localhost:8080" height="100" width="200"></iframe>

Open it in browser as "http:\\localhost\data_window.php"

The data coming from RS232 serial port will be visible in the browser in real-time.


  1. Hi,

    I tried above setup and while running the server I'm getting below error,

    I:\Ramesh\Official\node>node server.js "8080"
    throw er; // Unhandled 'error' event

    Error: Opening 8080: File not found
    at Error (native)

    Could you please let me know on the resolution.

  2. Hi,

    Please check if port 8080 is already occupied or bound by any other application or task.

    If all does not work, try to configure your server on different port like 8081.

    Please feel free to contact me, if you have any queries.


  3. hi
    There is warn during installation on library and Error during serialport,
    can i show reading output on JSP page?
    Please Help

  4. Dear Keval,

    Thank you for contacting.

    There may be issue with Python installation and C++ compiler. Please check following thread on stack overflow to get the solution:

    Yes, you can show output in JSP page.
    Just try to embed the URL http://localhost:8080 using iframe into your JSP page.



  5. very useful information and please keep updating us, .....

  6. Hi Vamshi,

    Thank you for appreciating the post.

    The out of box salesman

  7. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Mobile Marketing Service
    Mobile Marketing Companies

    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer Node js Training in Chennai . learn from or Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

    2. Dear IT,

      Thanks for appreciating my post.


  8. Dear Jessica,

    You are welcome. Thanks for appreciating my post.

    The out of box salesman

  9. Thank You for this information. But when i applied step 7 this error message has been shown:
    Syntax error: Unexpected token )
    at create script (vm.js:56:10)
    at object.runInthisContext (vm.js:97:10)
    at module._compile (module.js:542:28)
    at object.module.extensions..js(module.js:579:10)
    at module.load(module.js:487:32)
    at tryModuleLoad(module.js:438:3)
    at module.runmain(module.js:604:10)
    at run(bootstrap_node.js:389:7)
    at startup(bootstrap_node.js:149:9)

    how can i fix this? can you help me?

  10. Hi,

    You can find more information here:


  11. Hi,

    Also, please check all paths of folders and } and ) with matching one.
    Hoping a good luck.


  12. SyntaxError: Unexpected token )
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)

    i got this error when i run this script
    node server.js "COM5"

  13. Dear Sir,

    Please try to match opening ( and { with closed one. Just for verification and precaution.

    Many people have encountered this error. You may find more information here:

    Thanking You
    The out of box salesman

  14. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information about the web design and web development.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    Hire Nodejs Developers
    Nodejs Development Company

  15. this is useful article, but in installation routine i get this error :
    Type Error: serialport.parsers.readline is not a function

    1. Dear Amin,

      Thanks for appreciating the information. Make sure you have installed "serialport" package for nodejs using "npm" command without any error or failure.

      Mr. Vemuri Prasad ( was also facing lot of problems , but ultimately we together sorted it out. You can contact him on his email for complete running code with guidance which he is able to run successfully. I guess he will be able to help you.


  16. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.
    iOS App Development Company
    Android App Development Company

  17. Thank you john for appreciating my post and source code. Please feel free to contact me, if you have any queries.


  18. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Sms marketing
    Text message marketing
    Fitness SMS

  19. Thank you Louis for appreciating my post.