Search code examples
pythonpython-3.xnonetypecan-buspython-can

Python3 python CAN notifier - TypeError: 'NoneType' object is not callables


I'm trying to implement a notifier for for the python-can (4.0.0) following the exact same approach as in here but I'm getting the following error:

Exception in thread can.notifier for bus "socketcan channel 'can0'":
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.7/dist-packages/can/notifier.py", line 121, in _rx_thread
    self._on_message_received(msg)
  File "/usr/local/lib/python3.7/dist-packages/can/notifier.py", line 143, in _on_message_received
    res = cast(Union[None, Optional[Awaitable[Any]]], callback(msg))
TypeError: 'NoneType' object is not callables

My code:

import os
import can

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

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

def parseData(can):
        SingleCanFrame = can.Message

notifier = can.Notifier(bus,[parseData(can)])

while(1):
        continue

os.system('sudo ifconfig can0 down')

I don't really understand what I'm doing wrong and the python-can documentation on the notifier is not very helpful either.


Solution

  • Like @TimRoberts said I was passing the function in the wrong way.

    Here is a code that works and can be tested even without the CAN Hardware.

    from time import sleep
    import can
    
    bus1 = can.interface.Bus(channel = 'test', bustype = 'virtual')
    bus2 = can.interface.Bus('test', bustype='virtual')
    
    msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7])
    
    def parseData(can):
            print( can.arbitration_id )
    
    notifier = can.Notifier(bus2,[parseData])
    
    while (1):
        bus1.send(msg)
        sleep(1)