Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In MS VB 6.0, I can able to wait until the specified condition met. How can I do the same thing in C.

Public Sub Wait_For_Response()
    Dim Start

   Start = Timer
   
   Do While Timer < Start + 8
      DoEvents
   
      If OK Then
        Exit Sub
      End If
      If Error Then
        Exit Sub
      End If
   Loop
End Sub


Private Sub HandleComEvent(CEvent)
    Select case CEvent
        case "OK"    
               OK = true
        case "Error"
               Error = true
    End Select     
End Sub

OK = false
Error = false

While not Ok or Error
  DoEvents()
  WaitForResponse()
wend

If OK Then
'Statements
End if

If Error Then
'Statements
End if


Above code works fine. Can anyone help me to achieve the same thing C.

1. Open Serial Port in asynchronous mode;
2. WritePort;
3. Wait until OK or Error received
4. if (OK){
//statements
}
5. Read only if Data arrives in Port using "WaitComEvent";

I have used Sleep() function. But not works.

[edit]Code block added, "Ignore HTML..." option deselected - OriginalGriff[/edit]
Posted
Updated 29-Jan-11 3:59am
v2

This should help I/O Completion Ports[^]

Do something like this:
Open the serial port with CreateFile() and remember to pass the FILE_FLAG_OVERLAPPED value in the dwFlagsAndAttributes parameter.

You can modify the serial port state as using GetCommState() and SetCommState().

GetCommTimeouts() and SetCommTimeouts() allows you to modify timeouts.

At this point you can use the handle with an IO completion port like a file or socket handle.

Attach the handle to a completion port using CreateIoCompletionPort(), initiate I/O operations with ReadFile() or WriteFile() using an OVERLAPPED structure, dequeue completed, failed or canceled operations from the completion port using GetQueuedCompletionStatus().


Regards
Espen Harlinn
 
Share this answer
 
Comments
pmk_1969 29-Jan-11 10:11am    
OK. I will try. Thanks.
Sergey Alexandrovich Kryukov 30-Jan-11 1:00am    
Great answer - my 5.
It also can be noted that looking for a DoEvents analog is not very productive because DoEvents is a bad thing, shown in OP sample.
--SA
Espen Harlinn 30-Jan-11 4:50am    
Thanks SAKryukov!
You should use threads for the serial communication. See , for instance: "Serial Communications in Win32"[^].
:)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 30-Jan-11 1:02am    
Good reference - my 5.
--SA
Espen Harlinn 30-Jan-11 4:20am    
5+ it's a better solution, but requires a better understanding of the windows api
Sergey Alexandrovich Kryukov 30-Jan-11 14:08pm    
...which make double benefits I should admit.
--SA
Espen Harlinn 30-Jan-11 14:16pm    
Sure, when you know how then it's often easier to implement this kind of stuff using a multithreaded design ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900