pdf-icon

UIFlow Guide

UIFlow 1.0 Blockly

Event

Unit

UIFlow 1.0 Project

CAN

Example

Use ESP32 internal CAN controller resources to realize CAN bus data sending and receiving, Note: Before using, you need to access CAN UNIT for the device.

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)

API

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)
  • Initialize CAN bus, configure frame expansion mode, operating mode (normal mode, loopback mode, etc.) and baud rate setting.
can.any()
  • Check for unread data in FIFOs
frame = can.recv()
  • receive data
can.send([0, 1, 2, 3, 4, 5, 6, 7], id)
  • Sends a piece of data and specifies the ID of the data frame, the ID length is 1 byte, the incoming data type is required to be list or tuple, and the data length of the data frame is required to be 8 bytes.
can.setfilter(0, CAN.FILTER_RAW_SINGLE, [])
  • Setting up filter groups
can.state()
  • Get CAN controller status
can.clear_rx_queue()
  • Clear the receive queue
can.clear_tx_queue()
  • Clearing the send queue
can.clearfilter()
  • Clear Filter Groups
can.restart()
  • Reboot CAN bus
can.deinit()
  • Stop CAN bus
On This Page