I am using python
and python-can
to send messagesreceive and process messages from a CAN bus which I create like this:
bus = Bus(interface="pcan", channel="EXT_CAN", bitrate=500000, receive_own_messages=True)
The bus object is then passed down to a class that connects to that bus and implements the sending / processing:
def connect(self, *args, **kwargs):
"""Connects to bus. Expects "bus" as keyword argument."""
self._bus = kwargs.get("bus")
The class also implements a method for waiting for a specific message:
def wait_for_message(self, can_id: int, timeout: int):
self.time_start_listen = time.time()
expected_message = None
while time.time() - self.time_start_listen < timeout:
msg = self._bus.recv(timeout=1.0)
if msg.arbitration_id == can_id:
expected_message = msg
break
assert expected_message
While testing this method, I have noticed that sometimes, the expected message is simply not picked up and the method raises an AsserionError
.
I am using PCAN Viewer in parallel and I can see the expected message on the trace, so I am certain the message is actually being sent.
For reference, this is a cyclic message, with a cycle time of 1s.
There are several other cyclic messages on bus, with 20ms cycle times.
The timeout argument in the wait_for_message
method is large enough to capture at least one frame of the 1s message. This also checks out on the timestamps of the messages I do receive. However, the 1s cycle time message is sometimes skipped.
Has anyone experienced this behavior? What can be causing this?
Any hints are appreciated. TIA!
Also tried replacing the
while time.time() - self.time_start_listen < timeout:
msg = self._bus.recv(timeout=1.0)
with:
for msg in self._bus:
but the behavior is the same.