简体   繁体   中英

Python-can: Fault injection on CAN bus possible?

I'm currently working on some tests for a CAN node, where I'm using python-can and Linux SocketCAN to send and receive CAN frames from the node.

Regular sending and receiving is working fine, but now I want to inject faults and see how the CAN node behaves.

Does anybody know if it's possible to do this, for example by changing the CRC of the frame. I already have one test where I take down the CAN interface, so the node goes bus-off, but there are so much more CAN errors to test.

Edit: To make thinks clear: I'm working on a test framework, using pytest and python-can, and for regular sending of CAN-frames I have the following code:

import can
bus = can.interface.Bus(bustype='socketcan', channel='can0', bitrate=250000)
msg = can.Message(arbitration_id=can_id,
                  data=data,
                  is_extended_id=False)
bus.send(msg)

And here it stops for me, What I can read for the API, there is no option for fault injections here ( Pyhon-can API ).

A another example of what I have today:

import can
bus = can.interface.Bus(bustype='socketcan', channel='can0', bitrate=250000)
msg = can.Message(arbitration_id=can_id,
                  data=data,
                  is_error_frame=True)
bus.send(msg)

Above codes generates error frames on the bus, that is part of the fault-handling tests I'm designing.

so, I'm not familiar with the library, however this appears to be the only place in the library where they expose the word CRC.

I fear there might not be a native way to do it, but you may extend the classes of the library and override the CRC field yourself, maybe.

The implementation of can.message doesn't let you touch the CRC. However, this is a "meh" news, in the sense that CRC is not computed when you first create the can.message() object.

A quick search in the socketcan.send() method has no mention of CRC either, so it may be computed somewhere in between. Or, and I fear this one more, it is computed by an external library libc which is referred in the can.interfaces.socketcan class again:

try:
    libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True)
    libc.bind.errcheck = check_status
    libc.connect.errcheck = check_status
    libc.sendto.errcheck = check_status
    libc.recvfrom.errcheck = check_status
except:
    log.warning("libc is unavailable")
    libc = None

and if this is the case, you'll have a very hard time trying to tackle the CRC. I do sincerely hope this is pointing you in the right direction, I can't really do more with my very limited knowledge of this particular library.

I have investigated this some more now, and also checked with the HW supplier of my CAN dongle. It seems that what I want to do is not possible from SW side, and I need to purchase a another HW CAN dongle that can support this kind of error injection.

The HW supplier also told me that these kinds of testings is normally done by the CAN controller suppliers, so this is not (according to him) not a normal and easy testcase.

So I'm leaving this topic now, and continues with other testing instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM