WaitForSingleObject
-
I have external device. The device is connected with the computer through the serial-port. I send on the device number (signal), if it answers, the necessary procedure is started. I can use WaitCommEvent() for event (signal, EV_DSR), but if devise doesn't answer, WaitCommEvent will wait indefinitely, but i need to wait about 5 sec. I used WaitForSingleObject, but it doesn't work! I don't know why. In what my error? HANDLE port; DWORD bc; DCB dcb; char buffer[100]; DWORD dwBlockSize=512; void *szBuffer; int numb=255; BOOL fSuccess; OVERLAPPED event; DWORD dwEvtMask; ZeroMemory(&dcb,sizeof(DCB)); strcpy(buffer,"baud=1200 parity=N data=8 stop=1"); BuildCommDCB((char*)&buffer,&dcb); dcb.fRtsControl = RTS_CONTROL_ENABLE; port=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); GetCommState(port,&dcb); if(port==INVALID_HANDLE_VALUE) { CloseHandle(port); } SetCommState(port,&dcb); HeapFree(GetProcessHeap(),0,&dcb); szBuffer = malloc(dwBlockSize); memset(szBuffer, numb, dwBlockSize); WriteFile(port,szBuffer,sizeof(szBuffer),&bc,NULL); free(szBuffer); fSuccess = SetCommMask(port, EV_DSR); event.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL); //Wait for signal if (WaitForSingleObject(event.hEvent, 5000) == WAIT_OBJECT_0) { //procedure } else AfxMessageBox("There is no signal from the device"); CloseHandle(port);
-
I have external device. The device is connected with the computer through the serial-port. I send on the device number (signal), if it answers, the necessary procedure is started. I can use WaitCommEvent() for event (signal, EV_DSR), but if devise doesn't answer, WaitCommEvent will wait indefinitely, but i need to wait about 5 sec. I used WaitForSingleObject, but it doesn't work! I don't know why. In what my error? HANDLE port; DWORD bc; DCB dcb; char buffer[100]; DWORD dwBlockSize=512; void *szBuffer; int numb=255; BOOL fSuccess; OVERLAPPED event; DWORD dwEvtMask; ZeroMemory(&dcb,sizeof(DCB)); strcpy(buffer,"baud=1200 parity=N data=8 stop=1"); BuildCommDCB((char*)&buffer,&dcb); dcb.fRtsControl = RTS_CONTROL_ENABLE; port=CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); GetCommState(port,&dcb); if(port==INVALID_HANDLE_VALUE) { CloseHandle(port); } SetCommState(port,&dcb); HeapFree(GetProcessHeap(),0,&dcb); szBuffer = malloc(dwBlockSize); memset(szBuffer, numb, dwBlockSize); WriteFile(port,szBuffer,sizeof(szBuffer),&bc,NULL); free(szBuffer); fSuccess = SetCommMask(port, EV_DSR); event.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL); //Wait for signal if (WaitForSingleObject(event.hEvent, 5000) == WAIT_OBJECT_0) { //procedure } else AfxMessageBox("There is no signal from the device"); CloseHandle(port);
Two questions: Where is the event.hEvent signaled? Are you waiting for the event to become signaled or waiting for it to time out? If I understand your question you need to test for WAIT_TIMEOUT not WAIT_OBJECT_0