SerialPort.GetPortNames()返回不正确的端口名称 [英] SerialPort.GetPortNames() returns incorrect port names

查看:486
本文介绍了SerialPort.GetPortNames()返回不正确的端口名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然c#不是我的主要编程语言,但我已经将这样的程序维护了几年。该程序连接到串行端口上的设备,并且可以在Windows XP或更高版本的8.1上运行。一个特定的功能是它使用.NET Framework 2.0。

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.

随着某些用户升级到Windows 10,我们抱怨该程序无法检测/打开COM。设备的端口。我们已经在干净安装Win10的测试系统上确认了这一点。

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.

事实证明,函数SerialPort.GetPortNames()返回错误的端口名并添加了奇怪的字符端口名称后。
例如:

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);
  }

其中log函数只是在表单上的标准TextBox中添加一行:

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

  txtLog.Text += Msg + Environment.NewLine;

此功能可在其他所有Windows版本中使用。
我已经检查了注册表,那里的一切看起来也不错。
有人对此有想法吗?

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?

我怀疑.NET Framework 2.0不再100%兼容,尽管您可以在其中启用它。 Windows功能,似乎程序本身运行正常(除了我的串行端口问题)。我有点害怕升级到新的.NET,更不用说我们拥有Visual Studio 2008 for c#(max = .NET 3.5)。请注意,该程序仍需要在Windows XP上运行(POS版本仍由Microsoft维护)。

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).

已添加:
我已经升级了测试程序到.NET 3.5,仍然存在完全相同的问题。
下一步是安装一个新的VisualStudio(现在看来它是免费的?我是否也应该在Studio中检查隐私设置?;-)。

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? ;-).

添加2:
安装VisualStudio 2015,并使用不同的.NET Framework版本进行多个构建。 v2.0和3.5仍然添加了奇怪的字符。但是在v4.0及更高版本中,似乎可以解决此问题!现在,要编译原始程序并为较新的Framework工作。
但是我发现这个问题确实很奇怪,并且希望它会影响更多的.NET功能和更多程序。

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.

推荐答案

我也看到过奇怪的角色。我的解决方案是使用正则表达式过滤掉通讯端口名称的数字部分。

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

这篇关于SerialPort.GetPortNames()返回不正确的端口名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆