2

我一直在使用 MCP2515 CAN 总线设备在 Raspberry 中使用 python 广播读取 J1939 消息的整个值。

我想过滤 J1939 消息,但我不明白 can-mask 的含义以及我如何发现它。在python-can的文档中说:

返回与至少一个过滤器匹配的所有消息。如果过滤器是 None 或零长度序列,则匹配所有消息。

[{"can_id": 0x11, "can_mask": 0x21, "extended": False}]

即使我理解这个“无”部分,我也不明白如何识别我的 ID 消息的掩码

例子:

我只想通过脚本获取 ID 为“0xCF00400”和“0x18fee927”的消息

import can

# CAN Setting
can_interface = 'can0'
bus = can.interface.Bus(can_interface, bustype='socketcan_native')

while True:
     message = bus.recv()

     bus.set_filters([{"can_id":0xF004 , "can_mask": ?? , "extended": True},
                      {"can_id":0xfee9 , "can_mask": ?? , "extended": True}])

我应该如何填充每个变量以及如何确定 ID 的掩码?

更新 2021 年 3 月 10 日

我已经尝试了下面的代码,但仍然返回所有消息

import can

# CAN Setting
can_interface = 'can0'

can_filters = [{"can_id":0xCF00400, "can_mask": 0, "extended": True},
               {"can_id":0x18fee927, "can_mask": 0, "extended": True}]

bus = can.interface.Bus(can_interface, bustype='socketcan_native',can_filters=can_filters)

while True:
     message = bus.recv()
     print(message)

输出:

Timestamp: 1615382511.238233    ID: 18fee500    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.238893    ID: 18fef100    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.247038    ID: 18fef100    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.247611    ID: 18fee500    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.248222    ID: 18fee900    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.248868    ID: 0cf00400    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.257056    ID: 0cf00400    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.257623    ID: 18fee900    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.258223    ID: 18fef100    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.258827    ID: 18fee500    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.267039    ID: 18fee500    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.267624    ID: 18fef100    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.268229    ID: 0cf00400    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.268835    ID: 18fee900    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.277035    ID: 18fee900    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.277620    ID: 0cf00400    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.278220    ID: 18fee500    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
Timestamp: 1615382511.278823    ID: 18fef100    X                DLC:  8    ff ff ff ff ff ff ff ff     Channel: can0
4

1 回答 1

2

您可以使用"can_id":0xCF00400, "can_mask": 0xFFFFFFF有效地过滤(在内核级别)您想要的 cob ID,而无需掩码/过滤器。(所有掩码0xFFFFFFF位设置为 1)的掩码需要与 can_id 完全匹配。

bus.set_filters([{"can_id":0xCF00400, "can_mask": 0xFFFFFFF, "extended": True},
                 {"can_id":0x18fee927, "can_mask": 0xFFFFFFF, "extended": True}])

例如,文档说:

过滤器匹配,当 <received_can_id> & can_mask == can_id & can_mask. 如果extended也设置了,它只匹配 where 的消息 <received_is_extended> == extended。否则,它仅根据仲裁 ID 和掩码匹配每条消息。

举个例子:

# The following just equals zero
0xCF00400 & 0 == 0 # True

# The following equals 0xCF00400 (217056256 in decimal) exactly
0xCF00400 & 0xFFFFFFF == 0xCF00400 # True
0xCF00400 & 0xFFFFFFF == 217056256 # True

# The following can_id would not get through the filter + mask:
0x18fee500 & 0xFFFFFFF == 0xCF00400 & 0xFFFFFFF # False

# The following obviously would get through the filter + mask:
0xCF00400 & 0xFFFFFFF == 0xCF00400 & 0xFFFFFFF # True

我把循环放在bus.set_filters()前面,放在. 这是一个设置,所以你只需要在开始时设置一次。while Truebus.recv

最好在初始化总线时添加它,如下所示:

can_filters = [{"can_id":0xCF00400, "can_mask": 0xFFFFFFF, "extended": True},
               {"can_id":0x18fee927, "can_mask": 0xFFFFFFF, "extended": True}]

bus = can.Bus(
    interface="socketcan",
    channel="can0",
    can_filters=can_filters
)

另外,我认为不赞成bustype='socketcan_native'使用. 我已经成功使用后者很长一段时间了,并且没有警告消息。interface="socketcan"

于 2021-03-07T21:01:02.023 回答