Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SerialPort.GetPortNames() returns incorrect port names

While c# is not my primary programming language, I'm maintaining such a program for a couple of years now. This program connects to a device on a serial port and works from Windows XP up to 8.1. One specific "feature" is that it uses .NET Framework 2.0.

With some users upgrading to Windows 10 we've got complains that the program cannot detect/open the COM port of the device. We have confirmed this on our own test systems with clean Win10 installation.

It turns out that the function SerialPort.GetPortNames() returns incorrect port names and adds 'strange' characters after the port name. For example:

  • COM3吀
  • COM3䡢
  • COM3゠

Etc. When I refresh the list, every time another character (or two) shows up after the number. The test code is super straightforward:

  string[] portNames = System.IO.Ports.SerialPort.GetPortNames();
  log("Available ports:");
  foreach (string PortAvailable in portNames)
  {
    log(PortAvailable);
  }

Where the log function mearly adds a line to a standard TextBox on the form:

  txtLog.Text += Msg + Environment.NewLine;

This works in every other Windows version. I've checked the registry and all looks fine there also. Does anybody has an idea about this?

I'm suspecting that .NET Framework 2.0 is not 100% compatible anymore, although you can enable it in the Windows features and it seems that the program itself runs fine (besides my serial port problem). I'm a bit scared to upgrade to a newer .NET, let alone that we've VisualStudio 2008 for c# (max=.NET 3.5). Note that the program still needs to run on Windows XP also (POS version is still maintained by Microsoft).

ADDED: I've "upgraded" a test program to .NET 3.5, and still having exactly the same issue. Next step is to install a new VisualStudio (it appears that it is free nowadays?? Should I check for privacy settings in Studio also? ;-).

ADDED 2: Installed VisualStudio 2015 and made multiple builds with different .NET framework versions. v2.0 and 3.5 still adding the strange character. But in v4.0 and up this issue seems te be solved! Now to get the original program compiled and working for the newer Framework. But I find this issue really strange and would expect that this would hit more .NET functions and more programs.

like image 757
msoft Avatar asked Aug 16 '15 21:08

msoft


3 Answers

I've seen the strange characters too. My solution is using Regular Expressions to filter out the number portion of the comm port name.

Dim ports As New Devices.Ports
For Each s As String In ports.SerialPortNames
    s = Regex.Replace(s, "\D*(\d+)\D*", "$1")
    Debug.WriteLine(s)
Next
like image 56
Bob Aune Avatar answered Nov 04 '22 06:11

Bob Aune


I've had this exact same problem with USB CDC serial devices, handled by the new rewritten Windows 10 usbser.sys driver.

The garbage characters are often digits, so removing non-digits isn't a reliable way to work around it. For my solution, look at my last post on this thread here:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/a78b4668-ebb6-46aa-9985-ec41667abdde/ioportsserialportgetportnames-registrykeygetvalue-corruption-with-usbsersys-driver-on-windows?forum=netfxbcl

..there is code there that'll go through the registry, find usbser ports, and return their unmangled names. Beware that it doesn't return all serial ports, just ones provided by that driver. The code works on Windows XP through to 10.

The underlying problem is that the usbser.sys driver creates a registry entry, and on .NET (at least up to 3.5) the GetPortNames() function tries to read those registry keys and gets corrupted data. I've reported this to Microsoft, both via that forum (assuming they read it) and using the built-in Windows 10 beta error reporting tool. Maybe one day there will be a fix.

like image 38
sstteevvee Avatar answered Nov 04 '22 06:11

sstteevvee


As you say the program works after enabling the windows feature:

.NET 2.0,3.0,3.5 isn't enabled by default on Windows 8/8.1/10. The files aren't stored on the install media/wim.

It can be enabled with the DISM command from windows update or a local source.

DISM /Online /Enable-Feature /FeatureName:NetFx3 /All
like image 1
yobbo Avatar answered Nov 04 '22 04:11

yobbo