Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why SerialPort.DataReceived event causes error "Access to the path COM4 is denied

I am working on a C# program to communicate with Arduino through serial port. I used this code to receive data and write it in console:

using System;
using System.IO.Ports;


while (true)
{
    SerialPort serialPort = new SerialPort();
    serialPort.PortName = "COM4";
    serialPort.BaudRate = 9600; 
    serialPort.Parity = Parity.None;
    serialPort.DataBits = 8;
    serialPort.StopBits = StopBits.One;
    serialPort.Handshake = Handshake.None;
    serialPort.DataReceived += SerialPortDataReceived;
    try
    {
        serialPort.Open();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: " + ex.Message);
    }

    void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string data = sp.ReadExisting();
        Console.WriteLine("Received data: " + data);
    }

}

But this error happened:
Access to the path ‘COM4’ is denied.

I checked again the settings (such as port name etc.). And the settings were correct.
Also, no other application was using this port.
After that I tried this code in similar situation and it worked fine:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using System.IO.Ports;
SerialPort serialPort = new SerialPort();
serialPort.PortName = "COM4";
serialPort.BaudRate = 9600;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Handshake = Handshake.None;
try
{
    serialPort.Open();
}
catch (Exception ex)
{

    Console.WriteLine("Error: " + ex.Message);
}

while (true)
{

    string a = serialPort.ReadExisting();
    Console.WriteLine(a);
    Thread.Sleep(200);

}

Why does the first code not work and is there a way I can use the first method?

>Solution :

Well, look at your first code…

You use a loop, trying to open the serial port COM4 again, and again, and again, and again, and again, and again, and again, and again, and again, and again, and again…

But of course that doesn’t happen because you can’t open the same COM port more than once unless you close it before opening it again a second time. What happens is that if you try to open a COM port again when it is already open is the operating system saying "Na-Ah!" to you in the form of the exception you observed.

Therefore the answer to one of your questions: "is there a way I can use the first method?" would basically be: What’s the point of trying to repeatedly open the same COM port? Just open it once and be happy…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading