Problems with COM Ports > COM9 on Windows 10

Hello,
I have problems with virtual Com Ports on a Windows 10 64 bit System.
When pairing with different Bluetooth modules, Windows 10 assigns virtual Com Ports with a number greater than 9 (Windows 8.1 32 bit assigns Com Ports < 10).
Now on Windows 10 I could not manage to open these virtual Com Ports. I get an exception when I try to open the ports > COM9.
Anybody knows how to solve?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace Serial_Port_Test
{
    public partial class Form1 : Form
    {
        string[] ComPorts = null;
        SerialPort mySerialPort;

        public Form1()
        {
            InitializeComponent();
            ComPorts = SerialPort.GetPortNames();
            mySerialPort = new SerialPort(ComPorts[2]);
            try
            {
                mySerialPort.Open();
                MessageBox.Show("ComPort: " + ComPorts[2] + " could be opened");
            }
            catch (Exception ex)
            {
                MessageBox.Show("ComPort: " + ComPorts[2] + " could not be opened \r\n"
                + "Error Message: " + ex.Message);
            }
           
        }
    }
}

I think your answer is here : HOWTO: Specify Serial Ports Larger than COM9 - Microsoft Support

You need to use an alternative notation for the com port name.

@ mcalsyn - Thanks for the answer, but I do not unterstand how to do it in a short time. Do you have a short code example in C#?

I have not tested this, but it should go something like this on line 23:



Basically, just add \\.\ in front of the com port name.  This works for all com port names, not just COM10 and up.

Throws an exception.
Is it needed to use PInvoke or similar to access the Windows API for CreateFile()?

No - I was hoping it was just a notational change. I don’t want to guess at this further and I don’t have a compiler handy at the moment.

@ andre.m - thanks, seems to be good for a Store App.
Is it possible to use the Windows.Devices.SerialCommunication namespace in a Forms Application?

@ andre.m -
ok, then how to access virtual serial ports in a new fashioned one?
Could not find the namespace Windows.Devices.SerialCommunication in the Windows RT Api.
This post is rather old but perhaps still correct:
https://social.msdn.microsoft.com/Forums/windowsapps/en-US/58ae107a-5cef-4848-9df5-00685303adaa/access-serial-port-in-windows-store-app?forum=tailoringappsfordevices

The only references I can find to your problem all refer to solving it using P/Invoke to access COM ports 10 and up. I have not found any good examples of that. I did find source code that excludes the \.\COMXX notation in the SerialPort() source as some sort of security issue.

In Modern/Universal apps, you can use the code that andre.m showed. In ‘classic’ desktop apps, you’ll probably have to resort to accessing the serial port via P/Invoke calls to the WIN32 api. That will work no matter what the COM port name is. If all you need is some basic setup (baud, format, handhshake) and blocking read/blocking write, then it shouldn’t be too hard as you’ll only need to map a few methods and structures. I don’t have any examples handy though.

@ mcalsyn -
Thanks for the time you spent on this issue.
I only wanted a short test if the GHI Bluetooth Module and the Seeed BLE dual Bee (HM-13) module work on Windows 10 as well as on 8.1. It was not so short as I thought… I think that I go away from the Forms App and use a Universal or Store App with the Devices.Bluetooth.Rfcomm Class.

For a quick workaround, you could always rename Com17, and assign it to a free Com < 9

I have to do this all the time, to run old apps.

According to the link below .Net doesn’t allow the opening of COM ports that do not begin with COM:

so maybe if you try what Martin said but add COM in front of it see what that does!


mySerialPort = new SerialPort(@ "COM\\.\COM10");

[/quote]

Cheers,
Jay

@ mtylerjr -
@ jay jay
Thanks for the good ideas. I’ll be away for some days, then I will try