首页 > 解决方案 > Connect multiple Serialports from Arduino with NodeJs

问题描述

I'm having trouble connecting 4 arduinos using serialports and NodeJs. When I connect all the ports only one is actually working and collecting the data message while all the others are ignored.

If I declare the serial ports separately they all work fine so the problem is not the Arduino code.

Here's how i declare all the serialports:

// Load HTTP module to create server, handle requests and send back static files (html, css, js)
const http = require('http');
// Load file system module to load files from computer
const fs = require('fs');
// Load path module to read paths from urls
const path = require('path');

// Load serialport module to communicate with arduino
const SerialPort = require('serialport');
// Open up connection with Arduino board
const serial = new SerialPort('/dev/cu.usbserial-1411140', {
        baudRate: 115200
    }, function() {
        console.log('1411140 ready');
})

const SerialPort1 = require('serialport');
const serial1 = new SerialPort1('/dev/cu.usbserial-141120', {
        baudRate: 115200
    }, function() {
        console.log('141120 ready');
})

const SerialPort2 = require('serialport');
const serial2 = new SerialPort2('/dev/cu.usbmodem-1411301', {
        baudRate: 115200
    }, function() {
        console.log('1411301 ready');
})

const SerialPort3 = require('serialport');
const serial3 = new SerialPort3('/dev/cu.usbserial-1411130', {
        baudRate: 115200
    }, function() {
        console.log('1411130 ready');
})

// Define port on which the webpage will be served from
const port = 8080;  

This is how i read the arduino data

const io = require('socket.io')(server);
// do stuff when a client connects
io.on('connection', (socket) => {
    console.log('a new client connected');
    // let the client know that it's connected
    socket.emit('greetings', 'You are now connected to the server through Socket IO');

    // when receiving data from Arduino, tell the client what to do accordingly
    serial.on('data', forwardMessage);


    // log if an user disconnects
    socket.on('disconnect', () => {
        console.log('client disconnected');
        // remove listener from Node EventEmitter
        serial.removeListener('data', forwardMessage);
    });

    function forwardMessage(data) {
        let message = data.toString().replace(/\n*/, '');
        //riceve messaggi dal device corrispondente. Attenzione al nome messo anche sul codice Arduino
        if (message.includes('Coinv')) {
            socket.emit('CoinvChange', message.substring(7));
        }
        if (message.includes('Impor')) {
            socket.emit('ImporChange', message.substring(7));
        }
        if (message.includes('Piace')) {
            socket.emit('PiaceChange', message.substring(7));
        }
        if (message.includes('Cresc')) {
            socket.emit('CrescChange', message.substring(7));
        }
        if (message.includes('Press')) {
            socket.emit('PressChange', message.substring(7));
        }
    }
});

And finally this is how i use the message

const socket = io();

// log on browser console when socket is connected to server
socket.on('greetings', (message) => {
    console.log(message);
});

// Caricamento Petali
socket.on('CoinvChange', (message) => {
  console.log('coinv');

  if(message<=6){
    getFlowerObject ("petali", 1);
  }

  if(message>7 && message <=9) {
    getFlowerObject ("petali", 2);
  }

  if(message>12) {
    getFlowerObject ("petali", 3);
  }
});

// Caricamento Sepali
socket.on('ImporChange', (message) => {
  console.log('Impor');

  if(message<=2){
    getFlowerObject ("sepali", 1);
  }

  if(message>3 && message <=7) {
    getFlowerObject ("sepali", 2);
  }

  if(message>8) {
    getFlowerObject ("sepali", 3);
  }
});

Thank you for your help!

标签: javascriptnode.jssocketsarduinoserial-port

解决方案


Well, in the second snipped, you call just serial.on('data', forwardMessage);, and serial just refers to the firt one. If you want to interact with the other ones, you have to call the same method also on serial1, serial2 and serial3, which you never use, instead.

As a sidenote, it's enugh to use const SerialPort = require('serialport'); just at the beginning (of the first snippet), then you can do

const serial1 = new SerialPort(...)
...
const serial2 = new SerialPort(...)
...

推荐阅读