UART - Duplex Serial Communication Bus
This class transmits data through the UART.
Constructor
machine.UART
class machine.UART(UART.UARTn, baudrate, databits, parity, stopbits, flowctl)
Parametric Description:
-
UARTn
- UART number, int type, UARTn description is as follows:
UART0
- DEBUG PORT
UART1
- BT PORT
UART2
- MAIN PORT
UART3
- USB CDC PORT
UART4
- STDOUT PORT
-
baudratec
- Baud rate, int type, supports common baud rates such as4800
、9600
、19200
、38400
、57600
、115200
、230400
.
-
databits
- The data bit only supports 8 bits, int type.
-
parity
- Parity (0-None, 1-even, 2-odd), int type.
-
stopbits
- Stop bit [1 ~ 2], int type.
-
flowctl
- Hardware control flow (0-FC_NONE, 1-FC_HW), int type.
class machine.UART(UART.UARTn, baudrate, databits, parity, stopbits, flowctl)
Parametric Description:
-
UARTn
- UART number, int type, UARTn description is as follows:
UART0
- DEBUG PORT
UART1
- BT PORT
UART2
- MAIN PORT
UART3
- USB CDC PORT
-
baudratec
- Baud rate, int type, supports common baud rates such as4800
、9600
、19200
、38400
、57600
、115200
、230400
.
-
databits
- Data bits [5-8], int type.
-
parity
- Parity (0-None, 1-even, 2-odd), int type.
-
stopbits
- Stop bit [1 ~ 2], int type.
-
flowctl
- Hardware control flow (0-FC_NONE, 1-FC_HW), int type.
class machine.UART(UART.UARTn, baudrate, databits, parity, stopbits, flowctl)
Parametric Description:
-
UARTn
- UART number, int type, UARTn description is as follows:
UART0
- DEBUG PORT
UART1
- BT PORT
UART2
- MAIN PORT
UART3
- USB CDC PORT (BG95M3 is not supported.)
-
baudratec
- Baud rate, int type, supports common baud rates such as4800
、9600
、19200
、38400
、57600
、115200
、230400
.
-
databits
- Data bits [5-8], int type.
-
parity
- Parity (0-None, 1-even, 2-odd), int type.
-
stopbits
- Stop bit [1 ~ 2], int type.
-
flowctl
- Hardware control flow (0-FC_NONE, 1-FC_HW), int type.
UART Pin Correspondences:
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART1 | pin124 | pin123 | NULL | NULL |
UART2 | pin32 | pin31 | pin34 | pin33 |
UART4 | pin103 | pin104 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART1 | pin138 | pin137 | NULL | NULL |
UART2 | pin67 | pin68 | pin65 | pin64 |
UART4 | pin82 | pin81 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin12 | pin11 | NULL | NULL |
UART1 | pin63 | pin66 | NULL | NULL |
UART2 | pin67 | pin68 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin12 | pin11 | NULL | NULL |
UART1 | pin63 | pin66 | NULL | NULL |
UART2 | pin67 | pin68 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin39 | pin38 | NULL | NULL |
UART1 | pin50 | pin51 | NULL | NULL |
UART2 | pin18 | pin17 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART1 | pin29 | pin28 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin23 | pin22 | NULL | NULL |
UART1 | pin27 | pin28 | NULL | NULL |
UART2 | pin64 | pin65 | NULL | NULL |
UART4 | pin35 | pin34 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin71 | pin72 | NULL | NULL |
UART1(flowctl = 0) | pin3 | pin2 | NULL | NULL |
UART1(flowctl = 1) | pin33 | pin34 | NULL | NULL |
UART2 | pin32 | pin31 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART1 | pin27 | pin28 | NULL | NULL |
UART2 | pin35 | pin34 | pin37 | pin36 |
UART4 | pin19 | pin18 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin39 | pin38 | NULL | NULL |
UART1(flowctl = 0) | pin50 | pin51 | NULL | NULL |
UART1(flowctl = 1) | pin22 | pin23 | NULL | NULL |
UART2 | pin18 | pin17 | NULL | NULL |
UART4 | pin29 | pin28 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin39 | pin38 | NULL | NULL |
UART1(flowctl = 0) | pin50 | pin51 | NULL | NULL |
UART1(flowctl = 1) | pin22 | pin23 | NULL | NULL |
UART2 | pin18 | pin17 | NULL | NULL |
UART4 | pin29 | pin28 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin23 | pin22 | NULL | NULL |
UART1(flowctl = 0) | pin27 | pin28 | NULL | NULL |
UART1(flowctl = 1) | pin36 | pin37 | NULL | NULL |
UART2 | pin35 | pin34 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin71 | pin72 | NULL | NULL |
UART1 | pin70 | pin69 | NULL | NULL |
UART2 | pin32 | pin31 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin39 | pin38 | NULL | NULL |
UART1 | pin29 | pin28 | NULL | NULL |
UART2 | pin18 | pin17 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin124 | pin123 | NULL | NULL |
UART2 | pin32 | pin31 | pin34 | pin33 |
UART4 | pin116 | pin9 | NULL | NULL |
UART5 | pin125 | pin126 | NULL | NULL |
UART6 | pin106 | pin105 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART1 | pin29 | pin28 | NULL | NULL |
UART2 | pin18 | pin17 | NULL | NULL |
UART5 | pin23 | pin22 | NULL | NULL |
UART6 | pin86 | pin83 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART1 | pin27 | pin28 | NULL | NULL |
UART2 | pin35 | pin34 | pin37 | pin36 |
UART4 | pin19 | pin18 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin71 | pin72 | NULL | NULL |
UART1(flowctl = 0) | pin3 | pin2 | NULL | NULL |
UART1(flowctl = 1) | pin33 | pin34 | NULL | NULL |
UART2 | pin32 | pin31 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin39 | pin38 | NULL | NULL |
UART1(flowctl = 0) | pin50 | pin51 | NULL | NULL |
UART1(flowctl = 1) | pin22 | pin23 | NULL | NULL |
UART2 | pin18 | pin17 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin39 | pin38 | NULL | NULL |
UART1(flowctl = 0) | pin50 | pin51 | NULL | NULL |
UART1(flowctl = 1) | pin22 | pin23 | NULL | NULL |
UART2 | pin18 | pin17 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin39 | pin38 | NULL | NULL |
UART1 | pin22 | pin23 | NULL | NULL |
UART2 | pin18 | pin17 | pin23 | pin22 |
UART3 | pin29 | pin28 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin27 | pin26 | NULL | NULL |
UART1 | pin20 | pin19 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART1 | pin35 | pin34 | NULL | NULL |
UART2 | pin28 | pin27 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin21 | pin22 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin22 | pin21 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin23 | pin22 | NULL | NULL |
UART1 | pin27 | pin28 | NULL | NULL |
UART2 | pin35 | pin34 | pin36 | pin37 |
UART4 | pin36 | pin37 | NULL | NULL |
UART number | TX pin | RX pin | RTS pin | CTS pin |
---|---|---|---|---|
UART0 | pin39 | pin38 | NULL | NULL |
UART1 | pin29 | pin28 | NULL | NULL |
UART2 | pin18 | pin17 | NULL | NULL |
Attention:
Uart0 is not recommended
EC200ACN_LA module uart1 pin number is different from other models:
TX: Pin26
RX: Pin27
Attention:
Uart0 is not recommended
Attention:
Uart1 for EC800MCNGA, CNGB, and CNGD modules is not available
Attention:
Uart1 for EG810MCNGA, CNGB modules is not available
Attention:
The uart1 of the EG800KCN module is not available
Attention:
The uart4 of the EG915NEU_AG module is not available
Attention:
EC200UXXAA module uart4 is not available
Attention:
EC600ECN_LE&LQ module uart1 is not available
Attention:
EG912UGL_AA module uart4 is not available
To use UART4, you need to call modem.main_uart_enable_set(1) to enable UART4, and restart it as follows
import modem
# Get Main UART enabled status 1-enabled, 0-not enabled
modem.main_uart_enable_get()
# Set Main_UART enable state 1- enabled, 0- Not enabled, restart to take effect
modem.main_uart_enable_set(1)
When flowctl = 1, uart1 only maps uart1 to different pins and does not turn on the flow control function.
UART2 is initialized after the interaction port will not be used, you need to execute uart2.close() in the code or restart the module to continue to use the interaction port.
UART1 is initialized after the interaction port will not be used, you need to execute uart1.close() in the code or restart the module to continue to use the interaction port.
Example:
>>> # Creates a UART object
>>> from machine import UART
>>> uart1 = UART(UART.UART1, 115200, 8, 0, 1, 0)
Methods
uart.any
uart.any()
This method gets the size of the unread data in the receiving cache.
Return Value:
Size of data that is unread in the receiving cache.
Example:
>>> uart.any()
20 # It indicates that there is 20 bytes of unread data in the receiving cache.
uart.read
uart.read(nbytes)
This method reads data from the UART.
Parameter:
-
nbytes
- Integer type. Size of data to be read.
Return Value:
Size of data that has been read.
uart.write
uart.write(data)
This method sends data to the UART.
Parameter:
-
data
- Bytes type. Data to be sent.
Return Value:
Size of data that has been sent.
uart.close
uart.close()
This method disables the UART.
Return Value:
0
- Successful execution
-1
- Failed execution
uart.control_485
uart.control_485(UART.GPIOn, direction)
This method is used to control the 485 communication direction, and the specified GPIO is pulled up and down before and after the serial port sends data to indicate the 485 communication direction.
Parametric Description:
-
GPIOn
- GPIO Pin number to control, refer to pin definition in [Pin module](machine.Pin.html), int type.
-
direction
- Pin level change, int type, described as follows:
1
It indicates that the pin level change is: the serial port is pulled up by low before sending data, and then pulled down by high after sending data
0
It indicates that the pin level change is: the serial port is pulled down by high before sending data, and then pulled up by low after sending data
Return value Description:
Returns an integer value on success
0
,Returns an integer value on failure
-1
。
Example:
from machine import UART
uart1 = UART(UART.UART1, 115200, 8, 0, 1, 0)
art1.control_485(UART.GPIO24, 1)
uart.control_485
This API is not supported by this model
uart.set_callback
uart.set_callback(fun)
This method sets the callback function of the UART. This callback function will be triggered when data is received on the UART.
Parameter:
-
fun
- Callback function of the UART. Prototype:fun(result_list)
Parameter of the callback function:
-
result_list[0]
:Whether the data is received successfully.0 - Received successfully
Others - Receiving failed
-
result_list[1]
:Port for receiving data. -
result_list[2]
:How much data is returned.
-
Return Value:
0
- Successful execution
-1
- Failed execution
Example:
>>> from machine import UART
>>> uart1 = UART(UART.UART1, 115200, 8, 0, 1, 0)
>>>
>>> def uart_call(para):
>>> print(para)
>>> uart1.set_callback(uart_call)
Example:
"""
Runnnig this routine, you need to connect the main port on the EVB to a PC by a USB-to-Serial Port Adapter, enable the main port by a UART tool on the PC and send data to this port. Then you can see the messages sent by PC.
"""
import _thread
import utime
import log
from machine import UART
'''
The following two global variables are necessary. You can modify the values of these two global variables based on your project requirements.
'''
PROJECT_NAME = "QuecPython_UART_example"
PROJECT_VERSION = "1.0.1"
'''
* Parameter1: Port
Note: For EC100Y-CN and EC600S-CN modules, descriptions of UARTn are as follows:
UART0 - DEBUG PORT
UART1 – BT PORT
UART2 – MAIN PORT
UART3 – USB CDC PORT
* Parameter2:Baud rate
* Parameter3:Data bits (5—8)
* Parameter4:Parity (0:NONE 1:EVEN 2:ODD)
* Parameter5:Stop bits (1–2)
* Parameter6:Flow control (0: FC_NONE 1: FC_HW)
'''
# Sets the log output level
log.basicConfig(level=log.INFO)
uart_log = log.getLogger("UART")
class Example_uart(object):
def __init__(self, no=UART.UART2, bate=115200, data_bits=8, parity=0, stop_bits=1, flow_control=0):
self.uart = UART(no, bate, data_bits, parity, stop_bits, flow_control)
self.uart.set_callback(self.callback)
def callback(self, para):
uart_log.info("call para:{}".format(para))
if(0 == para[0]):
self.uartRead(para[2])
def uartWrite(self, msg):
uart_log.info("write msg:{}".format(msg))
self.uart.write(msg)
def uartRead(self, len):
msg = self.uart.read(len)
utf8_msg = msg.decode()
uart_log.info("UartRead msg: {}".format(utf8_msg))
return utf8_msg
def uartWrite_test(self):
for i in range(10):
write_msg = "Hello count={}".format(i)
self.uartWrite(write_msg)
utime.sleep(1)
if __name__ == "__main__":
uart_test = Example_uart()
uart_test.uartWrite_test()
# Examples of running results
'''
INFO:UART:write msg:Hello count=0
INFO:UART:write msg:Hello count=1
INFO:UART:write msg:Hello count=2
INFO:UART:write msg:Hello count=3
INFO:UART:write msg:Hello count=4
INFO:UART:write msg:Hello count=5
INFO:UART:write msg:Hello count=6
INFO:UART:write msg:Hello count=7
INFO:UART:write msg:Hello count=8
INFO:UART:write msg:Hello count=9
INFO:UART:call para:[0, 2, 15]
INFO:UART:UartRead msg: my name is XXX
'''
Constants
Constant | Description |
---|---|
UART.UART0 | UART0 |
UART.UART1 | UART1 |
UART.UART2 | UART2 |
UART.UART3 | UART3 |
UART.UART4 | UART4 |