Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. WaitForSingleObject

WaitForSingleObject

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sergei
    wrote on last edited by
    #1

    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);

    N 1 Reply Last reply
    0
    • S Sergei

      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);

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups