Problems using SerialPort to communicate between Node.js and Arduino
I’m trying to build a Node.js server that sends Data to an Arduino and the Arduino send this same Data back, I’m able to access any data from the Arduino to my computer, but not send data to the Arduino, I’m using SerialPort
This is my code in the Arduino IDE. My Arduino should be receiving ‘1’, which is 49 in ASCII. Then, if it actually receives 49, it will send the data back.
char incomingByte;
void setup() {
Serial.begin(9600);
}
void loop() {
incomingByte = Serial.read();
if (incomingByte == 49) {
Serial.write(incomingByte);
}
}
In my Node.js file, I’m trying to send “1” and I’m logging anything that I receive.
const { SerialPort } = require('serialport');
const port = new SerialPort('/dev/cu.usbmodem14201', {
baudRate: 9600,
dataBits: 8,
stopBits: 1,
parity: 'none'
});
port.on('open', () => {
port.write('1');
});
port.on('data', (data) => {
console.log(data.toString());
});
port.on('error', (err) => {
console.error('Error:', err);
});
Read more here: Source link
