Commu Module

This is the driver library for the module Commu for receiving and sending CAN / RS485 / I2C data.

Support the following products:

commu

UiFlow2 Example

CAN, RS485, I2C communication

Open the commu_core2_example.m5f2 project in UiFlow2.

This example shows how to receive and send data using the Commu Module.

UiFlow2 Code Block:

example.png

Example output:

None

MicroPython Example

CAN, RS485, I2C communication

This example shows how to receive and send data using the Commu Module.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from module import CommuModuleCAN
 9from module import CommuModuleRS485
10from module import CommuModuleI2C
11from hardware import Pin
12
13
14title0 = None
15label0 = None
16label1 = None
17label2 = None
18commu_0 = None
19commu_1 = None
20commu_2 = None
21
22
23def setup():
24    global title0, label0, label1, label2, commu_0, commu_1, commu_2
25
26    M5.begin()
27    Widgets.fillScreen(0x222222)
28    title0 = Widgets.Title(
29        "COMMUModule Core2 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
30    )
31    label0 = Widgets.Label("CAN Rec:", 1, 77, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
32    label1 = Widgets.Label("RS485 Rec:", 1, 121, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
33    label2 = Widgets.Label("I2C List:", 1, 166, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
34
35    commu_0 = CommuModuleCAN(0x00, baudrate=16)
36    commu_1 = CommuModuleRS485(2, baudrate=115200, bits=8, parity=None, stop=1, tx=14, rx=13)
37    commu_2 = CommuModuleI2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
38
39
40def loop():
41    global title0, label0, label1, label2, commu_0, commu_1, commu_2
42    M5.update()
43    if commu_0.any():
44        label0.setText(str((str("CAN Rec:") + str((commu_0.recv())))))
45    if BtnA.isPressed():
46        commu_0.send("uiflow2", 0, extframe=False)
47    elif BtnB.isPressed():
48        label2.setText(str((str("I2C List:") + str((commu_2.scan())))))
49    if commu_1.any():
50        label1.setText(str((str("RS485 Rec:") + str((commu_1.read())))))
51
52
53if __name__ == "__main__":
54    try:
55        setup()
56        while True:
57            loop()
58    except (Exception, KeyboardInterrupt) as e:
59        try:
60            from utility import print_error_msg
61
62            print_error_msg(e)
63        except ImportError:
64            print("please update to latest firmware")

Example output:

None

API

CommuModule

class module.commu.CommuModuleCAN(mode=0, baudrate=16, spi_baud=8000000, canIDMode=0, debug=False)

Bases: MCP2515_CAN

Create an CommuModuleCAN object

Parameters:
  • mode (int) –

    The CAN mode to use(NORMAL, LISTEN_ONLY), Default is NORMAL.

    Options:
    • NORMAL: Normal mode

    • LISTEN_ONLY: Listen only mode

  • baudrate (int) –

    The baudrate to use, Default is CAN_1000KBPS.

    Options:
    • CAN_5KBPS: 5Kbps

    • CAN_10KBPS: 10Kbps

    • CAN_20KBPS: 20Kbps

    • CAN_31K25BPS: 31.25Kbps

    • CAN_33KBPS: 33Kbps

    • CAN_40KBPS: 40Kbps

    • CAN_50KBPS: 50Kbps

    • CAN_80KBPS: 80Kbps

    • CAN_83K3BPS: 83.33Kbps

    • CAN_95KBPS: 95Kbps

    • CAN_100KBPS: 100Kbps

    • CAN_125KBPS: 125Kbps

    • CAN_200KBPS: 200Kbps

    • CAN_250KBPS: 250Kbps

    • CAN_500KBPS: 500Kbps

    • CAN_1000KBPS: 1Mbps

  • spi_baud (int) – The SPI baudrate to use, Default is 8000000.

  • canIDMode (int) –

    The CAN ID mode to use(MCP_STDEXT, MCP_EXTDONLY), Default is MCP_STDEXT.

    Options:
    • MCP_STDEXT: Standard and Extended

    • MCP_EXTDONLY: Extended only

  • debug (bool) – Whether to enable debug mode, Default is False.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from module import CommuModuleCAN

commu = CommuModuleCAN(CommuModule.NORMAL, baudrate=16)
any()

Check if any message is available.

Returns:

The current message availability.

Return type:

bool

UiFlow2 Code Block:

any.png

MicroPython Code Block:

commu.any()
info()

Get the state of error information.

Returns:

The current error information.

Return type:

str

UiFlow2 Code Block:

info.png

MicroPython Code Block:

commu.info()
recv(fifo=0, list=None, timeout=5000)

Read a message from the CAN bus.

Parameters:
  • fifo (int) – The fifo is an integer, it can be any number and compatible with Pyb.CAN

  • list (list) – list is an optional list object to be used as the return value.

  • timeout (int) – timeout is the timeout in milliseconds to wait for the receive.

Returns:

Tuple containing (can_id, is_extended, is_rtr, fmi, data)

Return type:

tuple

  • The id of the message.

  • A boolean that indicates if the message ID is standard or extended.

  • A boolean that indicates if the message is an RTR message.

  • The FMI (Filter Match Index) value.

  • An array containing the data.

UiFlow2 Code Block:

recv_message.png

recv_message_into.png

MicroPython Code Block:

commu.recv(0)

buf = bytearray(8)
lst = [0, 0, 0, 0, memoryview(buf)]
# No heap memory is allocated in the following call
commu.recv(0, lst)
send(data, can_id, extframe=False)

Send a message to the CAN bus.

Parameters:
  • data (str) – The message data.

  • can_id (int) – The CAN ID.

  • extframe (bool) – Whether to use extended frame format.

Returns:

The message data.

Return type:

str

UiFlow2 Code Block:

send.png

MicroPython Code Block:

commu.send('uiflow2', 0, extframe=False)
class module.commu.CommuModuleRS485(id, **kwargs)

Bases: object

The CommuModuleRS485 class wraps an instance of the UART class.

For more details, see hardware.UART.

class module.commu.CommuModuleI2C(id, **kwargs)

Bases: object

The CommuModuleI2C class wraps an instance of the I2C class.

For more details, see machine.I2C. – a two-wire serial protocol.