0

我正在尝试制作一个简单的控制台程序,该程序可以从插入 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 示例的复制粘贴,但它对我不起作用。

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

4

1 回答 1

2

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

串行鼠标是自 1995 年左右就已经过时的古老硬件。现代 USB 鼠标基于 USB HID 协议。

于 2015-10-01T22:28:05.323 回答