CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2003
    Posts
    79

    Exit ReadCom after 5 sec.

    In my application the program writes to a device through RS232 and then waits for an answer. If a correct answer has not arrived after 5 seconds the application should do a new write. Non overlapped communication is used. The function that does the reading is named ReadCom. WaitForSingleObject wants a handle as first argument. How can I create a handle that is related to the ReadCom function?
    Or is there any other solution than using WaitForSingleObject?

  2. #2
    Join Date
    May 2002
    Posts
    159
    I am not sure you can use WaitForSingleObject if you are using non overlapped serial communication.

    If you use Overlapped communication, you can do something like,
    Code:
    OVERLAPPED osEvent= {0};
    osEvent.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    SetCommMask (commhandle, EV_RXFLAG);
    WaitCommEvent (commhandle, &dwCommEvent, &osStatus);
    
    DWORD dwRet = ::WaitForSingleObject (osStatus.hEvent, iTimeout);
    You can check the dwRet value to decide what to do. If timeout, then resend your command.

    On the other hand, if you are not using overlapped communication, you may be able to use SetCommTimeouts to set a timeout to your Read function.

    Hope this helps but I definitely prefer using overlapped communication because it is so much more robust.

    Good luck .

    Hujan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured