【问题标题】:Connect multiple Serialports from Arduino with NodeJs使用 NodeJs 连接来自 Arduino 的多个串行端口
【发布时间】:2020-12-09 10:37:54
【问题描述】:

我在使用串行端口和 NodeJ 连接 4 个 arduino 时遇到问题。 当我连接所有端口时,只有一个实际工作并收集数据消息,而所有其他端口都被忽略。

如果我单独声明串行端口,它们都可以正常工作,所以问题不在于 Arduino 代码。

这是我声明所有串行端口的方式:

// 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;  

这就是我读取 arduino 数据的方式

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

最后这就是我使用消息的方式

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

感谢您的帮助!

【问题讨论】:

    标签: javascript node.js sockets arduino serial-port


    【解决方案1】:

    好吧,在第二个片段中,您只调用serial.on('data', forwardMessage);,而serial 只是指第一个。 如果你想与其他人交互,你必须在 serial1serial2serial3 上调用相同的方法,而不是使用它们。

    作为旁注,在开头(第一个sn-p)使用const SerialPort = require('serialport');就足够了,然后你可以这样做

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多