Microseconds are unstable

Hello,

i want to measure the time from one SerialConnection.datareceived event to the next. With a frequency of precise 10Hz. But it doesn’t work propertly.
The code in the DataReceived is:

WinMain.Label_actual.Text = Format(System.Microseconds - actual ,"0") + " mics"
actual = System.Microseconds

(actual is double)

In the most of time the result is “100000 mics”
But after app 10 sec the result goes down to 200-4000.
The worst case is, if the mouse is moved over the window.
Then the speed drops down too.

Why? There is nothing, no mouse event!

I use new Linux Mint Mate on a new PC. The system load is lower than 4%
8GB are free.

Who can explain it to me?

On a Win10 laptop the microseconds are absolute unstable. The results are rubbish.
Ticks instead of microseconds are buggy too :frowning:

Now i use the SecondsFrom1970 as time base.
That works also very bad. It seems that the DataReceived event works absolute uneven…

Xojo uses cooperative, not preemptive interruptions, so anything making a system busy removes time from other tasks, like moving a mouse, that can fire events and consume time… The serial I/O works with small hiccups, so don’t expect precision in the time sharing in cooperative models, the slices of time can vastly differ.

Unbelievable. Then i have to solve this with my VB6.0 (again).

Have you tried implementing this as a Console app? The event handling is a lot more predictable - no user interface events - and you can use it as an external tool via a shell.

No, to learn how to develop a console app with serial input is to much work.
Now i send data with a timecode and i found out that Xojo drops some events.
The effect is stable on baudrates from 9600 - 115200.
The comport-event is unusable!!!

you just read the from to when it occur.
i believe serial using also a small buffer, if you send to fast it runs over or would send permanent.
are you sure the other side send with 10Hz?

I tested it with an Arduino with 10 Hz +/- 0,05 and a time code with single numbers from 0 to 9. No endofline, CR, etc…
The signal is stable and works without errors. I can control it in a serial monitor on the PC. It works! Now i count the time code in Xojo and found this errors.
It seems that the datareceive event fires without any reason

I generate a frequency of 1.72 Hz with a frequency generator controll it with an oscilloscope and Xojo calculates a frequency from 1,7273 to 1,7283 Hz. I use the system seconds from the clock. Ticks and microseconds doesn’t work properly.

265	153,346	1,728118	
266	153,948	1,727856	-0,000262
267	154,55	1,727596	-0,00026
268	155,151	1,727349	-0,000247
269	155,653	1,728203	0,000854
270	156,255	1,727945	-0,000258
271	156,857	1,727688	-0,000257
272	157,458	1,727445	-0,000243
273	157,96	1,728286	0,000841
274	158,561	1,728042	-0,000244
275	159,162	1,727799	-0,000243
276	159,764	1,727548	-0,000251
277	160,265	1,728387	0,000839
278	160,867	1,728136	-0,000251
279	161,469	1,727886	-0,00025
280	162,07	1,727649	-0,000237
281	162,671	1,727413	-0,000236
282	163,173	1,728227	0,000814
283	163,774	1,727991	-0,000236
284	164,376	1,727746	-0,000245
285	164,978	1,727503	-0,000243
286	165,479	1,728316	0,000813
287	166,081	1,728072	-0,000244
288	166,682	1,727841	-0,000231
289	167,285	1,727591	-0,00025
290	167,787	1,728382	0,000791
291	168,389	1,728141	-0,000241
292	168,991	1,727903	-0,000238
293	169,592	1,727676	-0,000227
294	170,193	1,727451	-0,000225
295	170,695	1,728229	0,000778
296	171,296	1,728003	-0,000226
297	171,897	1,727779	-0,000224
298	172,499	1,727546	-0,000233

As you can see, there is a big difference in timing. I can slow down to 0.1 Hz. There is always a time shift. Why?

My idea is to wait a long time and to count the events. If i divide the time by the events i get the frequency. But the value is not stable…

A Xojo GUI app is definitely not the right tool for the job. The event loop is doing a lot of work, which varies and throws your timing off.

2 Likes

serial does not work in packages. (it have a little buffer and everything in goes out.)
you can get also parts.
if you send Hello World it could be income as
el
lo
Wxrld

  • the x is an error the H was already received as Example.
    measure the time between datareceived make no sense to me.
    and be careful that plug via usb-cable did not interfere to the serial at arduino at some boards.
    if you want process the data in xojo you can use a thread but i think it is not relevant.

have you mesured the time now after 2 full rows?
|268|155,151|1,727349
test checksum,memory time and nr
|269|155,653|1,728203
test checksum,memory time and nr
check if it is in order, calculate time distance.

Serial can’t be used for this kind of timing. Not in xojo, not in another tool. Serial is buffered by the system that causes delays.

Try to have your arduino just send what the freq is and use that in xojo.

I did it in that way. The Arduino sends a realistic time in microseconds and Xojo counts the events and tries to calculate the time. The Arduino time is not very stable (temp drift) . But over one minute i got pretty good results. Thanks for all!

1 Like

I think you have it right; you use the arduino to calculate the frequency using a gate timer, and then you send the frequency to display in Xojo (or whatever other application you want). You average the results in Arduino you get better result. You use external, more stable, clock in arduino you get more accurate still. If that external clock has better temperature characteristic you get even more accurate (or you can even come up with your own temperature calibration for the internal clock if you know the temperature and have characterized the drift).