Reputation:
I am trying to write multiple data using the same port but I can not do it.
But node-serialport
provides multiple data read function at the same time.
How I write multiple data at the same time
This way I tryout
- index.js-
const SerialPort = require('serialport');
const Readline = SerialPort.parsers.Readline;
const port = new SerialPort('COM3', 9600);
const parser = port.pipe(new Readline({delimiter: '\r\n'}));
function sendToSerial(datax, datay) {
console.log("sending to serial: " + datax, datay);
port.write(datax);
port.write(datax);
}
parser.on('data', (data) => {
const responseArray = JSON.parse(data); // Incoming Data from arduino
sendToSerial1(datax, datay); // Send data to arduino
});
Arduino code
-arduino.ino-
void setup() {
Serial.begin(9600);
delay(1000);
}
void loop() {
if(Serial.available() > 0){
int datax = Serial.parseInt();
int datay = Serial.parseInt();
}
Serial.print(F("{\"temperature1\": "));
Serial.print(t1);
Serial.print(F(", \"moisture1\": "));
Serial.print(h1);
Serial.print(F(", \"temperature2\": "));
Serial.print(t2);
Serial.print(F(", \"moisture2\": "));
Serial.print(h2);
Serial.println(F("}"));
delay(1000);
}
How to solve this?
Upvotes: 1
Views: 893
Reputation: 11
I'm not sure its the solution, but I'm guessing there is a bug in your code here....
port.write(datax);
port.write(datax);
Guessing the last argument is meant to be datay?
Upvotes: 1