python-can icon indicating copy to clipboard operation
python-can copied to clipboard

Vector bus receiving buffered messages

Open manuelporta opened this issue 2 years ago • 2 comments

Hi! I'm building a tool to record CAN signals.

I've found that, once the system is booted, the first recording is allright but the following ones capture about one second of messages that corresponds to the end of the previous recording and then jump to the correct messages (it can be seen in both the CAN timestamps and the sensor's own timestamps).

After many tests, I've found that instantiating twice the bus solves this problem, but only if I let some time between those instantiations (it works both putting some code between them or just calling time.sleep(0.1)).

The instruction I use to instantiate the bus is the following:

            bus = pycan.interface.Bus(
                bustype="vector",
                channel=1,
                bitrate=5000,
                receive_own_messages=False,
                app_name="Recorder",
            )

As it is a really difficult issue to replicate and I'm new to this library I don't know what else data I can add. I've profiled the execution but didn't found anything. Please let me know if I should add anything.

Thanks all!

manuelporta avatar May 15 '23 11:05 manuelporta

Hello, I think the bus has a RX buffer, but I don't know if the python-can classes expose a way to clear that low level buffer. What you could do is to get the current timestamp just before you create the Bus, Notifier or Listener instance using start_time = time.time() and ignore messages that have .timestamp < start_time. Otherwise you might want to look into a low-level API (possibly at kernel level?) to clear the RX buffer of the corresponding interface.

AlexanderRavenheart avatar Sep 29 '23 06:09 AlexanderRavenheart

Hello, I think the bus has a RX buffer, but I don't know if the python-can classes expose a way to clear that low level buffer. What you could do is to get the current timestamp just before you create the Bus, Notifier or Listener instance using start_time = time.time() and ignore messages that have .timestamp < start_time. Otherwise you might want to look into a low-level API (possibly at kernel level?) to clear the RX buffer of the corresponding interface.

Thanks for your answer @AlexanderRavenheart ! I hadn't thought of that solution before, I would try it as soon as I can. At the moment, I'm just creating two identical buses with some other instructions between them: the first one seems to "clean" all the buffer and the second one works properly.

manuelporta avatar Oct 02 '23 07:10 manuelporta