views:

563

answers:

3

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.

A: 

Maybe a typo? Ensure that you are typing COM1 in all english letters.

denisenkom
I'm 100% sure "COM1" is spelled correctly.
Jon B
+1  A: 

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"

clinisbut
If I do that I get an exception stating the COM port cannot start with a \
Jon B
+1  A: 

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.

SwDevMan81
You could (in this case) omit the call to .PortName, as this gets set to "COM1" internally by default.
Andy
And also DataBits and StopBits.
SwDevMan81