Friday 5 May 2017

Node server cannot open specified port

I am trying to write a program that allows me to communicate with a device using the serial port. To do so, I am using the following code :

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

var SerialPort = require("serialport");
var port = new SerialPort("/dev/ttyS1");

app.use(express.static(__dirname+"/../.."));
app.use(express.static(__dirname+"/../../../bower_components/"));



io.on('connection', function(socket) {
console.log('new connection');

 socket.on('message', function (message) {
//console.log('Position : ' + message); //envoi port ... à la place
//port.on('open', function() {
  port.write(message, function(err) {
    if (err) {
      return console.log('Error on write: ', err.message);
    }

  });
//});

});
});

server.listen(8080, function() {
  console.log('server up and running at 8080 port');
});

However, the issue here is that the port never seems to open, but the value we are supposed to send is still printed in the console,and the /dev/tty (which represents the console) is opened, but the /dev/tty/ACM0 does not get opened, as you can see in this screenshot :

Console output

According to the documentation, the port is supposed to open automatically when we instantiate the SerialPort object.

So, are there any issues in my code or is there any better way to do this ?



via tarzan212

No comments:

Post a Comment