dotnet/runtime

SerialPort error on Virtual Serial Port for USB

Open

#30.877 aperta il 17 set 2019

Vedi su GitHub
 (15 commenti) (2 reazioni) (0 assegnatari)C# (5445 fork)batch import
area-System.IO.Portshelp wantedos-windows

Metriche repository

Star
 (17.886 star)
Metriche merge PR
 (Merge medio 12g 11h) (661 PR mergiate in 30 g)

Descrizione

I'm trying to access a Virtual Serial Port for USB with System.IO.Ports.SerialPort and I get the error The given port name (COM5) does not resolve to a valid serial when I try to open the port. Here is the code, it works with physical serial ports:

using (var serialPort = new System.IO.Ports.SerialPort("COM5", 9600))
{
    try
    {
        serialPort.Open(); // ERROR: The given port name (COM5) does not resolve to a valid serial port.
        serialPort.WriteLine("SerialPort Write Test");
        serialPort.Close();
    }
    catch (Exception exception)
    {
        Console.WriteLine(exception);
    }
}

I did several tests with .NET 4.7, .NET Core 3.0-RC1, the 4.6.0-rc1 of nuget System.IO.Ports, UWP application with Windows.Devices.SerialCommunication.SerialDevice. Always the same error.

The Virtual Serial Port works well with other programs, such as Termite by Compuphase or Serial Port Terminal by Eltima.

I have also tried with an alternative .NET library SerialPortStream and the access to the COM Port works fine.

Looking at the code, I see that if the fileType is different from FILE_TYPE_CHAR and FILE_TYPE_UNKNOWN, the error is reported. The problem is that my Virtual Service Port returns FILE_TYPE_DISK. Is there a reason to refuse FILE_TYPE_DISK? Is it possible to accept it or make it configurable?

Guida contributor