How to fix the CAN bus send 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: 88
Joined: Wed Mar 11, 2015 6:21 am

How to fix the CAN bus send error

Post by skylin008 » Wed May 18, 2022 12:03 pm

I had STM32F407VGT6 board, had porting the pyb can function base on Micropython-v1.18-443. The Can Bus LOOPBACK is ok, but when send the message used
the API:Can.send("message!", 123), send four times later every 1 seconds, had show: OSError 16. How slove this issue.

Code: Select all

from pyb import CAN

from machine import Pin
import uasyncio as asyncio


from pyb import CAN

can1 = CAN(2, CAN.NORMAL,prescaler=2, sjw=1, bs1=14, bs2=6 )

buf = bytearray(1)
buf[0] = 0

msg_id = 123
def printmsg(msg):
    buf[0] += 1
    print('msg: {} count: {}'.format(msg,buf[0]))
    can1.send((msg), msg_id)
    
def cb0(bus, reason):
  print('cb0')
  if reason == 0:
      print('pending')      
      buf1[0] += 1
      print('msg: {} count: {}'.format(bytes(lst[3]), buf1[0]))
    
  if reason == 1:
      print('full')
  if reason == 2:
      print('overflow')


can2.rxcallback(0, cb0)

async def main():
    #pin = Pin(Pin.cpu.C4, Pin.IN, Pin.PULL_UP)
    #pb = Pushbutton(pin)
    #pb.press_func(printmsg, ('push', ))
    #pb.long_func(printmsg, ('long',))
    while True:
        await asyncio.sleep(1)       #keep the uasyncio loop alive
        printmsg('message')

try:
    asyncio.run(main())
except (KeyboardInterrupt, Exception) as e:
    print('Exception {} {}\n'.format(type(e).__name__, e))
    
#finally:
#    ret = asyncio.new_event_loop()  # Clear retained uasyncio state


   

Post Reply