Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single millisecond send resolution with python-can, Vector #1777

Open
ack-kullum opened this issue May 12, 2024 · 2 comments
Open

Single millisecond send resolution with python-can, Vector #1777

ack-kullum opened this issue May 12, 2024 · 2 comments
Labels

Comments

@ack-kullum
Copy link

ack-kullum commented May 12, 2024

python version: 3.9.13, python-can version: 4.0.0

I am trying to send a CAN message every millisecond using python-can to access Vector's CANalyzer software. Using the code below, I would expect to see a message every millisecond:

import can
import time
import logging

bus = can.interface.Bus(bustype="vector", channel=0, bitrate=500000, app_name='CANalyzer')

def periodic_send(msg, bus):
    task = bus.send_periodic(msg, 0.001)
    assert isinstance(task, can.CyclicSendTaskABC)

    return task

tasks = []

msg = can.Message(arbitration_id=0xCE, data=[1, 1, 1, 1], is_extended_id=False)
tasks.append(periodic_send(msg,bus))

time.sleep(200)

However, when I look at the signal in CANalyzer, it shows the message being sent every 15ms or so (blue measurement cursor is first sample, orange is second):
image

I have also tried managing timing directly and have similarly been unable to achieve single millisecond intervals. Is this possible with python-can?

@ack-kullum ack-kullum added the bug label May 12, 2024
@danielhrisca
Copy link
Contributor

Does it make any difference if you patch time before importing can

import sys

if sys.platform == "win32":
    import time

    from win_precise_time import sleep as wpsleep
    from win_precise_time import time as wptime

    time.sleep = wpsleep
    time.time = wptime

import can

see https://pypi.org/project/win-precise-time/

@pierreluctg
Copy link
Collaborator

On Windows, which I assume you are since you are using Vector, python-can not using time.sleep in most setup.
See https://github.com/hardbyte/python-can/blob/main/can/broadcastmanager.py#L327

On of the reason to use win32event is exactly for the reason that you are raising. time.sleep (prior to Python 3.12) has a resolution of 15.6 ms.

Now you should try to figure out why in your setup USE_WINDOWS_EVENTS is False
https://github.com/hardbyte/python-can/blob/main/can/broadcastmanager.py#L23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants