English 中文(简体)
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 DeviceSerial0.


Workaround

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

问题回答

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.

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"

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.

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!

In my case, the program I built in c# worked fine on my PC, but when I ran it on other computers it won t initialize the serial port. I found out, the other pcs didn t have access to a database and that was affecting my program. Weird c# behavior.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签