代码之家  ›  专栏  ›  技术社区  ›  sorin

检测Windows上所有可用串行端口的正确方法是什么?

  •  6
  • sorin  · 技术社区  · 16 年前

    有几种方法可以在Windows下列出串行端口,但我不确定什么是正确的方法:检测所有可用串行端口的方法。

    一个好的代码示例是 http://www.naughter.com/enumser.html -那里有9(9!)枚举串行设备的方法。

    要求:

    • 不打开端口以检查它们是否可用。
    • 能够检测具有不同名称的端口 COMx
    • 在Windows XP SP2或更高版本上工作
    4 回复  |  直到 16 年前
        1
  •  6
  •   BenMorel Manish Pradhan    12 年前
    void SelectComPort() //added function to find the present serial 
    {
    
        TCHAR lpTargetPath[5000]; // buffer to store the path of the COMPORTS
        DWORD test;
        bool gotPort=0; // in case the port is not found
    
        for(int i=0; i<255; i++) // checking ports from COM0 to COM255
        {
            CString str;
            str.Format(_T("%d"),i);
            CString ComName=CString("COM") + CString(str); // converting to COM0, COM1, COM2
    
            test = QueryDosDevice(ComName, (LPSTR)lpTargetPath, 5000);
    
                // Test the return value and error if any
            if(test!=0) //QueryDosDevice returns zero if it didn't find an object
            {
                m_MyPort.AddString((CString)ComName); // add to the ComboBox
                gotPort=1; // found port
            }
    
            if(::GetLastError()==ERROR_INSUFFICIENT_BUFFER)
            {
                lpTargetPath[10000]; // in case the buffer got filled, increase size of the buffer.
                continue;
            }
    
        }
    
        if(!gotPort) // if not port
        m_MyPort.AddString((CString)"No Active Ports Found"); // to display error message incase no ports found
    
    }
    
        2
  •  4
  •   Doc    11 年前

    如果您可以访问注册表 HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM 密钥包含Windows当前支持的COM端口列表(在某些情况下,此信息可能已过时/不正确;我怀疑,当提供串行端口的即插即用设备尚未完成检测/安装或最近已被删除时)。

    这就是.NET Framework的 SerialPort.GetPortNames() 方法报告可用的COM端口,并且上述信息是从链接页派生的。

        3
  •  3
  •   Hans Passant    16 年前

    串行端口是非常简单的设备,可以追溯到计算机硬件的石器时代。它们不支持即插即用,无法判断是否有人插入了设备。唯一能做的就是发现哪些端口可用,SerialPort.GetPortNames()返回列表。一些USB模拟器可以生成一个描述性的名称来与端口名匹配,您可以发现那些具有WMI、Win32_SerialPort类的。

    这些都无法帮助您发现连接到特定设备的COM端口。只有一个人知道,她把电缆插在了连接器上。您需要提供一个配置UI,让用户选择端口号。组合框完成任务。将所选内容保存在配置数据中,它是 很可能下次程序启动时设备仍连接到同一端口。

        4
  •  0
  •   Dženan    7 年前

    这是一个现代化版本的@michael jacob mathew的答案:

    #include <iostream>
    #include <string>
    #include <Windows.h>
    
    bool SelectComPort() //added function to find the present serial 
    {
        char lpTargetPath[5000]; // buffer to store the path of the COMPORTS
        bool gotPort = false; // in case the port is not found
    
        for (int i = 0; i < 255; i++) // checking ports from COM0 to COM255
        {
            std::string str = "COM" + std::to_string(i); // converting to COM0, COM1, COM2
            DWORD test = QueryDosDevice(str.c_str(), lpTargetPath, 5000);
    
            // Test the return value and error if any
            if (test != 0) //QueryDosDevice returns zero if it didn't find an object
            {
                std::cout << str << ": " << lpTargetPath << std::endl;
                gotPort = true;
            }
    
            if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
            }
        }
    
        return gotPort;
    }
    

    COM1: \Device\Serial0
    COM3: \Device\VCP0
    
        5
  •  -2
  •   Nathaniel Ford iFail    10 年前
    CUIntArray ports;
    EnumerateSerialPorts(ports);
    
    for (int i = 0; i<ports.GetSize(); i++)
    {
        CString str;
        str.Format(_T("COM%d"), ports.ElementAt(i));
        m_ctlPort.AddString(str);
    }