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

Timestamp synchronization between interface hardware and local time #1861

Open
mdabrowski1990 opened this issue Sep 19, 2024 · 0 comments
Open

Comments

@mdabrowski1990
Copy link

Is your feature request related to a problem? Please describe.

As a user, I would like to have synchronization between hardware time (currently I am using Kvaser, but it relates to all interfaces) and local time, so I can tell when certain frame was received/transmitted in local computer time.

This code shows the problem I am facing:

from time import time

from can import BufferedReader, Bus, Message, Notifier

if __name__ == "__main__":
    kvaser_interface_1 = Bus(interface="kvaser", channel=0, fd=True, receive_own_messages=True)
    kvaser_interface_2 = Bus(interface="kvaser", channel=1, fd=True, receive_own_messages=True)  # connected with bus 1 to aknowledge frames

    buffered_reader = BufferedReader()
    notifier = Notifier(bus=kvaser_interface_1, listeners=[buffered_reader])

    message = Message(data=[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0], arbitration_id=0x100)

    for _ in range(10):
        timestamp_before_send = time()
        kvaser_interface_1.send(message)
        sent_message = buffered_reader.get_message(timeout=1)
        timestamp_after_send = time()

        print(f"-----------------------------------------------\n"
              f"Result:\n"
              f"Timestamp before send: {timestamp_before_send}\n"
              f"Message timestamp: {sent_message.timestamp}\n"
              f"Current timestamp: {timestamp_after_send}\n"
              f"Message timestamp - Timestamp before send: {sent_message.timestamp - timestamp_before_send} (expected > 0)\n"
              f"Current timestamp - Message timestamp: {timestamp_after_send - sent_message.timestamp} (expected > 0)\n"
              f"Timestamp before send <= Message timestamp <= Current timestamp: {timestamp_before_send <= sent_message.timestamp <= timestamp_after_send} (expected `True`)")

    kvaser_interface_1.shutdown()
    kvaser_interface_2.shutdown()

Example result:

Result:
Timestamp before send: 1726742267.3760204
Message timestamp: 1726735815.9516842
Current timestamp: 1726742267.3770118
Message timestamp - Timestamp before send: -6451.424336194992 (expected > 0)
Current timestamp - Message timestamp: 6451.425327539444 (expected > 0)
Timestamp before send <= Message timestamp <= Current timestamp: False (expected `True`)

Describe the solution you'd like

I would like to have a variable that provides local hardware time. Example:

from time import time

from can import Bus

if __name__ == "__main__":
    kvaser_interface_1 = Bus(interface="kvaser", channel=0, fd=True, receive_own_messages=True)
    kvaser_interface_1.time() != time()

Describe alternatives you've considered

Synchronize Message.timestamp with local time.

Additional context

Related to mdabrowski1990/uds#228 in my project which uses python-can as handler for CAN communication.

@mdabrowski1990 mdabrowski1990 changed the title Timestamp synchronization between hardware and local time Timestamp synchronization between interface hardware and local time Sep 19, 2024
@github-staff github-staff deleted a comment from Kami-prog Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant