Canbus Transceive Error

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
skylin008
Posts: 67
Joined: Wed Mar 11, 2015 6:21 am

Canbus Transceive Error

Post by skylin008 » Mon Jul 06, 2020 11:54 am

Hello, I have two pyboards and commuicate with each other via CanBus. when them work receiver and sender simultaneously, only received four bytes each other. when this script single run and comunicate with PC tools ,it works well. How solve this issue. Thanks!

A board:

Code: Select all

import pyb
can = pyb.CAN(1, mode=pyb.CAN.NORMAL, prescaler=4, bs1=14, bs2=6)
"""
This is a pyboard v1.12.0. PYBv1.1 with STM32F405RG.

125k (prescaler=16, sjw=1, bs1=14, bs2=6)
250k (prescaler=8, sjw=1, bs1=14, bs2=6)
500k (prescaler=4, sjw=1, bs1=14, bs2=6)
1M (prescaler=2, sjw=1, bs1=14, bs2=6)

1000.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=4, bs1=14, bs2=6)
800.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=5, bs1=14, bs2=6)
500.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=8, bs1=14, bs2=6)
400.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=10, bs1=14, bs2=6)
250.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=16, bs1=14, bs2=6)
200.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=20, bs1=14, bs2=6)
160.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=25, bs1=14, bs2=6)
125.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=32, bs1=14, bs2=6)
100.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=40, bs1=14, bs2=6)
80.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=50, bs1=14, bs2=6)
50.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=80, bs1=14, bs2=6)
40.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=100, bs1=14, bs2=6)
32.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=125, bs1=14, bs2=6)
25.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=160, bs1=14, bs2=6)
20.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=200, bs1=14, bs2=6)
16.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=250, bs1=14, bs2=6)
10.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=400, bs1=14, bs2=6)
"""
can.setfilter(0, pyb.CAN.LIST16, 0, (0x123,0x124,0x125,0x126))

import uasyncio

async def master():
    while True:
        can.send("message!", 0x123)
        await uasyncio.sleep_ms(100)

async def slave():
    count = 0
    while True:
        if can.any(0):
            res = can.recv(0, timeout = 10)
            count += 1
            print(res)
            await uasyncio.sleep_ms(1)        
def run_app():
    loop = uasyncio.get_event_loop()
    loop.create_task(slave())
    loop.create_task(master())        
    loop.run_forever()
run_app()     
B board:

Code: Select all

import pyb
can = pyb.CAN(1, mode=pyb.CAN.NORMAL, prescaler=4, bs1=14, bs2=6)
"""
This is a pyboard v1.12.0. PYBv1.1 with STM32F405RG.

125k (prescaler=16, sjw=1, bs1=14, bs2=6)
250k (prescaler=8, sjw=1, bs1=14, bs2=6)
500k (prescaler=4, sjw=1, bs1=14, bs2=6)
1M (prescaler=2, sjw=1, bs1=14, bs2=6)

1000.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=4, bs1=14, bs2=6)
800.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=5, bs1=14, bs2=6)
500.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=8, bs1=14, bs2=6)
400.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=10, bs1=14, bs2=6)
250.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=16, bs1=14, bs2=6)
200.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=20, bs1=14, bs2=6)
160.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=25, bs1=14, bs2=6)
125.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=32, bs1=14, bs2=6)
100.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=40, bs1=14, bs2=6)
80.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=50, bs1=14, bs2=6)
50.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=80, bs1=14, bs2=6)
40.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=100, bs1=14, bs2=6)
32.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=125, bs1=14, bs2=6)
25.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=160, bs1=14, bs2=6)
20.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=200, bs1=14, bs2=6)
16.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=250, bs1=14, bs2=6)
10.00 kBaud: can = CAN(1, CAN.NORMAL, prescaler=400, bs1=14, bs2=6)
"""
can.setfilter(0, pyb.CAN.LIST16, 0, (0x123,0x124,0x125,0x126))

import uasyncio

async def master():
    while True:
        can.send("cantest!", 0x124)
        await uasyncio.sleep_ms(100)

async def slave():
    count = 0
    while True:
        if can.any(0):
            res = can.recv(0, timeout = 10)
            count += 1
            print(res)
            await uasyncio.sleep_ms(1)        
def run_app():
    loop = uasyncio.get_event_loop()
    loop.create_task(slave())
    loop.create_task(master())        
    loop.run_forever()
run_app()    

Post Reply