如何解码来自CAN总线的消息(Iptronik)

2024-06-23 19:15:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一个CAN总线检索以下消息和信号数据(从Iptronik sim stg机器)。我用python编写它,使用python can库,我有ubuntu18.04

检索到的消息/信号:

Timestamp: 1570622811.311113        ID: 0060    S                DLC:  8    e4 7f 55 55 55 55 04 00     Channel: can0
Timestamp: 1570622811.811173        ID: 0060    S                DLC:  8    e4 7f 55 55 55 55 0f 06     Channel: can0
Timestamp: 1570622812.311224        ID: 0060    S                DLC:  8    e4 7f 55 55 55 55 65 1a     Channel: can0
Timestamp: 1570622812.811266        ID: 0060    S                DLC:  8    e4 7f 55 55 55 55 47 0c     Channel: can0
Timestamp: 1570622813.311266        ID: 0060    S                DLC:  8    e4 7f 55 55 55 55 2c 58     Channel: can0
Timestamp: 1570622813.811316        ID: 0060    S                DLC:  8    e4 7f 55 55 55 55 20 36     Channel: can0

从dbc文件中我得到了这个信息。在

^{pr2}$

当我转换为前4位和后4位时,得到的数字与dbc文件不对应。在

(228, 127, 3, 0)
(228, 127, 3, 0)
(228, 127, 62, 3)
(228, 127, 162, 16)
(228, 127, 143, 28)
(228, 127, 252, 93)

你能帮我或给我一些建议,根据dbc指令正确地转换信号吗。 提前感谢:)


Tags: 文件数据id消息信号channelsimcan
2条回答

这可能对你有帮助。。。在

import cantools
import can
from pprint import pprint 

db = cantools.database.load_file('******_dbc.dbc') #path of .dbc file
print( db.messages)
can_bus = can.interface.Bus('can0', bustype='socketcan')
message = can_bus.recv()
for msg in can_bus:
     print ( db.decode_message(msg.arbitration_id, msg.data))

您可以使用CAN总线工具(https://cantools.readthedocs.io/en/latest/)加载.DBC文件并正确解码CAN消息:

pip install cantools

下面是示例代码:

^{pr2}$

相关问题 更多 >

    热门问题