Class CAN in pyb


I’m currently trying to run the CAN protocol example in OpenMV. However, I get the error “ImportError: cannot import name CAN” when trying to run the script :cry: .

I’m using the example code provided in the pyb.CAN documentation:

    from pyb import CAN
    can = CAN(2, CAN.LOOPBACK)
    can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))  # set a filter to receive messages with id=123, 124, 125 and 126
    can.send('message!', 123)   # send a message with id 123
    can.recv(0)                 # receive message on FIFO 0

I’m using OpenMV IDE 1.5.2 on Windows 7, 64-bit.

I’m relatively new to python. Is it possible that I’m doing something wrong so that the CAN library is not being imported properly?

Thank you for your time.

We’ve never actually tested CAN. We just enabled the micropython module.

Mmm… not sure how to go about getting it working for you.

Are you using the latest firmware BTW? We only enabled CAN for the latest firmware.

Hello, thank you for the quick reply .

I’ve just updated the board to the latest firmware (v2.3.0) as suggested and the error disappeared :smiley:

Thank you very much for your help.

This weekend I’ll try to interface the OpenMV Cam with a SAM v71 board through CAN for both transmission and reception of messages.

I’ll keep you informed of how everything went.

Thnx again and great work on the OpenMV Cam :mrgreen:

Hi, so, when debugging MicroPython driver code I heavily suggest you read the C code behind all of this stuff to get the system working.

In particular:

I say you need to do this because I really can’t help you if there are any errors since I’ve never tested the code myself. That said, following the C code isn’t that hard and will illuminate how to call the code in Python.

I’ve debugged and figured out how to do UART/I2C pretty well and I know those things are solid. SPI works good too for master mode. Slave mode is rather made to use however as it’s difficult to get in sync with a spi master, but, it’s possible. See the Pixy emulation example scripts for how to code this stuff up.

Yes please give us an update when you’re done, if you get a working CAN script we’ll add it to the examples. Thanks!