I do not think that GetPortNames () or Open () is causing the problem: my bet is that it is hardware related. Have you tried your code on another machine?
Unfortunately, I do not have direct experience with your scenario, because double-digit ports have always worked for me ... But there is one thing that I would like to note: over time, I learned that it is better to be safe than to apologize, and so Thus, I have increased the use of try-catch blocks. In your case, I would do the following:
static System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); private static int defaultBaudRate = 9600, defaultDataBits = 8; static System.IO.Ports.SerialPort TryOpeningPort(string portName) { System.IO.Ports.SerialPort port = null; timer.Start(); try { port = new System.IO.Ports.SerialPort(portName, defaultBaudRate, System.IO.Ports.Parity.None, defaultDataBits, System.IO.Ports.StopBits.One); port.Open(); port.Close(); Console.WriteLine(portName + " : OK"); } catch (Exception exceptionInfo)
You can call this either directly, as in:
TryOpeningPort("COM10");
or using the initial check method:
foreach (string portName in System.IO.Ports.SerialPort.GetPortNames()) if (portName.Equals("Com10", StringComparison.InvariantCultureIgnoreCase)) TryOpeningPort(portName);
source share