Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing COM10 or greater port in C#

Tags:

c#

serial-port

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

I want to write/read data from COM10.

Here is the simple code for that:

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 as

The port 'COM10' does not exist

On Port.Open(), it should not even reach to command Port.Open() if COM10 doesn't exist.

Another way, I tried Portname as "\.\COM10". It gives an error as

The given port name does not start with COM/com or does not resolve to a valid serial port.

This happens with any port number greater than COM9.

Is there a way out?

like image 764
Swanand Avatar asked Jan 13 '11 05:01

Swanand


People also ask

Why is access to COM port denied?

This error indicates "Access is denied". You may see this error when when attempting to use an already open COM port, it's been used by other program. You could try close other software that may be using the port in the Task manager, and try again.


1 Answers

The reason why you can't open a serial port greater than 10 is because FCL SerialPort implemented like in the following sample:

[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 see, standard SerialPort does not allow you to use \\.\ notation in the port name. And I don't know why they did this. With \\.\ notation, ports greater than 10 can be opened. So, the only way is to implement your own SerialPort component.

like image 188
EngineerSpock Avatar answered Nov 11 '22 16:11

EngineerSpock