WaitCommEvent Problem



  • Hi, habe ein Problem mit WaitCommEvent habe an DSR extern einen Schalter angeschlossen und will nun mit WaitCommEvent herausfinden wann er gedrückt wurde.
    Wenn ich mit

    DWORD Flag;
      if(hwndCOM != INVALID_HANDLE_VALUE){
        if(GetCommModemStatus(hwndCOM, &Flag))
          return((Flag & MS_DSR_ON) > 0);
      }
      return(false);
    

    den Status abfrage bekomme ich das richtige Ergebnis zurück. Wenn ich allerdings mein Programm auf das WaitCommEvent auflaufen lasse merkt es nicht das der Taster gedrückt wurde.

    Selbst folgendes Beispiel aus der MSDN funktioniert nicht:

    #include <windows.h>
    #include <assert.h>
    #include <stdio.h>
    
    void main( )
    {
        HANDLE hCom;
        OVERLAPPED o;
        BOOL fSuccess;
        DWORD dwEvtMask;
    
        hCom = CreateFile( "COM1",
            GENERIC_READ | GENERIC_WRITE,
            0,    // exclusive access 
            NULL, // default security attributes 
            OPEN_EXISTING,
            FILE_FLAG_OVERLAPPED,
            NULL 
            );
    
        if (hCom == INVALID_HANDLE_VALUE) 
        {
            // Handle the error. 
            printf("CreateFile failed with error %d.\n", GetLastError());
            return;
        }
    
        // Set the event mask. 
    
        fSuccess = SetCommMask(hCom, EV_CTS | EV_DSR);
    
        if (!fSuccess) 
        {
            // Handle the error. 
            printf("SetCommMask failed with error %d.\n", GetLastError());
            return;
        }
    
        // Create an event object for use by WaitCommEvent. 
    
        o.hEvent = CreateEvent(
            NULL,   // default security attributes 
            FALSE,  // auto reset event 
            FALSE,  // not signaled 
            NULL    // no name
            );
    
        // Intialize the rest of the OVERLAPPED structure to zero.
        o.Internal = 0;
        o.InternalHigh = 0;
        o.Offset = 0;
        o.OffsetHigh = 0;
    
        assert(o.hEvent);
    
        if (WaitCommEvent(hCom, &dwEvtMask, &o)) 
        {
            if (dwEvtMask & EV_DSR) 
            {
                 // To do.
            }
    
            if (dwEvtMask & EV_CTS) 
            {
                // To do. 
            }
        }
        else
        {
            DWORD dwRet = GetLastError();
            if( ERROR_IO_PENDING == dwRet)
            {
                printf("I/O is pending...\n");
    
                // To do.
            }
            else 
                printf("Wait failed with error %d.\n", GetLastError());
        }
    }
    

    Ich hoffe jemand kann mir helfen... es ist wirklich wichtig.

    Gruss
    Michael



  • hi! schau dir mal an wie ich das mache...ich glaub dir fehlt da ein WaitForSingleObject??

    Taster an DSR - Status prüfen:
    -----------------------------
    
    // WaitCommEvent function monitors a set of events for a specified communications resource
    // Wait for event to happen
    if (!WaitCommEvent(pReceive->getHandle(), &dwEvtMask, &pReceive->getOVERLAPPED_receive()))
    {
    	if (::GetLastError() != ERROR_IO_PENDING) //Overlapped IO–Operation is in progress
    		throw Exception("::Overlapped IO Operation is in progress!", __FILE__, __LINE__);
    }
    
    // Wait for Set Receive Event
    WaitForSingleObject(pReceive->getEventHandleReceive(), INFINITE);
    

    cu



  • if (WaitCommEvent(hCom, &dwEvtMask, &o)) 
    {
    	if (dwEvtMask & EV_DSR) 
    	{
        		cout << "The DSR signal is on" << endl; 
    	}
    	else
    	{		
    		cout << "The DSR signal is off" << endl;
    	}
    }
    

    klappt es wenn du es so einbaust?

    cu



  • @surf; nein leider funktioniert es so auchnicht!
    Das WaitCommEvent an sich kommt ja schon immer mit false zurück.
    Hole ich mir mit GetLastError(); den Errorcode steht 997 drin was als Text I/O Pending bedeutet..

    Kann es sein das in deinem ersten Post

    if (::GetLastError() != ERROR_IO_PENDING) //Overlapped IO–Operation is in progress
            throw Exception("::Overlapped IO Operation is in progress!", __FILE__, __LINE__);
    

    das != ...

    if (::GetLastError() == ERROR_IO_PENDING) //Overlapped IO–Operation is in progress
            throw Exception("::Overlapped IO Operation is in progress!", __FILE__, __LINE__);
    

    heissen müsste? Warum sollte ich dann nochmal mit WaitForSingleObjetauf das Event warten müssen?! Versteh ich das irgendwie falsch??

    Wenn ich das Ganze als non-Overlapped Operation mach , bleibt mein waitcomm.. ganz hängen.

    Bei deinem ersten Post bleibt mein Code dann am WaitForSingleObjet hängen..

    Gruss
    Michael


Anmelden zum Antworten