Thread: WaitCommEvent?

  1. #1
    Registered User
    Join Date
    Apr 2003



    Im writing a software which should listen to the serial port. It works but I do not want to loop alla the time to see if something has come.

    There I found WaitCommEvent() which sounded very nice. But I connot get it to work properly. It does not stop waiting when there is something comming to the serial port. What have I done wrong?

    Thanks for any help!

    Here is the code:
    static int raknare = 0;

    HANDLE tComm, hComm;
    LPVOID inBuffer;
    DWORD noBytes = 4096;
    LPDWORD lpBytesRead;
    DCB dcb;
    BOOL dcbStatus;

    char sBuffer[128];
    DWORD iBytesRead;

    DWORD dwEvtMask=0;
    OVERLAPPED overlapped;
    overlapped.hEvent = CreateEvent(
    NULL, // no security attributes
    TRUE, // auto reset event
    FALSE, // not signaled
    NULL // no name

    tComm = CreateFile( "COM1",GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);

    if (tComm == INVALID_HANDLE_VALUE)
    //status = FALSE;
    //printf ("<CreateFile> Errorcode: %lx\n",GetLastError());
    raknare = -1;

    hComm = tComm;

    WaitCommEvent(hComm, &dwEvtMask,&overlapped);

    GetCommState(hComm, &dcb);

    // Fill in the DCB: baud=9600, 8 data bits, no parity, 1 stop bit.

    dcb.BaudRate = 9600;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
    dcb.StopBits = ONESTOPBIT;


    // BuildCommDCB((WCHAR *)"9600,n,8,1,x", &dcb);

    dcbStatus = SetCommState(hComm, &dcb);

    //if (!dcbStatus)
    //printf ("<SetCommState> Errorcode: %lx\n",GetLastError());

    //status = TRUE;

    //ReadFile ( hComm, &inBuffer, noBytes, lpBytesRead, NULL);
    ReadFile ( hComm, &sBuffer, 6, &iBytesRead, NULL);


    m_SerialText.Format("%s %d", sBuffer, raknare);
    m_NonStaticText.Format("texten %d", raknare);


  2. #2
    Registered User
    Join Date
    Nov 2001
    When you say "It does not stop waiting," do you mean WaitCommEvent() never returns?


  3. #3
    Registered User
    Join Date
    Apr 2003

    It just stops at:
    WaitCommEvent(hComm, &dwEvtMask,&overlapped);

    I know that something comes through the serial port so I cannot understand why it just waits.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    I can't try this becaise I don't have any hardware to talk to the port.

    Something that struck me was the lpOverlapped parameter. In the description of the WaitCommEvent(), it says this parameter is required if you open the resource with FILE_FLAG_OVERLAPPED which you don't appear to be doing.

    From the help...

    If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL.

    ... I would try setting the third parameter to NULL and see what happens - can't be any worse.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Thank you for youre help but I solved it finally. I did a bounch of stuff but the line that got it to work was
    SetCommMask(hComm, EV_RXCHAR);

    WaitCommEvent didnt know what to wait for

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WaitCommEvent()????
    By SuperNewbie in forum Windows Programming
    Replies: 3
    Last Post: 03-10-2004, 12:04 AM
  2. Cancel WaitCommEvent on win2k?
    By yoxler in forum Windows Programming
    Replies: 0
    Last Post: 05-08-2003, 12:34 AM