Time measurement in Python between transmitted and received CAN frame

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
msalz
Posts: 8
Joined: Wed 20. May 2020, 16:17

Time measurement in Python between transmitted and received CAN frame

Post by msalz » Wed 20. May 2020, 18:14

Hi,

I would like to measure the time between the transmission of a CAN frame and the next received CAN frame in Python (I am aware that PCAN does not provide a timestamp for transmitted frames). Therefore I use the uptime() function (https://pythonhosted.org/uptime/), which returns a timestamp measured relative to the boot time of the operating system - comparable to the PCAN timestamp. The difference between read- and writeTime is the time window.

The response is sent back from the CAN slave to the PCAN within 1-2 ms. This time is displayed in the PCAN View. If the time is measured with the API in Python, the time window is sometimes 150 ms or 2 s, which varies depending on whether the operating system (Windows 10) is restarted. So there is a significant difference between the timestamps. I expect that the measurement is not accurate to one millisecond, but there is a significant time difference.
How can I achieve a more precise measurement?

For the development I use the PCAN-Basic API: https://www.peak-system.com/PCAN-Basic.239.0.html



Best regards,
Martin

K.Wagner
Software Development
Software Development
Posts: 767
Joined: Wed 22. Sep 2010, 13:36

Re: Time measurement in Python between transmitted and received CAN frame

Post by K.Wagner » Fri 22. May 2020, 08:28

Hello,

note that you need to use high resolution timers. Looking at the function "uptime()" you mentioned, I couldn't find any information about the resolution of it. Also the unit time (import time, time.time()) you only get a resolution of 16 millis, which is not accurate enough.

Check this post, Problem with PCAN-USB Speed, which has some more information on this. Please visit a Python forum to get more information about how to get a resolution of 1 millisecond in Python applications.
Best regards,
Keneth

msalz
Posts: 8
Joined: Wed 20. May 2020, 16:17

Re: Time measurement in Python between transmitted and received CAN frame

Post by msalz » Mon 25. May 2020, 14:35

Hi,

thanks for the quick answer.

I agree that under Windows with the standard timer (64 interrupts per second) timing with the uptime() function in Python will be inaccurate due to the 15.625 ms. Which irritates me anyway:

When I calculate the difference of the timestamps (timestamp when sending by uptime() in Python, timestamp of the received CAN message by PEAK CAN) the value is partially > 150 ms.
The value should be calculated from the time between sending and receiving (approx. 2 ms in the test setup) + the measurement inaccuracy by Windows. Therefore I would have expected a much lower value.
Is it possible that the time stamps do not have the same reference point? (


Best regards.

Martin

K.Wagner
Software Development
Software Development
Posts: 767
Joined: Wed 22. Sep 2010, 13:36

Re: Time measurement in Python between transmitted and received CAN frame

Post by K.Wagner » Tue 26. May 2020, 08:43

Hello,

the timestamps within our APIs are generated using high-resolution timers, more specific, using the Windows API functions QueryPerformanceFrequency and QueryPerformanceCounter. The timestamp represents the time elapsed since the start of Windows, with an accuracy of 1 millisecond.

Note that Python supports several OSs, and therefore it has different ways to calculate time, so yes, maybe the basis time used for time functions in Python is not the same as used by our Timestamp. Try calculating your time using the functions mentioned above. Use the module ctypes to load and use those functions. There are plenty of samples in internet about this.
Best regards,
Keneth

Post Reply