Accurate Timers?


Ive noticed the accuracy of the Timer component provided by Delphi/Borland is poor - especially when
accurate timing is crucial in data transfer with your interface boards.

How do you improve it?

After doing some further research - I feel this can be a heavy subject, so please don’t waste your time on this one!

Please specify a product.


I wrote some code in Borland C++ Builder.
I used the bitset template class to create a 4 bit address from a decimal number to work with 4 of the VM167 digital outputs.

I wanted to put a delay each time when calling SetDigitalChannel(int,int).
Ive tried using the Timer component in a for loop iteration to call the function 4 individual times.

for (int = i; i < 4; i++)
Timer1->Enabled = true; //my code is in Timer1Timer1 Event which is now called
I expect my code to be called 4 times, but it is only called once??

Im using the library, time_t

The code above enables the timer 4 times, you only need to enable the timer once. The timer routine will then be called each time that a timer event occurs, the timer interval is specified in the timer properties.

for (int = i; i < 4; i++)	
	     Timer1->Enabled = true; //my code is in Timer1Timer1 Event which is now called

That’s not how it works at all… From what I can see I think you need the Sleep(…) function defined in <windows.h>.

for(int i=0; i<4; ++i) {
    Sleep(1000); // Suspend the current thread for 1000 milliseconds

Not sure why you would want to do this though…

The Borland Timer component uses Windows timers (SetTimer, …). Forget about “accurate timing in data transfer with your interface boards”, you will never achieve it.

Rather than using a timer VCL component dropped on your form, you’d be better off using a thread with a loop and using the sleep function of your language but as the above post says, accurate timing in the milliseconds range over usb in windows isn’t going to be great. No idea how usb oscilloscopes could ever achieve this (actually, i suspect they don’t they probably send data packets with recorded signals that can be reconstructed).

FYI Delphi is not a company. It’s another language created by Borland once upon a time.