Python-can:AttributeError:“列表”对象没有属性“通道”

问题描述 投票:0回答:1

我正在尝试使用bus.send_periodic发送多条消息,周期为100ms。但是,当我运行下面的代码时,出现错误: AttributeError: 'list' object has no attribute 'channel' in line 50: task = bus.send_periodic(messages, 0.1)

这对我来说没有意义,因为它的编写方式与源代码的example相同。

import logging
import time
import can
import cantools

bus = can.interface.Bus(channel='can0', bustype='socketcan', bitrate='500000')
db = cantools.database.load_file('CAN_v2.2.kcd')

def cyclic_multiple_send(bus):

    messages = []

    # LLC_Mode_Control = Voltage feedback
    messages.append(
        can.Message(
            arbitration_id=0x30041,
            data=[8],
            is_extended_id=True,
        )
    )
    # LLC_Voltage_Limits
    LLC_Voltage_Limits_data = db.encode_message('LLC_Voltage_Limits_3', {
        'Input_voltage_min_3': 0, 'Input_voltage_max_3': 750, 'Output_voltage_min_3': 0, 'Output_voltage_max_3': 750
        }
    )
    messages.append(
        can.Message(
            arbitration_id=0x30047,
            data=LLC_Voltage_Limits_data,
            is_extended_id=True,
        )
    )
    #LLC_Current_Limits
    LLC_Current_Limits_data = db.encode_message('LLC_Current_Limits_3', {
        'Output_current_min_3': 0, 'Output_current_max_3': 75
        }
    )    
    messages.append(
        can.Message(
            arbitration_id=0x30048,
            data=LLC_Current_Limits_data,
            is_extended_id=True,
        )
    )

    task = bus.send_periodic(messages, 0.1)
    assert isinstance(task, can.CyclicSendTaskABC)
    time.sleep(100)

if __name__ == "__main__":
    cyclic_multiple_send(bus)
python attributeerror python-can
1个回答
1
投票

查看示例代码的第 6 行。

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

在查看链接的示例代码时我会这样写。

bus = can.Bus(channel='can0', interface='socketcan', bitrate='500000')
因为您没有提供开箱即用的运行示例,所以我无法测试此解决方案。这只是将您的代码与 python-can 示例代码进行比较时的猜测。

© www.soinside.com 2019 - 2024. All rights reserved.