I really need your help I am trying to make a server with Node.js to send Value "1" for exemple to Arduino via Serial Port "COM3" so when I run my node.js code it throw no error and it seems that my server is working but after that when I try to upload my arduino code in order to see in the serial monitor if I'am recieving Data or not from my node.js server I am getting errorCould not open COM3, the port doesn't exist
and I notice that if I run only the arduino code it throw no error and the same for nodejs. server but if I run both node.js server first and than arduino I am getting an error that the port doesn't exist...it seems that the node.js server is using the port "COM3" and then if I try to select that Port "COM3" in arduino and then run the Arduino code it throw an error...but it is supposed that the node.js server and my arduino code use the same Port in order to communicate with each other ? so how I can read recieved Data from nodejs in my arduino ?
here is my nodejs code:
var http = require('http');
var fs = require('fs');
var index = fs.readFileSync( 'index.html');
const { SerialPort } = require('serialport')
const { ReadlineParser } = require('@serialport/parser-readline')
const parser = new ReadlineParser({ delimiter: '\r\n' });
var port = new SerialPort(
{ path: 'COM3', baudRate: 9600 , dataBits: 8, parity: 'none', stopBits: 1, autoOpen: true}
);
port.pipe(parser);
setTimeout(function(){
port.write("1"); // here i am trying to send "1" to arduino
console.log("i send 1 to ardouino");
}, 3000);
and here my arduino code :
void setup() {
Serial.begin(9600);
}
void loop() {
// CHeck to see if Serial data is being received
if (Serial.available() > 0) {
// Create a new string variable to receive Serial data
String receivedString = "";
// Loop through received data and append to the receivedString variable
while (Serial.available() > 0) {
receivedString += char(Serial.read ());
}
// Print received Serial data
Serial.println(receivedString);
}
}