Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ GetCommState始终为false_C++_Usb_Port_Msdn_Getlasterror - Fatal编程技术网

C++ GetCommState始终为false

C++ GetCommState始终为false,c++,usb,port,msdn,getlasterror,C++,Usb,Port,Msdn,Getlasterror,我试着制作一个简单的控制台程序,读取插入USB的鼠标的所有信号。我遇到了一个问题:GetCommState(nCom和dcb)总是返回零,这对我的任务不是很有用。 代码如下: int _tmain(int argc, TCHAR *argv[]) { DCB dcb; HANDLE hCom; BOOL fSuccess; TCHAR *pcCommPort = TEXT("\\\\.\\HCD0"); // USB name // Open a handle to the specifi

我试着制作一个简单的控制台程序,读取插入USB的鼠标的所有信号。我遇到了一个问题:GetCommState(nCom和dcb)总是返回零,这对我的任务不是很有用。 代码如下:

int _tmain(int argc, TCHAR *argv[]) {
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("\\\\.\\HCD0"); //  USB name

//  Open a handle to the specified com port.
hCom = CreateFile(pcCommPort,
                  GENERIC_READ | GENERIC_WRITE,
                  0,      //  must be opened with exclusive-access
                  NULL,   //  default security attributes
                  OPEN_EXISTING, //  must use OPEN_EXISTING
                  0,      //  not overlapped I/O
                  NULL); //  hTemplate must be NULL for comm devices

if (hCom == INVALID_HANDLE_VALUE) {
    //  Handle the error.
    printf("CreateFile failed with error %d.\n", GetLastError());
    Sleep(15000);
    return (1);
}

//  Initialize the DCB structure.
SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);



//  Build on the current configuration by first retrieving all current
//  settings.
fSuccess = GetCommState(hCom, &dcb);

if (!fSuccess) {
    //  Handle the error.
    printf("GetCommState failed with error %s.\n", GetLastError());

    printf("Cannot get first time");
    Sleep(12000);
    return (2);
}
.......
GetLastError()返回1,但对此问题的潜伏没有给我任何结果

这只是msdn示例中的一个复制粘贴,但我发现它不适用于我


请告诉我:我应该改变什么使其返回非零并让我继续执行任务的另一部分。

USB鼠标与COM端口无关,因此调用
GetCommState
毫无意义

串行鼠标是一种古老的硬件,大约在1995年就过时了。现代USB鼠标基于USB HID协议