【问题标题】:GetCommState always falseGetCommState 始终为假
【发布时间】:2015-10-01 20:40:01
【问题描述】:

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

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

【问题讨论】:

    标签: c++ usb port msdn getlasterror


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-08-20
      • 2016-04-19
      • 2016-07-24
      • 2021-03-05
      • 1970-01-01
      • 2023-02-14
      • 2017-10-12
      • 2013-07-30
      • 1970-01-01
      相关资源
      最近更新 更多