RS232F/M Module

Supported Products:

RS232F Module

RS232M Module

Micropython TX Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from module import RS232Module
 9import time
10
11
12rs232_0 = None
13
14
15def setup():
16    global rs232_0
17
18    M5.begin()
19    Widgets.fillScreen(0x222222)
20
21    rs232_0 = RS232Module(
22        1,
23        baudrate=115200,
24        bits=8,
25        parity=None,
26        stop=1,
27        tx=(0),
28        rx=(35),
29        txbuf=256,
30        rxbuf=256,
31        timeout=0,
32        timeout_char=0,
33        invert=0,
34        flow=0,
35    )
36
37
38def loop():
39    global rs232_0
40    M5.update()
41    rs232_0.write("hello M5" + "\r\n")
42    time.sleep(1)
43
44
45if __name__ == "__main__":
46    try:
47        setup()
48        while True:
49            loop()
50    except (Exception, KeyboardInterrupt) as e:
51        try:
52            from utility import print_error_msg
53
54            print_error_msg(e)
55        except ImportError:
56            print("please update to latest firmware")

Micropython RX Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from module import RS232Module
 9import time
10
11
12rs232_0 = None
13
14
15def setup():
16    global rs232_0
17
18    M5.begin()
19    Widgets.fillScreen(0x222222)
20
21    rs232_0 = RS232Module(
22        1,
23        baudrate=115200,
24        bits=8,
25        parity=None,
26        stop=1,
27        tx=(0),
28        rx=(10),
29        txbuf=256,
30        rxbuf=256,
31        timeout=0,
32        timeout_char=0,
33        invert=0,
34        flow=0,
35    )
36
37
38def loop():
39    global rs232_0
40    M5.update()
41    if rs232_0.any():
42        print(rs232_0.readline())
43    time.sleep(1)
44
45
46if __name__ == "__main__":
47    try:
48        setup()
49        while True:
50            loop()
51    except (Exception, KeyboardInterrupt) as e:
52        try:
53            from utility import print_error_msg
54
55            print_error_msg(e)
56        except ImportError:
57            print("please update to latest firmware")

UIFLOW2 TX Example:

tx_example.png

UIFLOW2 RX Example:

rx_example.png

core_rs232_tx_example.m5f2

cores3_rs232_rx_example.m5f2

class RS232Module

Constructors

class RS232Module(id, baudrate=9600, bits=8, parity=None, stop=1, *, ...)

Construct a UART object of the given id.

For more parameters, please refer to init.

UIFLOW2:

init.png

Methods

RS232Module.init(baudrate=9600, bits=8, parity=None, stop=1, *, ...)

Initialise the UART bus with the given parameters:

  • baudrate is the clock rate.

  • bits is the number of bits per character, 7, 8 or 9.

  • parity is the parity, None, 0 (even) or 1 (odd).

  • stop is the number of stop bits, 1 or 2.

Additional keyword-only parameters that may be supported by a port are:

  • tx specifies the TX pin to use.

  • rx specifies the RX pin to use.

  • rts specifies the RTS (output) pin to use for hardware receive flow control.

  • cts specifies the CTS (input) pin to use for hardware transmit flow control.

  • txbuf specifies the length in characters of the TX buffer.

  • rxbuf specifies the length in characters of the RX buffer.

  • timeout specifies the time to wait for the first character (in ms).

  • timeout_char specifies the time to wait between characters (in ms).

  • invert specifies which lines to invert.

    • 0 will not invert lines (idle state of both lines is logic high).

    • RS232Module.INV_TX will invert TX line (idle state of TX line now logic low).

    • RS232Module.INV_RX will invert RX line (idle state of RX line now logic low).

    • RS232Module.INV_TX | RS232Module.INV_RX will invert both lines (idle state at logic low).

  • flow specifies which hardware flow control signals to use. The value is a bitmask.

    • 0 will ignore hardware flow control signals.

    • RS232Module.RTS will enable receive flow control by using the RTS output pin to signal if the receive FIFO has sufficient space to accept more data.

    • RS232Module.CTS will enable transmit flow control by pausing transmission when the CTS input pin signals that the receiver is running low on buffer space.

    • RS232Module.RTS | RS232Module.CTS will enable both, for full hardware flow control.

Note

It is possible to call init() multiple times on the same object in order to reconfigure UART on the fly. That allows using single UART peripheral to serve different devices attached to different GPIO pins. Only one device can be served at a time in that case. Also do not call deinit() as it will prevent calling init() again.

UIFLOW2:

setup.png

RS232Module.deinit()

Turn off the UART bus.

Note

You will not be able to call init() on the object after deinit(). A new instance needs to be created in that case.

UIFLOW2:

deinit.png

RS232Module.any()

Returns an integer counting the number of characters that can be read without blocking. It will return 0 if there are no characters available and a positive number if there are characters. The method may return 1 even if there is more than one character available for reading.

For more sophisticated querying of available characters use select.poll:

poll = select.poll()
poll.register(uart, select.POLLIN)
poll.poll(timeout)

UIFLOW2:

any.png

RS232Module.read([nbytes])

Read characters. If nbytes is specified then read at most that many bytes, otherwise read as much data as possible. It may return sooner if a timeout is reached. The timeout is configurable in the constructor.

Return value: a bytes object containing the bytes read in. Returns None on timeout.

UIFLOW2:

read_all.png

read_bytes.png

read_raw_data.png

RS232Module.readinto(buf[, nbytes])

Read bytes into the buf. If nbytes is specified then read at most that many bytes. Otherwise, read at most len(buf) bytes. It may return sooner if a timeout is reached. The timeout is configurable in the constructor.

Return value: number of bytes read and stored into buf or None on timeout.

UIFLOW2:

readinto.png

RS232Module.readline()

Read a line, ending in a newline character. It may return sooner if a timeout is reached. The timeout is configurable in the constructor.

Return value: the line read or None on timeout.

UIFLOW2:

readline.png

RS232Module.write(buf)

Write the buffer of bytes to the bus.

Return value: number of bytes written or None on timeout.

UIFLOW2:

write.png

write1.png

write_line.png

write_list.png

write_raw_data.png

write_raw_data_list.png

RS232Module.sendbreak()

Send a break condition on the bus. This drives the bus low for a duration longer than required for a normal transmission of a character.

UIFLOW2:

sendbreak.png

RS232Module.flush()

Waits until all data has been sent. In case of a timeout, an exception is raised. The timeout duration depends on the tx buffer size and the baud rate. Unless flow control is enabled, a timeout should not occur.

Note

For the rp2, esp8266 and nrf ports the call returns while the last byte is sent. If required, a one character wait time has to be added in the calling script.

UIFLOW2:

flush.png

RS232Module.txdone()

Tells whether all data has been sent or no data transfer is happening. In this case, it returns True. If a data transmission is ongoing it returns False.

Note

For the rp2, esp8266 and nrf ports the call may return True even if the last byte of a transfer is still being sent. If required, a one character wait time has to be added in the calling script.

UIFLOW2:

txdone.png