Search code examples
c#serial-portportmodem

Trouble Accessing com port in c#


I'm trying to read and write to a usb modem that is using the com port 3 with this code.

 SerialPort sp = new SerialPort();
 sp.PortName = "COM3";
 //sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
 sp.Open();
 sp.Write("AT<CR>");
 byte[] bytes = new byte[sp.BytesToRead];
 sp.Read(bytes, 0, sp.BytesToRead);

 textBox1.Text = Encoding.UTF8.GetString(bytes);

But I get this error :

Access to the port 'COM3' is denied.

Someone have an idea ... Thanks


Solution

  • You can only open the port once. Maybe you're accidentally opening it more than once within your code or another program is using it?