C# 查找串行设备COM端口

C# 查找串行设备COM端口,c#,windows,winforms,serial-port,rfid,C#,Windows,Winforms,Serial Port,Rfid,我有一个Windows C#应用程序。应用程序通过串行端口连接到RFID读卡器。虽然我默认为它提供了COM端口3。我遇到了这样的情况:用户的端口不可用,而他的windows操作系统使用的端口与此不同 我的应用程序确实让用户能够更改COM端口,但要找到他们的操作系统正在使用哪个COM端口,用户需要转到设备管理器并进行检查,这对于新手来说可能不太舒服 是否有一种功能或方法可以准确地找到我的RFID卡在Windows中连接到的端口,以便我可以简单地显示如下内容: 应用程序端口设置为:COM。。。。 操

我有一个Windows C#应用程序。应用程序通过串行端口连接到RFID读卡器。虽然我默认为它提供了COM端口3。我遇到了这样的情况:用户的端口不可用,而他的windows操作系统使用的端口与此不同

我的应用程序确实让用户能够更改COM端口,但要找到他们的操作系统正在使用哪个COM端口,用户需要转到设备管理器并进行检查,这对于新手来说可能不太舒服

是否有一种功能或方法可以准确地找到我的RFID卡在Windows中连接到的端口,以便我可以简单地显示如下内容:

应用程序端口设置为:COM。。。。 操作系统上的设备连接端口:COM

我的目标框架也是3.5

编辑1:

尝试使用SerialPort.GetPortNames(),但它返回一个空字符串:System.string[]


我的RFID设备列在设备管理器==>端口(COM和LPT)下,作为Silicon Labs CP210x USB到UART网桥(COM3)

Hi@user3828453以下内容如何,然后您可以,只要使用正确的端口号,如果您仍然有一个空端口,则返回正确的端口号,然后您必须要求用户进入设备管理器并通过您的界面更新端口

private static string GetRFIDComPort()
{
  string portName = "";

  for ( int i = 1; i <= 20; i++ )
  {
    try
    {
      using ( SerialPort port = new SerialPort( string.Format( "COM{0}", i ) ) )
      {
        // Try to Open the port
        port.Open();

        // Ensure that you're communicating with the correct device (Some logic to test that it's your device)

        // Close the port
        port.Close();
      }

    }
    catch ( Exception ex )
    {
      Console.WriteLine( ex.Message );
    }
  }

  return portName;
}
private静态字符串GetRFIDComPort()
{
字符串portName=“”;

对于(int i=1;i这是一个正常问题,串行端口不是即插即用设备。你必须向RFID制造商投诉,并要求更好的方式与之交谈。@HansPassant:我同意你的观点……但我想在这里找到的是,一旦我的设备被连接起来……我能亲自返回给用户它连接的端口吗ed to?这台机器只有一个串行端口,为什么它需要更多,那么选择就相当简单了。SerialPort.GetPortNames()为您提供100%的成功率。如果它有更多,那么您需要知道他在做什么的人来删除多余的一个。Easy peasy.using SerialPort.GetPortNames()为我提供System.String[]作为输出…您无法真正回避要求用户输入正确COM端口以供使用的需要。探测可能会中断RFID读卡器未使用的端口上的活动。您如何处理连接两个读卡器的情况,并希望连接到特定读卡器?(相信我,这种情况/问题最终会发生在节目的(长期)生命周期中。)当你投反对票时,请提供你投反对票的原因。
using System;
using System.Threading.Tasks;
namespace XYZ{
public class Program
{
    public static void Main(string[] args)  
    {
        Task<string> t = Task.Run( () =>
        { 
            return FindPort.GetPort(10);
        });
        t.Wait();

        if(t.Result == null) 
            Console.WriteLine($"Unable To Find Port");
        else
            Console.WriteLine($"[DONE] Port => {t.Result} Received");


        // Console.ReadLine();
    }
}
}
using System;
using System.IO.Ports;
public static class FindPort
{
    public static string GetPort(int retryCount)
    {
        string portString = null;
        int count = 0;
        while( (portString = FindPort.GetPortString() ) == null) {
            System.Threading.Thread.Sleep(1000);
            if(count > retryCount) break;
            count++;
        }
        return  portString;
    }
    static string GetPortString()
    {
        SerialPort currentPort = null;
        string[] portList = SerialPort.GetPortNames();

        foreach (string port in portList)
        {
            // Console.WriteLine($"Trying Port {port}");
            if (port != "COM1")
            {
                try
                {
                    currentPort = new SerialPort(port, 115200);
                    if (!currentPort.IsOpen)
                    {
                        currentPort.ReadTimeout = 2000;
                        currentPort.WriteTimeout = 2000;

                        currentPort.Open();
                        // Console.WriteLine($"Opened Port {port}");

                        currentPort.Write("connect");

                        string received = currentPort.ReadLine();

                        if(received.Contains("Hub"))
                        {
                            // Console.WriteLine($"Opened Port {port} and received {received}");

                            currentPort.Write("close");
                            currentPort.Close();

                            return port;
                        }
                    }
                }
                catch (Exception e)
                {
                    //Do nothing
                    Console.WriteLine(e.Message);

                    if(currentPort.IsOpen)
                    {
                        currentPort.Write("close");
                        currentPort.Close();
                    }
                }
            }
        }
        // Console.WriteLine($"Unable To Find Port => PortLength : {portList.Length}");
        return null;
    }
}