Timing between events

Zenerdiode

Active Member
Licensed User
I'm using the Com port as a crude digital input device. I want to time between raising DSR and raising CTS; to an accuracy of 10ms. The time interval may be anything between one and 500 seconds. I'm trying to use the code below with timers to avoid burning CPU cycles but I'm losing track of what I'm doing(!)

Timer 2 and Timer 3 are set to 1ms.
B4X:
#Region Timer
Sub TimeEvent
   Port.DTREnable=True
   WatchDog=HW.GetTickCount
   Timer2.Enabled=True
   Timer3.Enabled=True
End Sub

Sub Timer2_Tick
   If Port.DSR=True Then
      DSRtime=HW.GetTickCount
      Timer2.Enabled=False
      Return
   End If
   If (HW.GetTickCount-WatchDog)>500 Then
      Timer2.Enabled=False
      Timer3.Enabled=False
      Port.DTREnable=False
      Msgbox("DSR has not responded; check cable and connections.","Error",cMsgBoxOK,cMsgBoxHand)
   End If
End Sub

Sub Timer3_Tick
   If Port.CTS=True Then
      CTStime=HW.GetTickCount
      Timer3.Enabled=False
      Port.DTREnable=False
      Return
   End If
   If (HW.GetTickCount-WatchDog)>500000 Then
      Timer2.Enabled=False
      Timer3.Enabled=False
      Port.DTREnable=False
      Msgbox("CTS has not responded; Check cable and connections.","Error",cMsgBoxOK,cMsgBoxHand)
   End If
End Sub
#End Region

The time interval is then CTStime-DSRtime, but I don't know where to put it. Ideally, I would also like to be able to call the routine five times with a two second interval between, but because each sequence may be upto 500 seconds long, the CPU has to be free.

Am I missing the point here and be better using:
B4X:
Do Until Port.DSR=True
   Sleep(1)
Loop

But that will lock into that loop for the duration; and I will not be able to update the elapsed time on screen (thats what I'm doing with Timer1)

I hope this makes sense as I'm tying myself in knots as I'm trying to get my problem across.
:sign0148:
 
Last edited:
Top