UART - Duplex Serial Communication Bus
This class transmits data through the UART.
Constructor
machine.UART
class machine.UART(UART.UARTn, buadrate, databits, parity, stopbits, flowctl)
Parameter:
UARTn
- Integer type. UART number.UART0
- DEBUG PORTUART1
- BT PORTUART2
- MAIN PORTUART3
- USB CDC PORT (BG95M3 series module does not support it.)UART4
- STDOUT PORT (Only EC200U/EC600U/EG915U series module supports it. )buadrate
- Integer type. Baud rate. Some common baud rates are supported, like4800
,9600
,19200
,38400
,57600
,115200
and230400
.EC200U/EC600U/EG912U/EG915U series support 2400、4800、9600、14400、19200、28800、33600、38400、57600、115200、230400、460800、921600、1000000.
databits
- Integer type. Data bit. Range: [5–8]. EC600U/EC200U/EG915U/EC800G series module only supports 8 data bits.parity
- Integer type. Parity check.0
– NONE,1
– EVEN,2
– ODD.stopbits
- Integer type. Stop bit. Range: [1–2].flowctl
- Integer type. Hardware control flow.0
– FC_NONE,1
– FC_HW.
UART Pin Correspondences:
Module | Pin |
---|---|
EC600U | UART1: TX: pin124 RX: pin123 UART2: TX: pin32 RX: pin31 RTS:pin34 CTS:pin33 UART4: TX: pin103 RX: pin104 |
EC200U | UART1: TX: pin138 RX: pin137 UART2: TX: pin67 RX: pin68 CTS:pin64 RTS:pin65 UART4:(EU200UXXAA not support) TX: pin82 RX: pin81 |
EC200A/UC200A | UART0:(advise to use other ports) TX: pin12 RX: pin11 UART1: TX: pin63 RX: pin66 UART2: TX: pin67 RX: pin68 |
EC600S/EC600N | UART0: TX: pin71 RX: pin72 UART1: TX: pin3 RX: pin2 UART2: TX: pin32 RX: pin31 |
EC100Y | UART0: TX: pin21 RX: pin20 UART1: TX: pin27 RX: pin28 UART2: TX: pin50 RX: pin49 |
EC800N | UART0: TX: pin39 RX: pin38 UART1: TX: pin50 RX: pin51 UART2: TX: pin18 RX: pin17 |
BC25 | UART1: TX: pin29 RX: pin28 |
BG95 | UART0: TX: pin23 RX: pin22 UART1: TX: pin27 RX: pin28 UART2: TX: pin64 RX: pin65 uart4: TX:pin35 RX:pin34 |
EC600M | UART0: TX: pin71 RX: pin72 UART1 (flowctl = 0): TX: pin3 RX: pin2 UART1 (flowctl = 1): TX: pin33 RX: pin34 UART2: TX: pin32 RX: pin31 |
EG915U | UART1: TX: pin27 RX: pin28 UART2: TX: pin35 RX: pin34 CTS:pin36 RTS:pin37 UART4: TX: pin19 RX: pin18 |
EC800M/EG810M | UART0: TX: pin39 RX: pin38 UART1 (flowctl = 0): TX: pin50 RX: pin51 UART1(flowctl = 1): TX: pin22 RX: pin23 Note: UART1 is unavailable for EC800MCNGA、CNGD / EG810MCNGA module. UART2: TX: pin18 RX: pin17 uart4: TX:pin29 RX:pin28 |
EG800P | uart0: TX: pin39 RX: pin38 uart1: TX: pin22 RX: pin23 uart2: TX:pin18 RX:pin17 RTS:pin23 CTS:pin22 uart3: TX:pin29 RX:pin28 |
EG912N | UART0: TX: pin23 RX: pin22 UART1 (flowctl = 0): TX: pin27 RX: pin28 UART1 (flowctl = 1): TX: pin36 RX: pin37 UART2: TX: pin35 RX: pin34 |
EG912U | UART1: TX: pin27 RX: pin28 UART2: TX: pin35 RX: pin34 CTS:pin36 RTS:pin37 UART4:(EG912UGL_AA unsupported) TX:pin19 RX:pin18 |
BC32 | UART0: TX: pin21 RX: pin22 |
BC92 | UART0: TX: pin22 RX: pin21 |
EG915N | UART0: TX: pin23 RX: pin22 UART1 : TX: pin27 RX: pin28 UART2 : TX: pin35 RX: pin34 CTS:pin36 RTS:pin37 UART4:(EG915NEU_AG unsupported) TX: pin36 RX: pin37 |
1.When UART1 of EC600M/EC800M/EG912N series module is in flowctl = 1 state, modules only map UART1 to different pins but flow control is not enabled.
2.BG95 series module use UART4, need call modem.main_uart_enable_set(1) to enable UART4 and reset module to take effect,as follow:
import modem #get Main_UART state 1-enabled,0-disabled modem.main_uart_enable_get() #set Main_UART 1-enable,0-disable,need reset module to take effect modem.main_uart_enable_set(1)
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 controls the direction of RS-485 communication. Before and after sending data through the UART, the specified GPIO is pulled up and down to indicate the direction of RS-485 communication.
Parameter:
GPIOn
- Integer type. GPIO numbers to be controlled. See class Pin - Control I/O Pins for pin definitions.direction
- Integer type. Pin level change.1
- The pin is pulled high before the data is sent through the UART, and pulled low after the data is sent.0
- The pin is pulled low before the data is sent through the UART, and pulled high after the data is sent.
Return Value:
0
- Successful execution
-1
- Failed execution
Note: BC25 series/BG95-M3 series module does not support this method.
Example:
>>> from machine import UART
>>> uart1 = UART(UART.UART1, 115200, 8, 0, 1, 0)
>>> uart1.control_485(UART.GPIO24, 1)
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 |