how to receive can message?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
wdluo
Posts: 2
Joined: Tue Nov 11, 2014 5:43 am

how to receive can message?

Post by wdluo » Wed Dec 03, 2014 3:06 am

Code: Select all

import pyb
from pyb import CAN

can = CAN(1)
can.init(CAN.NORMAL,False,2,sjw=1,bs1=15,bs2=5)# 1Mbps
can.setfilter(0, CAN.MASK32, 0, (0, 0))  # set a filter to receive messages

def run():
    """
    Start the loop.
    
    """
    can_id = 0
    print('Loop started.\nPress Ctrl+C to break out of the loop.')
    while 1:
        try:
            can.send('message',can_id)
            can.recv(0,timeout=1000)
            can_id += 1
        except OSError: # VCPInterrupt # Ctrl+C in interpreter mode.
            break

if __name__ == '__main__':
    run()
send message it's ok
but have a OSError and break the loop
how i need to do?

PappaPeppar
Posts: 30
Joined: Thu Dec 18, 2014 10:38 pm

Re: how to receive can message?

Post by PappaPeppar » Fri Dec 19, 2014 6:21 pm

First, if you want to receive a message on the same bus (CAN controller) as you send the message on you have to set the bus in LOOPBACK mode.
Second, don't use 32 bit filters with basic frames. It is not supported.
The following initialization code works.

Code: Select all

can.init(CAN.LOOPBACK,False,2,sjw=1,bs1=15,bs2=5)
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))

Post Reply