UIFlow Guide
使用ESP32内部CAN控制器资源,实现CAN总线数据收发,注:使用前需要为设备接入 CAN UNIT .
from m5stack import *
from m5ui import *
from uiflow import *
from machine import CAN
import time
setScreenColor(0x222222)
frame = None
label0 = M5TextBox(16, 53, "Device State: ", lcd.FONT_Default, 0x00ff7c, rotate=0)
label1 = M5TextBox(126, 53, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label2 = M5TextBox(16, 97, "message: ", lcd.FONT_Default, 0x00ff7c, rotate=0)
label3 = M5TextBox(16, 132, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label4 = M5TextBox(16, 162, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label5 = M5TextBox(16, 192, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
def buttonA_wasPressed():
global frame
can.send([0, 1, 2, 3, 4, 5, 6, 7], 0)
pass
btnA.wasPressed(buttonA_wasPressed)
def buttonB_wasPressed():
global frame
can.clear_tx_queue()
can.clear_rx_queue()
pass
btnB.wasPressed(buttonB_wasPressed)
def buttonC_wasPressed():
global frame
can.restart()
pass
btnC.wasPressed(buttonC_wasPressed)
can = CAN(0, extframe=True, mode=CAN.NORMAL, baudrate=CAN.BAUDRATE_25K, tx_io=17, rx_io=16, auto_restart=False)
while True:
label1.setText(str(can.state()))
if can.any():
frame = can.recv()
label3.setText(str(frame[0]))
label4.setText(str(frame[1]))
label5.setText(str(frame[3]))
wait_ms(30)
wait_ms(2)
from machine import CAN
can = CAN(0, extframe=True, mode=CAN.NORMAL, baudrate=CAN.BAUDRATE_25K, tx_io=17, rx_io=16, auto_restart=False)
can.any()
frame = can.recv()
can.send([0, 1, 2, 3, 4, 5, 6, 7], id)
list
或是tuple
, 数据帧的数据长度要求是8个byte
。can.setfilter(0, CAN.FILTER_RAW_SINGLE, [])
can.state()
can.clear_rx_queue()
can.clear_tx_queue()
can.clearfilter()
can.restart()
can.deinit()