Python3 python CAN通知器-类型错误:“NoneType”对象不是可调用对象

kmbjn2e3  于 2023-02-17  发布在  Python
关注(0)|答案(2)|浏览(196)

我尝试按照与here中完全相同的方法为python-can(4.0.0)实现一个通知程序,但是我得到了以下错误:

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

我的代码:

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')

我真的不明白我做错了什么,通知程序上的python can文档也不是很有帮助。

fkaflof6

fkaflof61#

就像@TimRoberts说的,我传递函数的方式错误。
这是一个即使没有CAN硬件也能工作和测试的代码。

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)
i2loujxw

i2loujxw2#

由于互联网上关于此主题的信息不多,因此我在此发表了自己的意见,请按照我的脚本帮助社区。我正在使用带有CAN驱动程序的工业计算机采集CAN J1939数据,这些数据由VECTOR设备使用CANNalyzer SW生成。此代码对我来说非常有效,并且以良好的采样率(ms)收集数据。

import cantools
import can
from ctypes import *
import logging
import time

# Logger config
logger = logging.getLogger(__name__)

# CAN filter
can_filter = [
    {"can_id": 0xCF00400, "can_mask": 0xFFFFFFF, "extended": True},
    {"can_id": 0xCF00300, "can_mask": 0xFFFFFFF, "extended": True},
    {"can_id": 0x10FD5200, "can_mask": 0x1FFFFFFF, "extended": True},
    {"can_id": 0x1CFE8800, "can_mask": 0x1FFFFFFF, "extended": True},
]

def main():
    """main _summary_"""
    can_bus = can.interface.Bus(bustype="socketcan", channel="can1", bitrate=500000, can_filters=can_filter)  # type: ignore
    log_filename = time.strftime("%d-%m-%Y_%H-%M", time.localtime())
    listener = can.Logger(filename=f"can_listener_{log_filename}.log", mode="a")  # type: ignore
    notifier = can.Notifier(can_bus, [listener])  # type: ignore

    try:
        while True:
            continue
    except Exception as e:
        print(e)
    finally:
        notifier.stop()

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print("can reciever stopped")
    except Exception as e:
        print(e)

相关问题