简体   繁体   中英

Node SerialPort on Linux CentOS 6

I am new to node js. I have created an app using node, express, socket and serialport. I have a weighing scale connected to serialport (COM2). On windows (localhost) it is perfectly working. But when I deployed it on my Linux CentOS 6 server it is not reading any serialport.

package.json

{
  "name": "socket",
  "version": "0.0.1",
  "description": "wieghing machine serial port socket app",
  "dependencies": {
    "express": "^4.10.2",
    "socket.io": "1.2.0",
    "bindings": "1.2.1",
    "commander": "^2.9.0",
    "debug": "^2.4.5",
    "nan": "^2.4.0",
    "node-pre-gyp": "^0.6.32"
  },
  "devDependencies": {
    "serialport": "^4.0.7"
  }
}

app.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var SerialPort = require('serialport');


app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('register', function(msg){
    io.emit('register', msg);
  });
});

http.listen(8081, function(){
  console.log('listening on *:8081');
});


var port = new SerialPort("COM2", {
  baudrate: 9600
});

port.on('data', function (data) {
    io.emit('WeightMachine', data.toString());
});

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

Please do not mark my question negative I really am new to node js. Thanks in advance.

By default your code runs on localhost when not supplied the 2nd params, but when running on server it needs to be changed as. Also make sure that 8081 port is enabled on server

 http.listen(8081,"0.0.0.0", function(){
   console.log('listening on *:8081');
 });

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