hardbyte / python-can

The can package provides controller area network support for Python developers
https://python-can.readthedocs.io
GNU Lesser General Public License v3.0
1.31k stars 604 forks source link

Filters not working properly (socketcan on rPi with MCP 2515 hat #1846

Open phospho666 opened 2 months ago

phospho666 commented 2 months ago

Describe the bug

For my aplication I need to set filters dynamically, changing them every second or so. The bus I'm working on is quite crowded and I am runing into issues where blocked IDs are still read after the filter is set, I suppose these messages come from the RX buffer.

To Reproduce

`import os import can import time

os.system('sudo ip link set can0 type can bitrate 500000') os.system('sudo ifconfig can0 up')

can0 = can.interface.Bus(channel='can0', bustype='socketcan') # socketcan_native

print("setup ok")

timeout = 999.0
start_time = time.time() filterchanged = False

while time.time() - start_time < timeout: msg = can0.recv(0.1)
if time.time() - start_time > 3 and filterchanged == False: time.sleep(2) can0.set_filters([{"can_id" : 0x200, "can_mask" : 0xFFF, "extended" : False}]) filterchanged = True print("filter changed") if msg is not None: print("Received message on can0:", msg) else: print('Timeout occurred, no message.') can0.shutdown() os.system('sudo ifconfig can0 down')`

While this code is running on the rPi, i'm sending random messages on IDs 0x100 and 0x200. As we can see on the screenshot of the terminal, messages with the ID 0x100 are still read after the filter is set up.

Screenshot from 2024-08-28 16-02-02

Expected behavior

I expect messages to be filtered right away.

Additional context

OS and version: rPi os 12 Python version: 3.11.2 python-can version: python-can interface/s (if applicable): MCP2515 2ch hat from waveshare