Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将Arduino的多个串行端口与NodeJ连接起来_Javascript_Node.js_Sockets_Arduino_Serial Port - Fatal编程技术网

Javascript 将Arduino的多个串行端口与NodeJ连接起来

Javascript 将Arduino的多个串行端口与NodeJ连接起来,javascript,node.js,sockets,arduino,serial-port,Javascript,Node.js,Sockets,Arduino,Serial Port,我在使用串行端口和节点连接4个arduinos时遇到问题。 当我连接所有端口时,只有一个端口在实际工作并收集数据消息,而所有其他端口都被忽略 如果我单独声明串行端口,它们都可以正常工作,因此问题不在于Arduino代码 以下是我如何声明所有串行端口: // Load HTTP module to create server, handle requests and send back static files (html, css, js) const http = require('http'

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

如果我单独声明串行端口,它们都可以正常工作,因此问题不在于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);
  }
});

const socket=io();
//当套接字连接到服务器时登录浏览器控制台
socket.on('问候语',(消息)=>{
控制台日志(消息);
});
//加勒比花瓣
socket.on('CoinvChange',(消息)=>{
console.log('coinv');
如果(消息7和消息12){
getFlowerObject(“花瓣”,3);
}
});
//塞帕利加勒比酒店
socket.on('importchange',(消息)=>{
console.log('impo');
如果(消息3和消息8){
getFlowerObject(“Sepoli”,3);
}
});

谢谢你的帮助

好的,在第二个片段中,您只需调用
serial.on('data',forwardMessage)
,而
serial
仅指第一个。 如果要与其他方法交互,则必须在
serial1
serial2
serial3
上调用相同的方法,而这些方法是您从未使用过的

作为旁注,可以使用
const SerialPort=require('SerialPort')仅在(第一个代码段的)开头,然后您可以执行以下操作

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