Linux CentOS 6上的Node SerialPort

时间:2017-01-31 09:07:28

标签: javascript node.js linux centos6 node-serialport

我是节点js的新手。我使用node,express,socket和serialport创建了一个app。我有一个连接到serialport(COM2)的秤。在Windows(localhost)上它完全正常工作。但是当我在Linux CentOS 6服务器上部署它时,它并没有读取任何串口。

的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);
  });
});

请不要标记我的问题否定我真的是节点js的新手。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

默认情况下,当没有提供第二个参数时,您的代码在localhost上运行,但在服务器上运行时,需要将其更改为。还要确保在服务器上启用了8081端口

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