GetCommState always false

824 Views Asked by At

I was trying to make a simple console program that reads all signals from my mouse plugged in with USB. I faced a problem: GetCommState(nCom, &dcb) always returns zero, which is not very usefull for my task. Here is the code:

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() returns 1, but lurking for this problem gave me no results.

Thats simply a copypaste from msdn example, but it occurs that it didn't work for me.

Tell me please: what should i change to make it return nonzero and let me proceed with another part of task.

1

There are 1 best solutions below

0
Turbo J On

USB mice have nothing to do with COM ports, thus calling GetCommState makes no sense at all.

A serial mouse is ancient hardware that is outdated since about 1995. Modern USB mice are based on USB HID protocol.