CAN timeout recovery

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
smhodge
Posts: 83
Joined: Tue Jan 22, 2019 2:16 am
Location: Kirkland, WA, USA

CAN timeout recovery

Post by smhodge » Tue Sep 22, 2020 3:11 am

Once in a blue moon (like after 30 minutes or so of receiving a couple of packets every minute over the CAN bus) I get a "OSError: [Errno 110] ETIMEDOUT" error. The code is like this:

class CanPort(CAN):
def __init__(self):
super().__init__(1)
self.buf = bytearray(8)
self.lst = [0, 0, 0, memoryview(self.buf)]
...
def getPacket(self):
if not self.any(0): return
msg = self.recv(0, self.lst)
.... do stuff with rec'd msg

The error message occurs on the recv() line. My question to the forum is how to recover from this gracefully, forgetting about what if anything was rec'd and just reset the CAN bus and continue, waiting for the next packet. Clearly, I put a try...except around it, but what do I put in the except? Should I just set "auto_restart=True" in the initialize function? Or use the CAN.restart() method? The documentation for this function says that if used (and auto_restart was False), then "the controller will follow the CAN protocol to leave the bus-off state and go into the error active state". This confuses me. Why would it go into an active error state if it was just restarted?

At any rate, if someone can give me help recover gracefully I'd appreciate it. Thanks.

Post Reply