Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter result of CAN bus for IDs with Python-Can and Raspberry

Hello I have a Raspberry with an MCP2515 CAN bus device, for read entire values of broadcasting it's only this source in Python with use of python-can:

import can 
bus = can.interface.Bus(channel='can0', bustype='socketcan_native') 
notifier = can.Notifier(bus, [can.Printer()])

I need to filter that result for id, how it work? Can anybody make an example of it's possible make a filter? I watched on website of library and this is the web page of filtering: https://python-can.readthedocs.io/en/stable/bus.html#filtering

How it work? Thanks a lot for reply.

like image 555
Francesco Valla Avatar asked Sep 17 '25 21:09

Francesco Valla


1 Answers

You should set the filter using the set_filters() method of your instance of the Bus class. The argument is an iterable of dictionaries each containing a can_id, a can_mask, and an optional extended key.

bus.set_filters([{"can_id": 0x11, "can_mask": 0x21, "extended": False}])

Check out the internal api docs for more detailed information.

like image 67
insys Avatar answered Sep 20 '25 11:09

insys