Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to open serial port in .NET

I am trying to open COM1, but get a strange error every time I call SerialPort.Open().

The error is:

The given port name does not start with COM/com or does not resolve to a valid serial port. Parameter name: portName

The code is very simple:

SerialPort port = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
port.Handshake = Handshake.RequestToSend;
port.Open();

If I call SerialPort.GetPortNames(), it returns one port called "COM1".

I've verified that I have a "COM1" on my computer, and I'm not using it in any other applications. When I run the code on another computer, it works. My system is running Windows Vista. The version of .NET is 2.0.

Is there a security setting I have to change somewhere? I'm logged in as an admin, and have UAC turned off.


More info

I used Process Explorer and confirmed that nothing is using \Device\Serial0.


Workaround

I installed a USB-Serial adaptor (COM3), and it works fine. Go figure. There must be a problem with COM1.

like image 332
Jon B Avatar asked Nov 18 '09 21:11

Jon B


People also ask

Can not open the serial COM port?

The error "Could not open serial port", means that you do not have the correct comm port selected in Projects. Go to "Options" -> "Project Options" -> on the Communications Tab, choose the correct "Serial Port" under "Serial Options".

Why is access to COM port denied?

This error indicates "Access is denied". You may see this error when when attempting to use an already open COM port, it's been used by other program. You could try close other software that may be using the port in the Task manager, and try again.


2 Answers

I had this problem too. It turned out that I had a printer set to use the COM port I was trying to open. Once I changed the printer to use another port the port opened just fine.

like image 175
NoonKnight Avatar answered Oct 02 '22 14:10

NoonKnight


Try to use the notation \.\COMX instead of just COMX. Ensure you escape the characters: "\\.\COM1"

Edit:Wops, SO escapes my \ so it should be like this (ommit spaces): "\ \ \ \ . \ \ COM1"

like image 32
Pherrymason Avatar answered Oct 02 '22 15:10

Pherrymason