delay between events...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bluestag
    New Member
    • Mar 2008
    • 2

    delay between events...

    hello all...
    this is my first post... :)

    i need to send specific characters on rs232 at specified intervals...


    i call the 1st sub and it sends something on rs232 using trysend sub and for the delay i've used a variable timercnt1 ... u set timercnt1 to 50 and enable the timer.... as the timer ticks it counts down and at 0 it shuts off...

    [code=vbnet]
    Private Sub stdlengthcmdsen d()
    ' motor1 start at full speed
    trysend("9" & "1")
    timercnt1 = 50
    Timer1.Enabled = True
    While timercnt1 <> 0
    ' delay
    End While
    'WriteMessage(" while end" & Chr(13), True)

    'motor stop
    ' in this time it wud've covered the dstance...
    trysend("q")
    trysend("3")

    End Sub

    Private Sub timer1_tick(ByV al sender As System.Object, ByVal e As System.EventArg s) Handles Timer1.Tick
    ' For 5 seconds 50 ticks
    If timercnt1 = 0 Then
    Timer1.Enabled = False
    'oCP.Write(Enco ding.ASCII.GetB ytes(textinbox & Chr(13)))
    'WriteMessage(" time up" & Chr(13), True)
    End If
    timercnt1 = timercnt1 - 1
    End Sub[/code]

    and now..... the problem with this is....
    it simply hangs... nothign happens.. and i believe nothing is sent on rs232 second time when it has to... i guess its stuck in the while loop

    i've checked out the sleep option....
    but the problem with that is... my form has grafix that have to be drawn.. so if u go in sleep once they're lost...

    please suggest alternatives...

    thanks :)
    Last edited by Killer42; Apr 1 '08, 01:29 PM. Reason: Changed CODE=vb tag to vbnet
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    My advice is to put a DoEvents inside that While loop.

    Note though, that this advice is based on my knowledge of VB6. Dunno how closely the versions match in this regard.

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Do you really need your code to stop and wait for the 5 seconds, or do you just need the output to be sent at that rate?

      What I'm thinking is, why not just queue any data and continue on your way, and have the timer take things off the queue each 5 seconds and send them?

      Comment

      • !NoItAll
        Contributor
        • May 2006
        • 297

        #4
        Mr Killer is right - set the timer interval to 5000 and put your code inside the timer event.

        Comment

        • bluestag
          New Member
          • Mar 2008
          • 2

          #5
          Originally posted by Killer42
          Do you really need your code to stop and wait for the 5 seconds, or do you just need the output to be sent at that rate?

          What I'm thinking is, why not just queue any data and continue on your way, and have the timer take things off the queue each 5 seconds and send them?
          i didnt quite understand what u meant by queuing here...
          if i write the 2 sets of instructions in 2 different timer tick events and when i enable them wont they clash? .... coz here m controlling a robot and have to send the instructions only one after another...and the interval is also not constant for each set

          newaz i've got an idea using multiple timers for each set procedure and by calling the next one from the previous timer itself.. .. if that works i'll share it with u all...

          thanks
          -BlueStag

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            What I was thinking was something like this...

            You have an array to hold the instructions to be sent. Each entry holds whatever information you need to send for that instruction, plus an indicator of the minimum gap required before (or is it after?) that instruction.

            You create a routine to add an instruction to the "queue" (array). All it does is take the instruction you pass it, and add it onto the end of the queue.

            You have a single timer which ticks once per second, or ten times a second, or whatever turns out to be appropriate. Each time it ticks, it checks the next item in the queue (if any). It checks whether at least the minimum required time has elapsed since the last instruction was sent. If not, it does nothing. Otherwise, it sends the next instruction, and removes it from the queue. And sets an indicator saying what time it was sent.

            Does that make more sense?

            So your code could be saying things along the lines of "send instruction ABC and wait at least 5 seconds before the next one", by calling the "add to queue" routine. Or depending on how the robot works, it might be more like "send instruction ABC after a pause of at least 5 seconds since the last one".

            The basic idea is to keep the coding fairly simple. You have a queue with two simple actions - the "add to queue" routine to put things on the queue, and the timer tick to take them off.

            Comment

            Working...