CAN Bus Communication

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
satya369
Posts: 32
Joined: Tue Aug 18, 2015 3:45 am

Re: CAN Bus Communication

Post by satya369 » Mon Jul 11, 2016 4:02 pm

Code: Select all

from pyb import CAN
can = CAN(1,CAN.NORMAL)
can.init(CAN.NORMAL,True,21,sjw=1,bs1=5,bs2=2)  #extframe=True for 29bit identifier, PCL1=42000000 (42MHz, 23.8095nsec),250kbps= 42MHz/(21*(1+5+2))
can.setfilter(0, CAN.MASK32, 0, (0X00FF3101,0X00FF3801)) #set filters for bank 0
can_id,can_rtr,can_fmi,can_Data = can.recv(0)

#can.setfilter(0, CAN.MASK32, 0, (0X00FF3501,0X00FF3601))  #set filters for bank 1
#can_id,can_rtr,can_fmi,can_Data = can.recv(1)
With the code above, I was able to read CAN data from a device. The issue I am having is
even though I have set the filter on bank 0 to certain ID's, I am receiving CAN messages that are different from the filter ID's also.

Another issue is, if I set the filter bank 1 and tried to receive data from bank 1 I get a timeout error.

Any one know why?

Thank you.

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

Re: CAN Bus Communication

Post by PappaPeppar » Mon Jul 11, 2016 4:16 pm

Which messages do you want to receive? Are aware of how a filter of type MASK works? If you want to receive only 0x00FF3101 and 0x00FF3801 then you better use a LIST type filter instead.

In your example you are not setting the bank to 1. You are using 0 also in the lines that are commented out.

satya369
Posts: 32
Joined: Tue Aug 18, 2015 3:45 am

Re: CAN Bus Communication

Post by satya369 » Mon Jul 11, 2016 4:30 pm

Code: Select all

from pyb import CAN
can = CAN(1,CAN.NORMAL)
can.init(CAN.NORMAL,True,21,sjw=1,bs1=5,bs2=2)  #extframe=True for 29bit identifier, PCL1=42000000 (42MHz, 23.8095nsec),250kbps= 42MHz/(21*(1+5+2))
can.setfilter(0, CAN.LIST32, 0, (0X00FF3101,0X00FF3801))
can_id,can_rtr,can_fmi,can_Data = can.recv(0)
#can.setfilter(1, CAN.LIST32, 1, (0X00FF3501,0X00FF3501))
#can_id,can_rtr,can_fmi,can_Data = can.recv(1)
I updated my code the the following and it worked perfect.

Thank you for your help.

Post Reply