2015-10-01 71 views
0

我试图制作一个简单的控制台程序,用于读取插入USB的鼠标的所有信号。我遇到了一个问题:GetCommState(nCom,& dcb)总是返回零,这对我的任务不是非常有用。 下面是代码:GetCommState始终为假

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示例中的副本,但它发生了它并不适合我。

请告诉我:我应该改变什么使它返回非零值,让我继续完成另一部分任务。

回答

2

USB鼠标与COM端口无关,因此调用GetCommState根本没有意义。

一个串行鼠标是硬件是从1995年左右过时。现代USB鼠标是基于USB HID协议。