I try to establish a stable CAN BUS communication, but after a certain time, sometimes quite fast sometimes a few seconds, i have an OSError: 16
Maybe someone can help me, i am a micropython newbie.
I have found out that this error means "device is busy". So I increased the "time.sleep_ms(200)", but it didnt change anything.
Do I have to use uasyncio?
Error looks like this:
1266442
1266422
1266416
1266417
1266417
1266417
1266417
Traceback (most recent call last):
File "<stdin>", line 60, in <module>
OSError: 16
Line 60 is:
can.send(telegramOUT, DESTINATION_CAN_ID)
Code: Select all
import machine
import micropython
from machine import Pin, SoftI2C
import ssd1306
from pyb import CAN
from pyb import LED
import micropython
import time
from time import sleep_ms
Pin.board.EN_3V3.value(1)
globals()['message'] = 0
ledBlue = LED(1)
MOTORCOMMAND = 0x92
DESTINATION_CAN_ID = 0x141
button1 = machine.Pin('X12', machine.Pin.IN, Pin.PULL_DOWN)
IDs = (0x143, 0x140, 0x141, 0x142)
def button1_press(button1):
telegramOUT = bytes([0x90,0,0,0,0,0,0])
#print("Sending %s to CAN-id %d" % (telegram, DESTINATION_CAN_ID))
can.send(telegram, DESTINATION_CAN_ID)
can = CAN(1, CAN.NORMAL, prescaler=2, sjw=1, bs1=6, bs2=8,) #1000kbaud
#can = CAN(1, CAN.NORMAL, extframe=False,prescaler=4, sjw=1, bs1=6, bs2=8) #500kbaud
can.setfilter(0, CAN.LIST16, 0, IDs)
def my_handler_mainloop(reason):
(id, isRTR, filterMatchIndex, telegramIN) = can.recv(0)
globals()['message'] = telegramIN
#print(telegramIN)
if telegramIN == b'on':
ledBlue.on()
else:
ledBlue.off()
def my_canbus_interrupt(bus, reason):
# Don't handle code in the interrupt service routine.
# Schedule a task to be handled soon
if reason == 0:
# print('pending')
micropython.schedule(my_handler_mainloop, reason)
return
if reason == 1:
print('full')
return
if reason == 2:
print('overflow')
return
print('unknown')
can.rxcallback(0, my_canbus_interrupt)
print("Now listening on these CAN-id's: ", IDs)
while True:
telegramOUT = bytes([0x61, 0, 0, 0, 0, 0, 0, 0])
time.sleep_ms(200)
can.send(telegramOUT, DESTINATION_CAN_ID)
B_1 = globals()['message'][1]
B_2 = globals()['message'][2]
B_3 = globals()['message'][3]
B_4 = globals()['message'][4]
B_5 = globals()['message'][5]
B_6 = globals()['message'][6]
B_7 = globals()['message'][7]
EP = (B_5 + ( B_6 << 8 ))
EP = (B_4 + ( EP << 8 ))
EP = (B_3 + ( EP << 8 ))
EP = (B_2 + ( EP << 8 ))
EP = (B_1 + ( EP << 8))
#EP = (EOP_LB + (EOP_HB << 8))/ 6
print(str(EP))
time.sleep_ms(20)
button1_state = button1.value()
button1.irq(trigger=Pin.IRQ_FALLING, handler=button1_press)