[英]AttributeError when using python-can (module 'can' has no attribute 'interface')
[英]Python-can: AttributeError: 'list' object has no attribute 'channel'
我正在尝试使用 bus.send_periodic 以 100 毫秒的周期发送多条消息。 但是,当我运行下面的代码时,出现错误: AttributeError: 'list' object has no attribute 'channel' in line 50: task = bus.send_periodic(messages, 0.1)
这对我来说没有意义,因为它的编写方式与源代码的示例相同。
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)
查看示例代码的第 6 行。
bus = can.interface.Bus(channel='can0', bustype='socketcan', bitrate='500000')
在查看您链接的示例代码时,我会这样写。
bus = can.Bus(channel='can0', interface='socketcan', bitrate='500000')
因为您没有提供开箱即用的运行示例,所以我无法测试此解决方案。 在将您的代码与 python-can 示例代码进行比较时,这只是一个猜测。
还有另一个使用 StackOverflow 的建议:请仔细阅读如何提问和如何创建一个最小的、可重现的示例。 下次请为我们提供一个更小且可重现的示例。 你必须帮助我们,我们才能帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.