Page 1 of 2
Bad Timing when writing Messages
Posted: Wed 31. May 2017, 18:38
by developer
Hello guys,
I just implemented my first Program in C# using the PCAN Basic API.
I use a seperate Thread with a Stopwatch in order to send out messages with an accuracy of about 1 ms.
When I hooked up 2 PCAN-CAN-USB Devices together, one linked to my program, the other one connected to PCAN-Explorer 6 I discovered really bad timing on the receiving messages in PCAN-Explorer 6 (See attached picture).
Is there any way to send out messages more precise?
Thanks a lot for your help,
developer
Re: Bad Timing when writing Messages
Posted: Thu 1. Jun 2017, 08:51
by K.Wagner
Hello,
it seems as you have a sending average of 500 milliseconds. Please check your sending thread, for instance, the way how you calculate the sending pause between messages. Do you use any kind of sleep function, e.g.
System.Threading.Thread.Sleep(xxx)?
developer wrote:I use a seperate Thread with a Stopwatch in order to send out messages with an accuracy of about 1 ms.
I'm not quite sure how you are using an Stopwatch for message sending. The Stopwatch has no "Elapsed" event (or any other event); it is thought to be used as diagnostic tool, for instance, for time measurement in your code (e.g. the time it takes to execute a function).
developer wrote:Is there any way to send out messages more precise?
You need to use so called
high resolution timers (also known as multimedia timers).
Re: Bad Timing when writing Messages
Posted: Tue 6. Jun 2017, 15:16
by developer
thank you for your reply!
Yes correct, as a first test I wanted to sent out a test frame each 500ms. I tried implementing such a High resolution timer but that didn't improve the timing by much.
I fixed this issue by generating a List of TPCANMsg objects and than having a High Priority thread just sending out these message structures. I now achieve a timing resolution of about 1-2 ms which works for me.
I use System.Threading.Thread.Sleep(xxx) for the waiting period between 2 frames. The Stopwatch was only implemented as a check
Re: Bad Timing when writing Messages
Posted: Wed 7. Jun 2017, 09:06
by PEAK-Support
If you need a 1ms timer on Windows which is accuracy, simply use the MultiMedia Timer. If you use C or C# we could send you a sample how to use. It works for us very fine in many applications.
Re: Bad Timing when writing Messages
Posted: Wed 21. Aug 2019, 06:02
by riscy00
Can you send me C# multimedia timer example to ****** thanks.
Re: Bad Timing when writing Messages
Posted: Wed 21. Aug 2019, 09:04
by PEAK-Support
We have removed your mail address...and send you the sample
Re: Bad Timing when writing Messages
Posted: Thu 5. Sep 2019, 21:26
by kieldowdle
I'm very interested in seeing that example as well if you don't mind.
from Admin: e-mail address removed - files send
Thanks.
Re: Bad Timing when writing Messages
Posted: Tue 30. Jun 2020, 08:49
by horia
Hi,
I would be interested to receive the multimedia timer files as well.
Email is the one used to register.
Thanks a lot!
Re: Bad Timing when writing Messages
Posted: Tue 30. Jun 2020, 08:57
by PEAK-Support
@horia: mail was send
Re: Bad Timing when writing Messages
Posted: Thu 2. Jul 2020, 01:08
by horia
Thanks a lot for sending the example.
One followup :
Using PCAN Explorer, it is possible to send a CAN message in a loop, by setting "Cycle time", with <1ms timing accuracy(as far as I experimented)
Is this "loop" function available using pcanbasic, or in the "PEAK-Developer" package? Or it's specific to PCAN Explorer?