Page 1 of 1

nodejs npm-serialport, communications help

Posted: Mon Mar 25, 2019 8:41 pm
by chadk5utc
First thanks for taking the the time to look this mess over.
I am very new to programming so much to learn and I jumped off both feet into deep water as it were. Currently having trouble finding the correct way to send data(X, Y) to serialport. For what its worth I have spent unknown hours trying prior to this posting so thanks again for any custructive input.
joystick output console.log:

Code: Select all

listening on *:3000
A user connected
temp 126.7088
X:0 Y: 0
X:94 Y: 97
X:117 Y: 124
X:129 Y: 138
X:139 Y: 148
X:159 Y: 166
X:176 Y: 179
X:184 Y: 187
X:170 Y: 194
X:118 Y: 196
X:90 Y: 178
X:-108 Y: 188
X:-148 Y: 185
X:-157 Y: 102
X:-150 Y: -112
X:-117 Y: -164
X:-99 Y: -169
X:-99 Y: -171
X:0 Y: 0
My current code is as follows:

Code: Select all

// Server setup required
var app = require('express')();
var http = require('http').Server(app);
var io = require('')(http);
var exec = require('child_process').exec, child;
var port = process.env.PORT || 3000;

//Serial setup port
var SerialPort = require('serialport');
var serialPort = SerialPort.serialPort;
var sp = new SerialPort('/dev/ttyACM0', { baudRate: 115200, autoOpen: false}) (err) {
  if (err) {
    return console.log('Error opening port: ', err.message)


var Gpio = require('pigpio').Gpio,           // Let there be lights
  LED = new Gpio(22, {mode: Gpio.OUTPUT});   //

app.get('/', function(req, res){
  console.log('HTML sent to client');

child = exec("sudo bash", function(error, stdout, stderr){});

//Whenever someone connects this gets executed
io.on('connection', function(socket){
  console.log('A user connected');

  socket.on('pos', function (msx, msy) {
   console.log('X:' + msx + ' Y: ' + msy);
   io.emit('posBack', msx, msy);
      msx = Math.min(Math.max(parseInt(msx), -2047), 2047);
      msy = Math.min(Math.max(parseInt(msy), -2047), 2047);

// serialWrite
        sp.write(msx, msy);


  socket.on('light', function(toggle) {

  socket.on('OTS1', function(toggle) {
    var numPics = 0;
    console.log('Taking a picture..');
    //Count jpg files in directory to prevent overwriting
    child = exec("find -type f -name '*.jpg' | wc -l", function(error, stdout, stderr){
      numPics = parseInt(stdout)+1;
      // Turn off streamer, take photo, restart streamer
      var command = 'sudo killall mjpg_streamer ; raspistill -o OTS1' + numPics + '.jpg -n && sudo bash';
      console.log("command: ", command);
        child = exec(command, function(error, stdout, stderr){
        io.emit('OTS1', 1);


  socket.on('power', function(toggle) {
    child = exec("sudo poweroff");

  //Whenever someone disconnects this piece of code is executed
  socket.on('disconnect', function () {
    console.log('A user disconnected');

  setInterval(function(){ // send temperature every 5 sec
    child = exec("cat /sys/class/thermal/thermal_zone0/temp", function(error, stdout, stderr){
      if(error !== null){
               console.log('exec error: ' + error);
      } else {
         var temp = parseFloat(stdout)/1000 * 1.8 + 32;
         io.emit('temp', temp);
         console.log('temp', temp);

  }, 5000);

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


Re: nodejs npm-serialport, communications help

Posted: Tue Mar 26, 2019 8:03 am
by Heater
The first thing to learn about programming is what language you are using. This is the "Java" forum subsection. You are using Javascript. Which is a very different thing.

Second thing to learn is that when you to ask about an error or other program failure specify exactly what it is. What error do you get? What does it not do that it should? Or vice versa.

I notice you are sending two parameters to the serial port write() method:

sp.write(msx, msy);

A Cursory glance of the serial port documentation shows that write() only takes one parameter.

Why not write a string to the serial port, as shwon in the documentation:

Code: Select all

sp.write('X:' + msx + ' Y: ' + msy', function(err) {
  if (err) {
    return console.log('Error on serial port write: ', err.message)
  console.log('message written to serial port')
The docs:

Re: nodejs npm-serialport, communications help

Posted: Tue Mar 26, 2019 12:48 pm
by chadk5utc
Good morning Thanks for your response and inputs on this. Going to be testing this morning

Re: nodejs npm-serialport, communications help

Posted: Tue Mar 26, 2019 12:55 pm
by Heater
Oops, sorry, I had two many quotes in the example a gave, should be:

sp.write('X:' + msx + ' Y: ' + msy', function(err) {

Re: nodejs npm-serialport, communications help

Posted: Tue Mar 26, 2019 2:14 pm
by chadk5utc
Thanks for all the input, I truly appreciate it. The app runs without any errors however I get no response from motor controller so Im back to the manuals to see what I've missed.