2017-09-01 32 views
-1
using System; 
using System.IO.Ports; 

public static void getPorts() 
{ 
    string[] ports = SerialPort.GetPortNames(); 

    Console.WriteLine("The following serial ports were found:"); 

    foreach (string port in ports) 
    { 
     Console.WriteLine(port); 
    } 
    Console.ReadLine(); 
} 

我对这个简单的代码有问题。我需要写出我的端口,因为我稍后会与他们合作(我在我的大型应用程序中使用了这个简单的功能)。一切工作,直到我升级我的Windows。你有什么想法可能是什么问题?我也安装了所有的驱动程序。SerialPort(.net) - 没有找到端口

+0

确切的问题是什么?你有什么错误吗?请在代码中添加'try-catch'块。 –

+0

我是否正确,我认为你的港口已经消失了? –

+0

你能看到设备管理器中的端口号 – JSR

回答

1

SerialPort.GetPortNames方法将不会列出一些虚拟串行端口(如某些USB到RS232适配器等提供的端口),因为它依赖于documentation中指定的HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM注册表项。 您可以通过WMI查询来列出所有串行端口,详见here