【问题标题】:Finding the serial device COM port查找串行设备 COM 端口
【发布时间】:2014-10-10 20:20:24
【问题描述】:

我有一个 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)

【问题讨论】:

  • 这是正常问题,串口不是即插即用设备。您必须向 RFID 制造商投诉,并寻求更好的方式与之交谈。
  • @HansPassant :我同意你的看法......但我想在这里找到的是,一旦我的设备已连接......我可以自己返回给用户它已连接到哪个端口?
  • 本机只有一个串口,为什么还要多,那么选择就比较简单了。 SerialPort.GetPortNames() 为您提供 100% 的成功率。如果它有更多,那么你需要有人知道他在做什么来移除额外的。轻松愉快。
  • 使用 SerialPort.GetPortNames() 给我 System.String[] 作为输出 ....
  • 您无法真正绕过实际要求用户输入正确 COM 端口以使用的需求。探测可能会中断 RFID 阅读器未使用的端口上的活动。您如何处理连接了两个阅读器的情况,并且您想连接到特定的阅读器? (相信我,这种情况/问题最终会在程序的(长期)生命周期中发生。)

标签: c# windows winforms serial-port rfid


【解决方案1】:

嗨@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;
}

【讨论】:

  • 当您投反对票时,请说明您投反对票的理由。
【解决方案2】:
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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 2012-08-09
    • 2013-03-29
    • 2023-04-01
    相关资源
    最近更新 更多