8
votes

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.

4
I know sometime the GetPortNames() gives wonky characters out of it.Daniel A. White
Does hyperterminal work?SwDevMan81

4 Answers

5
votes

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.

1
votes

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"

1
votes

You could try setting up the properties of the port, rather than using the constructor.

mPort = new System.IO.Ports.SerialPort();
if(mPort.IsOpen)
{
   mPort.Close();
}
mPort.PortName = "COM1";
mPort.BaudRate = 19200;
mPort.Parity = Parity.None;
mPort.DataBits = 8;
mPort.StopBits = StopBits.One;
mPort.Handshake = Handshake.RequestToSend; // Handshake.None;
mPort.Open();

I've also run into problems with Serial comm's and Microsoft Active Sync. I'm not sure if you have this running or not, but it might be worth a shot to kill it (process name in the Task Manager is wcescomm.exe). Hope that helps.

You could also try and use Marshal.GetLastWin32Error() to see if something low level is causing a problem? I'm not sure if that will give you more information.

1
votes

May be some application running in the background is probably opened your port(see this question).

Download Process Explorer and use the "Find Handle or DLL" on the Find menu to find the process with the com port open. In my case spoolsv.exe always using COM1, COM3 port. so i used another com port (COM2) good luck!