我一直在使用 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