Synchronize Time & Timer(ms)

General FreeBASIC programming questions.
Post Reply
Tonigau
Posts: 36
Joined: Feb 25, 2021 20:19

Synchronize Time & Timer(ms)

Post by Tonigau »

I want to timestamp serial messages with better than about 10ms accuracy.
I can use Time or Now to get real time-stamp & timer decimal part for milliseconds, but the two are not 'holding hands', the Time second increment is not aligned to the 999 -> 000 ms.
(I reversed the order of Print #...'s just to be sure)
I don't know If/How? I can get real time from Timer integer part.
Using Win7x64

Code: Select all

'Test Extract Decimal (Timer ms value)

#include "vbcompat.bi"

Dim As Uinteger FnumDebug

      FnumDebug = FreeFile
      Open "debug_Tms.txt" For Output As FnumDebug

   For Lp1 As Integer = 1 To 50
      Print #FnumDebug, Format(Now, "hh:mm:ss"); ".";
      Print #FnumDebug, Trim(Right(Str(Int(Timer*1000)),3))  ' Get the ms from Timer
    
' '       Print #FnumDebug, Timer; " Start"  ' same
       Sleep 20 ' ~30ms acording to Timer (but observed sometimes not ?) 
' '       Print #FnumDebug, Timer; " End"
   Next

beep

Output

Code: Select all

12:41:01.804
12:41:01.831
12:41:01.862
12:41:01.893
12:41:01.924
12:41:01.955
12:41:01.987
12:41:01.018
12:41:01.049
12:41:01.080
12:41:01.111
12:41:01.143
12:41:01.174
12:41:01.205
12:41:02.236
12:41:02.267
12:41:02.299
12:41:02.330
12:41:02.361
12:41:02.392
12:41:02.423
12:41:02.454
12:41:02.486
12:41:02.517
12:41:02.548
12:41:02.579
12:41:02.611
12:41:02.642
12:41:02.673
12:41:02.704
12:41:02.735
12:41:02.767
12:41:02.798
12:41:02.829
12:41:02.860
12:41:02.891
12:41:02.923
12:41:02.954
12:41:02.985
12:41:02.016
12:41:02.047
12:41:02.079
12:41:02.110
12:41:02.141
12:41:02.172
12:41:02.203
12:41:03.235
12:41:03.266
12:41:03.297
12:41:03.328
Tonigau
Posts: 36
Joined: Feb 25, 2021 20:19

Re: Synchronize Time & Timer(ms)

Post by Tonigau »

Trying a different method now instead of extracting the millisecond Timer digits(3) I use the diff TimerOld <--> Timer and add it to the ms count, resetting count when Real Time second changes.
Seems to work ok & is less than 30us extra CPU time.
I intend to apply the time stamp as soon as I see LOC > 0 on the com in buffer. (should be close enough)
A com buffer event would be another way, (I've used this in VB6 a long time ago).

I am still not sure why the 'Sleep 20' eats 30ms

Code: Select all

'TimeStamp milli seconds - Take2
'  use timer diff from previous loop & reset msCount when Realtime second inc.

   #Include "vbcompat.bi"

   Dim As Uinteger FnumDebug, TimerOld, TimerDiff, msCount
   Dim As String s_inc
      FnumDebug = FreeFile
      Open "debug_Tms.txt" For Output As FnumDebug

   ' This loop just simulates sub call from WM_TIMER event (about 1 ms)
   For Lp1 As Integer = 1 To 100
      TimerDiff = Int(Timer*1000) - TimerOld
      msCount += TimerDiff
      If s_inc <> Format(Now, "ss") Then msCount = 0   '< second has incremented 
        Print #FnumDebug, Format(Now, "hh:mm:ss"); "."; Format(msCount,"000") 
      s_inc = Format(Now, "ss")
      TimerOld = Int(Timer*1000)
      Sleep 20 ' Just so we see some second changes 
   Next

beep

' '   Print #FnumDebug, Timer; " Start"
' '   Print #FnumDebug, Timer; " End"


Code: Select all

11:47:28.000
11:47:28.023
11:47:28.055
11:47:28.086
11:47:28.117
11:47:28.148
11:47:28.179
11:47:28.211
11:47:28.242
11:47:28.273
11:47:28.304
11:47:28.335
11:47:28.367
11:47:28.398
11:47:28.429
11:47:28.460
11:47:28.491
11:47:28.523
11:47:28.554
11:47:28.585
11:47:28.616
11:47:29.000
11:47:29.032
11:47:29.063
11:47:29.094
11:47:29.125
11:47:29.156
11:47:29.188
11:47:29.219
11:47:29.250
11:47:29.281
11:47:29.312
11:47:29.344
11:47:29.375
11:47:29.406
11:47:29.437
11:47:29.468
11:47:29.500
11:47:29.531
11:47:29.562
11:47:29.593
11:47:29.624
11:47:29.656
11:47:29.687
11:47:29.718
11:47:29.749
11:47:29.780
11:47:29.812
11:47:29.843
11:47:29.874
11:47:29.905
11:47:29.936
11:47:29.968
11:47:30.000
11:47:30.031
11:47:30.062
11:47:30.093
11:47:30.125
11:47:30.156
11:47:30.187
11:47:30.218
11:47:30.249
11:47:30.281
11:47:30.312
11:47:30.343
11:47:30.374
11:47:30.405
11:47:30.437
11:47:30.468
11:47:30.499
11:47:30.530
11:47:30.561
11:47:30.593
11:47:30.624
11:47:30.655
11:47:30.686
11:47:30.717
11:47:30.749
11:47:30.780
11:47:30.811
11:47:30.842
11:47:30.873
11:47:30.905
11:47:30.936
11:47:30.967
11:47:31.000
11:47:31.031
11:47:31.063
11:47:31.094
11:47:31.125
11:47:31.156
11:47:31.187
11:47:31.219
11:47:31.250
11:47:31.281
11:47:31.312
11:47:31.343
11:47:31.375
11:47:31.406
11:47:31.437

Post Reply