【问题标题】:How to see the error frames of a CAN network with Python-CAN如何使用 Python-CAN 查看 CAN 网络的错误帧
【发布时间】:2023-03-19 02:00:01
【问题描述】:

下面的程序是我写的,

can = CAN("can0", bitrate=50000, listen_only=True, error_reporting=True)
while True:
        msg = can.recv()
        print "Msg: ", msg

但它只显示标准S 或扩展X 标志,即使当我在Terminal 中运行命令检查network activity 时,我可以看到错误计数器正在增加。

import can
import CAN
import time
import logging
#logging.basicConfig(level=logging.DEBUG)

print("Initializing Listener")
can1 = CAN('can0', bitrate=500000, listen_only=True, err_reporting=True)
#print "Bus is : ", can1.bus.get_can_bus()
can1.bus.set_filters(can_filters=[{"can_mask":0x7FF, "can_id":0x00000000, "extended":False}])
CAN_ERR_FLAG = 0x20000000

while 1:
   msg = can1.recv()
   if (msg.arbitration_id & CAN_ERR_FLAG) == CAN_ERR_FLAG:
        print "Can Error Caught"
   elif msg.is_error_frame:
        print "Finally Error Frame"

如何读取 CAN 总线的错误帧? 当我使用 commnad candump -e any,0:0,#FFFFFFFF 时一切正常

【问题讨论】:

  • "当我运行我的代码时,代码从不显示"这是什么意思?
  • 我对这个库了解不多,但如果你打算在实时 CAN 总线上运行,listen_only 似乎是错误的。您至少需要 2 个节点来确认总线上发送的消息 - 否则您将收到错误帧。
  • 您的网络活动中有哪些错误?如果只是仲裁失败,您的 Python 脚本看不到错误帧是正常的。
  • 此外,python 的Issues 可以项目:“您不能发送错误帧,因为它在 CAN 总线中是非法的。[...] is_error_frame 通常仅用于供阅读之用。”
  • @Benoit:当我使用sudo candump -e 0~0:,#FFFFFFFF 时。它确实向我显示了所有错误帧。但是当我尝试使用 python 读取罐头框架时,没有出现错误框架。

标签: python error-handling embedded can-bus automotive


【解决方案1】:

使用 Python - 3

import binascii

channel_name = "vcan0"
socketID = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW)

# socketID.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_ERR_FILTER, 0x1FFFFFFF)

error = socketID.bind((channel_name,))
print(binascii.hexlify(socketID.recv(32)))

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    相关资源
    最近更新 更多