【问题标题】:how can i solve this? Python cantools key error regarding id我该如何解决这个问题? Python cantools关于id的关键错误
【发布时间】:2021-04-09 08:58:00
【问题描述】:

我正在尝试通过cantools 解码一个罐头消息。

for ii in range(len(dbcfiles)):
    databases.append(cantools.db.load_file(dbcfiles[ii]))
    
db = cantools.db.Database()

for database in databases:
    db.messages.extend(database.messages)

decoded_mess = []

with can.BLFReader(logfile) as can_log:
    for msg in can_log:
            print(msg.data)
            print(db.decode_message(msg.arbitration_id, msg.data))

print(msg)
# Timestamp: 1610115116.731417        ID: 0036    S                DLC:  8    c0 08 7e 08 a9 0d 0b 0f     Channel: 24

问题是最后一行db.decode_message(msg.arbitration_id, msg.data) 产生了Key Error: 54 (thats decimal of 36)。我得到了不同 id 的每个 msg 的错误,我知道 id 在 dbc 中。

【问题讨论】:

    标签: python can-bus


    【解决方案1】:

    在你的第二次 for 循环调用之后

    db.refresh()
    

    来自 cantools 文档:

    刷新() 刷新内部数据库状态。

    必须在修改数据库中的任何消息后调用此方法,以刷新编码和解码消息时使用的内部查找表。

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多