简体   繁体   中英

Server client communication Node.js

I have written a server program server.js as below

var SerialPort = require("serialport").SerialPort
var io = require('socket.io').listen(80);
var serialPort = new SerialPort("COM4", {
baudrate: 9600
 });
 serialPort.on("open", function () {
  console.log('open');
   serialPort.on('data', function(data) {
console.log('data received: ' + data);
});  
  serialPort.write("ls\n", function(err, results) {
  console.log('err ' + err);
   console.log('results ' + results);
//io.sockets.emit('message', 'data');
     }); 
     }); 
    io.sockets.on('connection', function (socket) {
// If socket.io receives message from the client browser then 
// this call back will be executed.
socket.on('message', function (msg) {
    console.log(msg);
});
// If a web browser disconnects from Socket.IO then this callback is called.
socket.on('disconnect', function () {
    console.log('disconnected');
});
});

  var cleanData = ''; // this stores the clean data
  var readData = '';  // this stores the buffer
  serialPort.on('data', function (data) { // call back when data is received
  readData += data.toString(); // append data to buffer
  // if the letters 'A' and 'B' are found on the buffer then isolate what's in the middle
  // as clean data. Then clear the buffer. 
  if (readData.indexOf('B') >= 0 && readData.indexOf('A') >= 0) {
    cleanData = readData.substring(readData.indexOf('A') + 1, readData.indexOf('B'));
    readData = '';
    io.sockets.emit('message', cleanData);

     }
 });

The client side program is (index.html)

<html>
    </head>
        <link type="text/css" href="/css/smoothness/jquery-ui-1.8.20.custom.css" rel="Stylesheet" />

        <script type="text/javascript" src="//localhost:8000/socket.io/socket.io.js"></script>
        <script type="text/javascript" src="/js/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="/js/jquery-ui-1.8.20.custom.min.js"></script>
            <script>

            var socket = io.connect('http://localhost:8000');
            socket.on('connect', function () {
                socket.on('message', function (msg) {
                    // Convert value to integer
                    var val = ((parseInt(msg) / 1023)*100);

                    // Put sensor value to the 'sensor_value' span
                    $('#sensor_value').html(val);

                    });
            });
        });
        </script>
    </head>
    <body>
        <div role="main">
            Potentiometer Value: <span id="sensor_value"></span><br/>
        </div>
    </body>
</html>

I am using WAMP as web server The server console is giving me the required output. But nothing is coming in the webpage index.html

Here you can find a tutorial showing exactly what you're trying to do: create a Node.js server and forwarding data from serial port to an html page via Websockets.

It was very useful to me when doing the same, sure it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM