C++ QueryDosDeviceA() return 0 但 COMX 已连接
C++ QueryDosDeviceA() return 0 but COMX is connected
所以我正在执行一个函数来检索使用 QueryDosDeviceA
连接到系统的所有 COM 串行端口名称
我尝试将 CreatFileA
与 "\\.\COM6"
一起使用,但没有出现任何错误。
但是 QueryDosDeviceA
与 "COM6"
或 "\\.\COM6"
Function.cpp
std::vector<std::string> SerialPort::_SerialList()
{
std::vector<std::string> serialList;
std::string COMName("\\.\COM"), queryName(COMName); //also tried with COMName("COM")
CHAR bufferTragetPath[5000];
std::string tmp;
DWORD path_size(0);
//test each COM name to get the one used by the system
for (int i(0); i < 255; i++)
{
queryName = COMName + std::to_string(i);
//Query the path of the COMName
path_size = QueryDosDeviceA((LPCSTR)&queryName, (LPSTR)&bufferTragetPath, 5000);
std::cout << std::endl << "Path for " << queryName << ":" << path_size;
if (path_size != 0) {
std::cout << "pushing..." << queryName << " on " << bufferTragetPath << std::endl;
serialList.push_back(queryName);
}
}
return serialList;
}
这是输出:
Path for \.\COM0:0
Path for \.\COM1:0
Path for \.\COM2:0
Path for \.\COM3:0
Path for \.\COM4:0
Path for \.\COM5:0
Path for \.\COM6:0
Path for \.\COM7:0
Path for \.\COM8:0
Path for \.\COM9:0
...
Path for \.\COM253:0
Path for \.\COM254:0Port name: \.\COM6
Hello World !
所以除了 COM6 端口可以被 CreateFileA 使用之外什么也没找到
感谢@user253751、@JohnnyMopp、@dresscherjm
还有@RemyLebeau
这是包含解决方案的代码:
std::vector<std::string> SerialPort::_SerialList()
{
std::vector<std::string> serialList;
std::string COMName("COM"), queryName("");
CHAR bufferTragetPath[5000];
std::string tmp;
DWORD path_size(0);
//test each COM name to get the one used by the system and get his description name
for (int i(0); i < 255; i++)
{
queryName = COMName + std::to_string(i);
//Query the path of the COMName
path_size = QueryDosDeviceA(queryName.c_str(), bufferTragetPath, 5000);
std::cout << std::endl << "Path for " << queryName << ":" << path_size << " " << queryName;
if (path_size != 0) {
std::cout << "pushing..." << queryName << " on " << bufferTragetPath << std::endl;
serialList.push_back(tmp);
}
}
return serialList;
}
基本上我将 queryName 的转换从字符串更改为 LPCSTR
//LPCSTR => Long Pointer Const String(= const char)
queryName.c_str() //return queryName as const char*
我还从 bufferTragetPath
中删除了一个无用的转换
//from
(LPSTR)&bufferTragetPath
//to
bufferTragetPath //cause bufferTragetPath is char *
所以我正在执行一个函数来检索使用 QueryDosDeviceA
我尝试将 CreatFileA
与 "\\.\COM6"
一起使用,但没有出现任何错误。
但是 QueryDosDeviceA
与 "COM6"
或 "\\.\COM6"
Function.cpp
std::vector<std::string> SerialPort::_SerialList()
{
std::vector<std::string> serialList;
std::string COMName("\\.\COM"), queryName(COMName); //also tried with COMName("COM")
CHAR bufferTragetPath[5000];
std::string tmp;
DWORD path_size(0);
//test each COM name to get the one used by the system
for (int i(0); i < 255; i++)
{
queryName = COMName + std::to_string(i);
//Query the path of the COMName
path_size = QueryDosDeviceA((LPCSTR)&queryName, (LPSTR)&bufferTragetPath, 5000);
std::cout << std::endl << "Path for " << queryName << ":" << path_size;
if (path_size != 0) {
std::cout << "pushing..." << queryName << " on " << bufferTragetPath << std::endl;
serialList.push_back(queryName);
}
}
return serialList;
}
这是输出:
Path for \.\COM0:0
Path for \.\COM1:0
Path for \.\COM2:0
Path for \.\COM3:0
Path for \.\COM4:0
Path for \.\COM5:0
Path for \.\COM6:0
Path for \.\COM7:0
Path for \.\COM8:0
Path for \.\COM9:0
...
Path for \.\COM253:0
Path for \.\COM254:0Port name: \.\COM6
Hello World !
所以除了 COM6 端口可以被 CreateFileA 使用之外什么也没找到
感谢@user253751、@JohnnyMopp、@dresscherjm 还有@RemyLebeau
这是包含解决方案的代码:
std::vector<std::string> SerialPort::_SerialList()
{
std::vector<std::string> serialList;
std::string COMName("COM"), queryName("");
CHAR bufferTragetPath[5000];
std::string tmp;
DWORD path_size(0);
//test each COM name to get the one used by the system and get his description name
for (int i(0); i < 255; i++)
{
queryName = COMName + std::to_string(i);
//Query the path of the COMName
path_size = QueryDosDeviceA(queryName.c_str(), bufferTragetPath, 5000);
std::cout << std::endl << "Path for " << queryName << ":" << path_size << " " << queryName;
if (path_size != 0) {
std::cout << "pushing..." << queryName << " on " << bufferTragetPath << std::endl;
serialList.push_back(tmp);
}
}
return serialList;
}
基本上我将 queryName 的转换从字符串更改为 LPCSTR
//LPCSTR => Long Pointer Const String(= const char)
queryName.c_str() //return queryName as const char*
我还从 bufferTragetPath
中删除了一个无用的转换
//from
(LPSTR)&bufferTragetPath
//to
bufferTragetPath //cause bufferTragetPath is char *