DoEvents serial port question.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: DoEvents serial port question.

  1. #1
    Join Date
    Nov 2005
    Posts
    95

    DoEvents serial port question.

    Hello:

    I am using the serial port to send & receive info.
    When I send something I need to wait until something comes back (ONcomm event), or a timer timeout occurs (error-->no response), whichever happens first. Otherwise I wait in a while loop waiting for one or the other.

    In order to respond to these events I think I must use a DoEvents....however this seems to open up to any event, whci causes bad things to happen.

    Is there a way to limit the while loop to respond only to the Oncomm or timout (timer) event, rather than any event in the whole system? The main problem is that new things are getting sent out from various other timers & user clicks before the reception is complete.

    Is it possible to wait for a reception or timer timeout, without using Doevents? Nothing else needs to happen during this lull in waiting.

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: DoEvents serial port question.

    You can set a Global variable in OnComm() and in Timer() events.
    Code:
    Public OnCommHappened as Boolean
    Public TimeoutHappened as Boolean
    'in _OnComm()
    ...
      OnCommHappened = True
    ...
    'in _Timer()
      TimeOutHappened = True
    ...
    'in your loop
     If TimeoutHappened or OnCommHappened then Exit While
    The only problem I see, I think without the DoEvents no OnComm() event would be raised...

  3. #3
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,844

    Re: DoEvents serial port question.

    You would need to have something like a busy flag that is set to true when you start your wait and then in the events that you do not want to fire a check of that flag that will exit the event if busy. Or you could disable your form while busy which would stop the user from clicking any controls or entering data while you are waiting.

  4. #4
    Join Date
    Nov 2005
    Posts
    95

    Re: DoEvents serial port question.

    It sounds like you are saying I DO need DoEvents...but is there a way to limit them?

    I only want to respond to a timeout (generate an error message) or an incoming MScomm message (data coming in). I really don't want to use doevents, but is there any other way. Doevents seems to allow other things to happen such as sending new messages before replies to old ones have been rcvd. Thus things get out of sync.
    S=send R=recieve reply

    instead of: SA RA SB RC SD RD SE RE....YOU MIGHT GET SA RA SB RB SC SD RC RD
    creating quite a mess.

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: DoEvents serial port question.

    Well, you can poll the MSComm in your loop, then you do not need to use the OnComm().
    Code:
      dim a$, tmo&
      tmo = 100000
      do
          a$=MSComm.Input
          if a$="" then tmo = tmo -1
      loop until a$<>"" or tmo = 0
    This loop will run until either a character arrived at the com port, or the counting down of tmo has reached 0.
    After that loop you check if a$="" then we have timeout
    This runs completely without DoEvents, but the timeout counting is not so elegant...
    You'd have to work out the constant yourself. I only put tmo = 100000 just out of thin air. Maybe it is to short a timeout.

  6. #6
    Join Date
    Nov 2005
    Posts
    95

    Re: DoEvents serial port question.

    thanks Wof !!!

    I was thinking of using a sleep(xxx) to cause a delay in the receive/wait loop, but then you can't exit "immediately" when something is received.

    ...I saw an example of using a system api that was somthing like tickcount or count ticks, that could be used to measure a delay (rather than forceing you to wait for a sleep to expire)...

    Do you happen to know what function I'm referring to? I should have paid more attention to it, but now can't seem to find it.

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: DoEvents serial port question.

    http://allapi.mentalis.org/apilist/GetTickCount.shtml

    Quite a good Idea. Better'n counting.
    I'll be back later to help if you can't work it out. Look at the samples anyway.

  8. #8
    Join Date
    Nov 2005
    Posts
    95

    Re: DoEvents serial port question.

    Thanks Wof

    by the way I'm posting an interesting question about doevents.

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: DoEvents serial port question.

    Ok, I shall look at it.
    Here is how I would implement a timeout using gettickcount
    Code:
    Private Declare Function GetTickCount Lib "kernel32" () As Long
    Private Const Max_TimeOutTicks = 5000 'approx. 5 seconds
    
    Private Function GetComChar() As Long
      Dim begin&, c$
      begin = GetTickCount
      Do
         c$ = MSComm.Input
      Loop Until c$ <> "" Or GetTickCount - begin > Max_imeOutCount
      If c$ <> "" Then GetComChar = Asc(c$) Else GetComChar = -1
    End Function
    The function would return -1 if timeout occured, otherwise it would return the Asc of the character received.
    As the return value is a long you wouldn't get conflicts. You can (if it is not -1) convert it to chr$() without problem. -1 instead would indicate timeout.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center