tonyo
tonyo

Reputation: 11

UnauthorizedAccessException when trying to open a com port in C#

I am using an Arduino Board to write on the Serial COM port, and using a C# app to read it. But whenever I try to run the C# program, an exception (UnauthorizedAccessException) occurs when it tries to open a serial port. How do I properly open a serial port?

I have very little knowledge in C++. Most of my knowledge comes from their similarities with C++, and a lot of googling tutorials. Here is one of the codes that I copied from one of the tutorials I found:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if(!serialPort1.IsOpen)
        {
            serialPort1.PortName = "COM3";

            serialPort1.Open();

            richTextBox1.Text = "opened";
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if(serialPort1.IsOpen)
        {
            string text = richTextBox1.Text;
            serialPort1.WriteLine(text);
        }
    }
}

Upvotes: 1

Views: 4587

Answers (1)

sa_leinad
sa_leinad

Reputation: 349

What is happening is that you have COM3 already open in another application (See MSDN). When you click on button1 the program can see that the COM port is open (in another application) and tries to write to it, even though your program does not have access to it.

Firstly you will need to create an instance of the serial port (which I suspect you have already done since you would receive different error). I create my instance when Form1 is created:

    public Form1()
    {
        InitializeComponent();

        // Create an instance of a serial port
        serialPort1 = new SerialPort();
    }

In the following I have renamed button2 to openPortButton:

    private void openPortButton_Click(object sender, EventArgs e)
    {

You could make your program aggressive by closing the serial port if it is already open:

        // Check if the serial port is already open. If it is close it down first.
        if (serialPort1.IsOpen == true)
        {
            serialPort1.Close();
        } 

You can also catch any exceptions when opening the serial port by wrapping it inside a try-catch statement:

        if(!serialPort1.IsOpen)
        {
            serialPort1.PortName = "COM3";

            try
            {
                serialPort1.Open();
            }
            catch
            {
                // Add exception handling here
            }
        }

Then test that the serial port is actually open:

        // Test connection
        if (serialPort1.IsOpen == true)
        {
            richTextBox1.Text = "opened";
            commsEstablished = true;
        }
        else
        {
            richTextBox1.Text = "not successful";
            commsEstablished = false;
        }
    }

Where commsEstablished is a private bool in Form1.

Now when the send button is pressed test the commsEstablished variable: (I have also renamed button1 to sendButton)

    private void sendButton_Click(object sender, EventArgs e)
    {
        if(commsEstablished && serialPort1.IsOpen)
        {
            string text = richTextBox1.Text;
            try
            {
                serialPort1.WriteLine(text);
            }
            catch
            {
                // Add exception handling here
            }
        }
    }

Upvotes: 2

Related Questions