Hi!

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;
memset(&overlapped,0,sizeof(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;

}
else
{
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;

/*~T*/

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

/*~T*/
dcbStatus = SetCommState(hComm, &dcb);

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

/*~T*/
//status = TRUE;


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

CloseHandle(tComm);

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

************************************