Open a COM port in C++ with number higher that 9

前端 未结 2 1622
广开言路
广开言路 2020-12-16 21:16

I am using a COM port in C++. I can not open COM ports with a higher number than 9, for example 10. This is function used for COM port detection:

WCHAR port_         


        
相关标签:
2条回答
  • 2020-12-16 22:04

    It is a bug and the resolution is to use the string

    \\.\COM10
    
    hPort = CreateFile("\\\\.\\COM10", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
    

    check this article.

    0 讨论(0)
  • 2020-12-16 22:05

    You need to use the following format for COM ports greater than 9:

    \\\\.\\COM%d
    

    Where %d is a printf-substitution for the port number.

    Why? Well, this accesses the global NT object space, where all objects are stored. Windows only knows to alias COM0-9 in the fashion you're using it for DOS support; beyond that, they act like ordinary devices, which are accessed this way.

    To explore the NT object space, I recommend WinObj which basically lets you browse around. \.\ is mapped to GLOBAL?? in this tree (as are some other areas, actually. The rest of the tree requires you have NT's, as opposed to Win32's, view of the system).

    And just in case you didn't know, INVALID_HANDLE_VALUE is defined as 0xffffff... - this usually occurs when an open fails.

    0 讨论(0)
提交回复
热议问题