Arduino serial port Access denied

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

}
1 Like

Sounds like you are having problem with uploading, you cant upload or open a serial port while there is another app or something that is using that com.. try uploading the node js and the arduino code separately

Thank you for the response I'm a beginner at arduino can you please explain a little bit more what you mean by "try uploading the node js and the arduino code separately" ? they are already separate

If you are using the ide and the port is connected you cant upload another code you will get access denied. try in the Arduino ide to change to some other port so you can open the port with the node js

actually I followed this Tutorial Adam Thomas and he used the same port for the nodejs server and for the Arduino in order to communicate with each other...unfortunately I don't have other port in my arduino ide... I have only port COM3

Ok I didn't make myself clear, yes you can use the same port but you cant upload to the Arduino or access the Arduino while it is connected to the ide/ if you are using something that is not the ide. try to close the ide first

1 Like

Also I saw he used the Arduino web editor you can use it but I will recommend the normal ide

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.