-
WaitCommEvent?
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);
************************************
-
When you say "It does not stop waiting," do you mean WaitCommEvent() never returns?
Kuphryn
-
exactly!
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.
-
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.
-
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 :)