我在 C++ 中使用 COM 端口。我无法打开大于 9 的 COM 端口,例如 10。这是用于 COM 端口检测的功能:
WCHAR port_name[7];
WCHAR num_port[4];
for (i=1; i<256; i++)
{
bool bSuccess = false;
wcscpy(port_name,L"COM");
wcscat(port_name,_itow(i,num_port,10));
HANDLE hPort;
//Try to open the port
hPort = CreateFile(L"COM10", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
//hPort = CreateFile(port_name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (hPort == INVALID_HANDLE_VALUE)
{
DWORD dwError = GetLastError();
//Check to see if the error was because some other application had the port open
if (dwError == ERROR_ACCESS_DENIED)
{
bSuccess = TRUE;
j=j+1;
}
}
else //The port was opened successfully
{
bSuccess = TRUE;
j=j+1;
CloseHandle(hPort); //closing the port
}
if (bSuccess)array_ports[j]=i;
}
我不明白为什么例如 COM10 将 FFFFFFFF 扔回 HANDLE hPort。
hPort = CreateFile(L"COM10", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
COM9、COM8、COM7 等工作正常,
hPort = CreateFile(L"COM9", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
这个问题有解决方案吗?