Access to COM10 or greater port in C #

I am using Visual Studio 2010 and programming in C # (.NET 3.5).

I want to write / read data from COM10.

Here is a simple code for this:

static void Main(string[] args) { String Portname = String.Empty; /* List out all COM ports present on the computer. */ foreach (string ports in SerialPort.GetPortNames()) { Console.WriteLine(ports); /* If COM10 exists, copy the name for further use. */ if (ports == "COM10") { Portname = ports; //I also tried this: "\\.\\COM10"; } } /* If COM10 not found, return */ if (Portname == String.Empty) { Console.WriteLine("Exiting"); return; } SerialPort Port = new SerialPort(Portname, 9600, // Baudrate Parity.None, //Parity 8, //DataBits StopBits.One); //Stop Bits Port.Open(); for (int count = 0; count < 5; count++) { Port.WriteLine("\nHello"); } Port.Close(); while (true); } 

Whenever I use Portname as "COM10" in SerialPort Port = new SerialPort(Portname,9600,.....); it gives an error like

COM10 port does not exist

In Port.Open() it should not even reach the Port.Open() command if COM10 does not exist.

Another way, I tried the port name as "\. \ COM10". This gives an error because

The specified port name does not start with COM / com or does not allow a valid serial port.

This happens with any port number greater than COM9.

Is there a way out?

+5
source share
3 answers

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) //most common is System.UnauthorizedAccessException { port = null; /**/Console.WriteLine(portName + " -- " + exceptionInfo.GetType().ToString() + " : " + exceptionInfo.Message); } timer.Stop(); //Console.WriteLine("Elapsed time : " + timer.ElapsedMilliseconds + "ms" + System.Environment.NewLine); timer.Reset(); return port; } 

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); 
0
source

The reason you cannot open a serial port greater than 10 is because FCL SerialPort is implemented, as in the following example:

 [MonitoringDescription("PortName")] [Browsable(true)] [DefaultValue("COM1")] public string PortName { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get { return this.portName; } set { if (value == null) throw new ArgumentNullException("PortName"); if (value.Length == 0) throw new ArgumentException(SR.GetString("PortNameEmpty_String"), "PortName"); if (value.StartsWith("\\\\", StringComparison.Ordinal)) throw new ArgumentException(SR.GetString("Arg_SecurityException"), "PortName"); if (this.IsOpen) throw new InvalidOperationException(SR.GetString("Cant_be_set_when_open", new object[1] { (object) "PortName" })); else this.portName = value; } } 

As you can see, the standard SerialPort does not allow you to use \\. \ Notation in the port name. And I don’t know why they did it. If the value is \\. \, You can open ports greater than 10. Thus, the only way is to implement your own SerialPort component.

0
source
  • For a port less than or equal to 9: new SerialPort("COM9")
  • For ports over 9: new SerialPort("\\\\.\\COM10")

I tested using the serialArduino.PortName = ... property for ports greater than 9, but this always led to errors, so I used the new SerialPort() constructor.

0
source

Source: https://habr.com/ru/post/1335359/


All Articles