【Windows API】打开串口号大于9的串口,CreateFile返回失败!

本文介绍了一段用于枚举计算机有效串口号的代码,并详细解释了如何解决打开高编号串口(如COM10及以上)的问题。通过理解CreateFile函数的使用方式,特别是对于Win32设备命名空间的正确应用,可以成功打开这些串口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

近日,从网络上移植如下代码用于枚举计算机有效串口号。

void EnumCom()
{
    CString				strCOM;  
    BOOL				bResult;  
    HANDLE				hCom;
    int					i;
    DWORD				dwError;

    m_uiCom.RemoveAll();

    for (i = 1; i <= 255; i ++)  
    {  
        //形成串口名称  
        strCOM.Format(_T("COM%d"), i);  
        
        bResult = FALSE;

        //尝试打开串口  
        hCom = CreateFile(strCOM, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);  
        
        if (hCom == INVALID_HANDLE_VALUE)  
        {  
            dwError = GetLastError();  
  
            if (dwError == ERROR_ACCESS_DENIED)  
            {  
                bResult = TRUE;  
                m_uiCom.Add(i);
            }  
        }   
        else  
        {  
            bResult = TRUE;  
            m_uiCom.Add(i);  
              
            CloseHandle(hCom);  
        }  
    }
}

调试时发现打开串口大于9的串口打开失败,GetLastError()函数返回2。

查看MSDN中System Error Codes页面,错误代码2是这么描述的,如下图。

仔细研究MSDN中CreateFile函数的说明,发现有如下一段话。

 

Win32 Device Namespaces

The "\\.\" prefix will access the Win32 device namespace instead of the Win32 file namespace. This is how access to physical disks and volumes is accomplished directly, without going through the file system, if the API supports this type of access. You can access many devices other than disks this way (using the CreateFile and DefineDosDevice functions, for example).

For example, if you want to open the system's serial communications port 1, you can use "COM1" in the call to the CreateFile function. This works because COM1-COM9 are part of the reserved names in the NT namespace, although using the "\\.\" prefix will also work with these device names. By comparison, if you have a 100 port serial expansion board installed and want to open COM56, you cannot open it using "COM56" because there is no predefined NT namespace for COM56. You will need to open it using "\\.\COM56" because "\\.\" goes directly to the device namespace without attempting to locate a predefined alias.

Another example of using the Win32 device namespace is using the CreateFile function with "\\.\PhysicalDisk X" (where X is a valid integer value) or "\\.\CdRom X". This allows you to access those devices directly, bypassing the file system. This works because these device names are created by the system as these devices are enumerated, and some drivers will also create other aliases in the system. For example, the device driver that implements the name "C:\" has its own namespace that also happens to be the file system.

APIs that go through the CreateFile function generally work with the "\\.\" prefix because CreateFile is the function used to open both files and devices, depending on the parameters you use.

If you're working with Windows API functions, you should use the "\\.\" prefix to access devices only and not files.

Most APIs won't support "\\.\"; only those that are designed to work with the device namespace will recognize it. Always check the reference topic for each API to be sure. 

翻译成中文简单讲就是:如果需要打开串口大于9的串口,需要在“COM10”前加上“\\.\”的前缀。因此,上述代码改为如下。

 strCOM.Format(_T("\\\\.\\COM%d"), i);  

综上所述,仔细阅读MSDN还是很重要的!!!

参考:http://www.connecttech.com/KnowledgeDatabase/kdb227.htm

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值