ERROR:can.socketcan.ctypes:Setting filters failed -1 Find out the can error frame

I am writing Python codes to see the Error frames. I want to set Error Filters. But this code never works when I set the CAN_RAW_ERR_FILTER. But the filter works when the filter set to CAN_RAW_FILTER. on vcan0

import can
import logging
def recv_one():
    bus = can.interface.Bus(bustype='socketcan', channel='vcan0', bitrate=250000, can_filters=[{"can_mask":0x1FFFFFFF, "can_id":0x20000000, "extended":True}])
        while 1: #print "bus: ", bus
            msg = bus.recv()
            print("Message recv : ", msg)
    except: # can.CanError:

if __name__ == '__main__':

import can
import logging
def send_one():
    bus = can.interface.Bus(bustype='socketcan', channel='vcan0', bitrate=250000, can_filters=[{"can_id": 0x11, "can_mask": 0x21, "extended": False}])
    msg = can.Message(arbitration_id=0xc0ffee,#0x20000088, #0xc0ffee,
                      data=[0, 25, 0, 1, 3, 1, 4, 1],
        print "bus: ", bus
        print("Message sent on {}".format(bus.channel_info))
    except can.CanError:
        print("Message NOT sent")

if __name__ == '__main__':

I tried to change the library code like this,

def set_filters(self, can_filters=None):
    """Apply filtering to all messages received by this Bus.

    Calling without passing any filters will reset the applied filters.

    :param list can_filters:
        A list of dictionaries each containing a "can_id" and a "can_mask".

        >>> [{"can_id": 0x11, "can_mask": 0x21}]

        A filter matches, when ``<received_can_id> & can_mask == can_id & can_mask``

    filter_struct = pack_filters(can_filters)
    print ("Filter Struct : ", filter_struct, type(filter_struct))
    res = libc.setsockopt(self.socket,
                          filter_struct, len(filter_struct)
    print("My Code")
    # TODO Is this serious enough to raise a CanError exception?
    if res != 0:
        log.error('Setting filters failed: ' + str(res))

The output comes as,

 DEBUG:can:can config: {'interface': 'socketcan_ctypes', 'channel': 'vcan0'}
INFO:can.socketcan.ctypes:Loading socketcan ctypes backend
INFO:can.socketcan.ctypes:Loading libc with ctypes
INFO:can.socketcan.native:Loading socketcan native backend
DEBUG:can.socketcan.native:CAN_* properties not found in socket module. These are required to use native socketcan
DEBUG:can.socketcan.ctypes:Result of createSocket was 3
DEBUG:can.socketcan.ctypes:Creating a filtered can bus
('Can_mask is : ', 536870911)
('The Pack_filer is :', [2684354560, 2684354559], {'can_id': 536870912, 'can_mask': 536870911, 'extended': True}, '=2I')
Filter Struct :  �����
My Code
ERROR:can.socketcan.ctypes:Setting filters failed: -1
DEBUG:can.socketcan.ctypes:Binding socket with id 3 to channel vcan0
DEBUG:can.socketcan.ctypes:calling ioctl SIOCGIFINDEX
INFO:can.socketcan.ctypes:ifr.ifr_ifindex: 22
DEBUG:can.socketcan.ctypes:bind returned: 0
DEBUG:can.socketcan.ctypes:Trying to read a msg
DEBUG:can.socketcan.ctypes:Receiving a message

1 answer

  • answered 2019-08-13 06:59 Benoît

    From the library code you are showing :

    >>> [{"can_id": 0x11, "can_mask": 0x21}] A filter matches, when <received_can_id> & can_mask == can_id & can_mask

    In your receiver, you set: "can_mask":0x1FFFFFFF, "can_id":0x20000000 but 0x1FFF_FFFF & 0x2000_0000 is always 0. If you want to keep this mask, you should change the can_id field to the same value of the ID your are sending.

    Also the can_id 0x2000_0000 is not possible on the bus as the maximum length of an ID is 29 bits and 0x2000_0000 is 30 bits.