Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

M4 CAN Express example code problem
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

M4 CAN Express example code problem

by valtsu23 on Wed Apr 28, 2021 4:59 am

Hi

I have Adafruit Feather M4 CAN Express with ATSAME51 running CircuitPython 6.2.0.
I uploaded Can listener example code: https://learn.adafruit.com/using-canio- ... nd-receive

At first it seems to work fine. I get the message "No messsage received within timeout" when there is nothing connected to Can Bus.
But when I reset the board computer won't see the device any more. I have try'd with Windows and Ubuntu.

Only way I can fix it is to use the safe mode and modify the code.

This problem seems to go away if I import time library and add line "time.sleep(1)" after the line 34 "print("No messsage received within timeout")"

Modified code:
Code: Select all | TOGGLE FULL SIZE
import struct

import board
import canio
import digitalio
import time

# If the CAN transceiver has a standby pin, bring it out of standby mode
if hasattr(board, 'CAN_STANDBY'):
    standby = digitalio.DigitalInOut(board.CAN_STANDBY)
    standby.switch_to_output(False)

# If the CAN transceiver is powered by a boost converter, turn on its supply
if hasattr(board, 'BOOST_ENABLE'):
    boost_enable = digitalio.DigitalInOut(board.BOOST_ENABLE)
    boost_enable.switch_to_output(False)

# Use this line if your board has dedicated CAN pins. (Feather M4 CAN and Feather STM32F405)
can = canio.CAN(rx=board.CAN_RX, tx=board.CAN_TX, baudrate=250_000, auto_restart=True)
# On ESP32S2 most pins can be used for CAN.  Uncomment the following line to use IO5 and IO6
#can = canio.CAN(rx=board.IO6, tx=board.IO5, baudrate=250_000, auto_restart=True)
listener = can.listen(matches=[canio.Match(0x408)], timeout=.9)

old_bus_state = None
old_count = -1

while True:
    bus_state = can.state
    if bus_state != old_bus_state:
        print(f"Bus state changed to {bus_state}")
        old_bus_state = bus_state

    message = listener.receive()
    if message is None:
        print("No messsage received within timeout")
        time.sleep(1)
        continue

    data = message.data
    if len(data) != 8:
        print(f"Unusual message length {len(data)}")
        continue

    count, now_ms = struct.unpack("<II", data)
    gap = count - old_count
    old_count = count
    print(f"received message: count={count} now_ms={now_ms}")
    if gap != 1:
        print(f"gap: {gap}")

valtsu23
 
Posts: 8
Joined: Tue Mar 12, 2013 2:09 pm

Please be positive and constructive with your questions and comments.


cron